Commit 5a54820b authored by sergio1990's avatar sergio1990

Completed caching

parent 8ba8d79f
......@@ -20,10 +20,11 @@ Gem::Specification.new do |spec|
spec.add_dependency "colorize", ">= 0.7.2"
spec.add_dependency "active_redis", "0.0.9"
spec.add_dependency "activesupport", '>= 3.0.0'
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake", ">= 10.3.0"
spec.add_development_dependency "activesupport", ">= 3.1.0"
# spec.add_development_dependency "activesupport", ">= 3.1.0"
spec.add_development_dependency "rspec", "~> 3.0.0.beta"
spec.add_development_dependency "rspec-its", "1.0.1"
spec.add_development_dependency "pry-nav", "0.2.3"
......
require 'ostruct'
require 'json'
module BetfairApiNgRails
module Api
module Caching
class ResponseCache
include Api::Caching::Helper
include Api::Caching::Models
attr_reader :method, :sig_params
def initialize(method, params)
@method = method
@sig_params = createsig(params)
end
def is_cached?
!cache_result.nil?
end
def responser
Api::Http::Responser.new prepare_response
end
def cache!(response)
BetfairCache.create(create_params(response)).expire expire_time
end
private
def cache_result
@_cache_result ||= BetfairCache.where(method: method, params: sig_params).first
end
def prepare_response
OpenStruct.new body: cache_result.response, code: '200'
end
def expire_time
BetfairApiNgRails.config.cache_expire
end
def create_params(response)
{ method: method, params: sig_params, response: JSON.dump(response) }
end
end
end
end
......
......@@ -9,7 +9,11 @@ module BetfairApiNgRails
def self.included(base)
base.send :class_eval, <<-CODE
def request_with_caching(method, params = {})
res = request_without_caching(method, params)
if is_caching_on?
process_with_caching method, params
else
return request_without_caching(method, params)
end
end
alias_method :request_without_caching, :request
alias_method :request, :request_with_caching
......@@ -18,6 +22,29 @@ module BetfairApiNgRails
private
def is_caching_on?
BetfairApiNgRails.config.use_cache
end
def process_with_caching(method, params)
cache_service = Api::Caching::ResponseCache.new(method, params)
if cache_service.is_cached?
return cache_service.responser
else
return make_real_request(method, params, cache_service)
end
end
def make_real_request(method, params, cache_service)
res = request_without_caching(method, params)
cache_service.cache!(res.result) if need_caching?(res)
res
end
def need_caching?(res)
!(res.nil? || res.has_error?)
end
end
end
end
......
require 'spec_helper'
describe BetfairApiNgRails::Api::Caching::ResponseCache do
let(:method) { :method }
let(:params) { :params }
subject(:cache) { described_class.new(method, params) }
before { allow_any_instance_of(described_class).to receive(:createsig).with(:params).and_return :sig_params }
describe "#is_cached?" do
before { expect(cache).to receive(:cache_result).and_return(cache_result) }
context 'when cache result returns item' do
let(:cache_result) { double }
its(:is_cached?) { is_expected.to be_truthy }
end
context 'when cache result returns no item' do
let(:cache_result) { nil }
its(:is_cached?) { is_expected.to be_falsey }
end
end
describe "#responser" do
before { expect(cache).to receive(:prepare_response).and_return :response }
it "returns http responser with cached info" do
expect(BetfairApiNgRails::Api::Http::Responser).to receive(:new).with(:response)
cache.responser
end
end
describe "#cache!" do
let(:response) { { some: :key } }
let(:record) { double(:record) }
before do
expect(cache).to receive(:create_params).with(response).and_return :create_params
expect(cache).to receive(:expire_time).and_return :expire_time
end
it "saves response in cache" do
expect(BetfairApiNgRails::Api::Caching::Models::BetfairCache).to receive(:create).with(:create_params).and_return record
expect(record).to receive(:expire).with :expire_time
cache.cache! response
end
end
context 'private method' do
describe "#cache_result" do
it "returns result from cache" do
expect(BetfairApiNgRails::Api::Caching::Models::BetfairCache).to receive(:where).with(method: :method, params: :sig_params).and_return []
cache.send :cache_result
end
end
describe "#prepare_response" do
let(:cache_result) { double(:cache_result, response: 'some response text') }
before { allow(cache).to receive(:cache_result).and_return cache_result }
subject(:response) { cache.send(:prepare_response) }
it { is_expected.to respond_to(:body) }
it { is_expected.to respond_to(:code) }
its(:body) { is_expected.to eq 'some response text' }
its(:code) { is_expected.to eq '200' }
end
describe "#expire_time" do
it "returns time from config" do
expect(BetfairApiNgRails).to receive_message_chain(:config, :cache_expire).and_return 4
expect(cache.send(:expire_time)).to eq 4
end
end
describe "#create_params" do
it "returns params for saving cache" do
expect(cache.send(:create_params, {some: :key})).to eq(method: :method, params: :sig_params, response: "{\"some\":\"key\"}")
end
end
end
end
\ No newline at end of file
$TESTING=true
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
module ActiveRedis
class Base
def self.attributes(*args); end
end
end
require 'rspec/its'
require 'betfair_api_ng_rails'
......
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