Commit 8ba8d79f authored by sergio1990's avatar sergio1990

Basic class skeleton of caching subsystem

parent 86b7103c
......@@ -2,6 +2,7 @@ require "betfair_api_ng_rails/version"
require "betfair_api_ng_rails/errors"
require "betfair_api_ng_rails/api/request_methods"
require "betfair_api_ng_rails/railtie" if defined?(Rails)
require 'active_redis'
module BetfairApiNgRails
......@@ -19,6 +20,19 @@ module BetfairApiNgRails
autoload :Connection, 'betfair_api_ng_rails/api/connection'
autoload :Hashalator, 'betfair_api_ng_rails/api/hashalator'
module Caching
autoload :Helper, 'betfair_api_ng_rails/api/caching/helper'
autoload :ResponseCache, 'betfair_api_ng_rails/api/caching/response_cache'
module Models
autoload :BetfairCache, 'betfair_api_ng_rails/api/caching/models/betfair_cache'
end
end
module Logs
autoload :FileLogger, 'betfair_api_ng_rails/api/logs/file_logger'
......@@ -134,6 +148,7 @@ module BetfairApiNgRails
autoload :Parsing, 'betfair_api_ng_rails/api/connection_ext/parsing'
autoload :Formatting, 'betfair_api_ng_rails/api/connection_ext/formatting'
autoload :Logging, 'betfair_api_ng_rails/api/connection_ext/logging'
autoload :Caching, 'betfair_api_ng_rails/api/connection_ext/caching'
end
......
module BetfairApiNgRails
module Api
module Caching
module Helper
def createsig(body)
Digest::MD5.hexdigest sigflat(body)
end
def sigflat(body)
res = nil
if body.class == Hash
arr = []
body.each do |key, value|
arr << "#{sigflat key}=>#{sigflat value}"
end
res = arr
end
if body.class == Array
str = ''
res = body.map do |value|
sigflat value
end.sort!.each do |value|
str << value
end
end
if body.class != String
res = body.to_s << body.class.to_s
end
res || body
end
end
end
end
end
\ No newline at end of file
module BetfairApiNgRails
module Api
module Caching
module Models
class BetfairCache < ActiveRedis::Base
attributes method: :string,
params: :string,
response: :string
end
end
end
end
end
\ No newline at end of file
module BetfairApiNgRails
module Api
module Caching
class ResponseCache
include Api::Caching::Helper
end
end
end
end
\ No newline at end of file
require 'spec_helper'
class TestObject
include BetfairApiNgRails::Api::Caching::Helper
end
describe BetfairApiNgRails::Api::Caching::Helper do
subject(:helper) { TestObject.new }
describe ".createsig" do
it "creates hex from sigflat result" do
expect(helper).to receive(:sigflat).with("some").and_return "some-sigflat"
expect(Digest::MD5).to receive(:hexdigest).with "some-sigflat"
helper.createsig "some"
end
describe 'real tests' do
context 'sigs equal values' do
let(:value1) { {key1: :value2} }
let(:value2) { {key1: :value2} }
it 'expects to be equal' do
val1 = helper.createsig(value1)
val2 = helper.createsig(value2)
expect(val1).to eq val2
end
end
context 'sigs different values' do
let(:value1) { {key1: :value2} }
let(:value2) { {key1: :value3} }
it 'expects not to be equal' do
val1 = helper.createsig(value1)
val2 = helper.createsig(value2)
expect(val1).not_to eq val2
end
end
end
end
describe ".sigflat" do
subject { helper.sigflat(value) }
context "when String passed" do
let(:value) { 'some' }
it { is_expected.to eq("some") }
end
context "when Integer passed" do
let(:value) { 1 }
it { is_expected.to eq("1Fixnum") }
end
context "when Hash passed" do
let(:value) { { key: :value } }
it { is_expected.to eq("{:key=>:value}Hash") }
end
context "when Array passed" do
let(:value) { ["val1", "val2"] }
it { is_expected.to eq("[\"val1\", \"val2\"]Array") }
end
end
end
\ No newline at end of file
require 'spec_helper'
describe BetfairApiNgRails::Api::Caching::ResponseCache do
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment