Permalink
Browse files

an attempt to cache deserialized api response (instead of XML)

  • Loading branch information...
1 parent 9b19ab6 commit 49b71c5a281019721e23a6a5c12c80b321fe6229 filip committed May 22, 2012
View
@@ -0,0 +1,10 @@
+
+/.idea/.rakeTasks
+/.idea/7digital.iml
+/.idea/encodings.xml
+/.idea/misc.xml
+/.idea/modules.xml
+/.idea/vcs.xml
+/.idea/workspace.xml
+/.yardoc
+/doc
View
@@ -1,16 +0,0 @@
-require '../lib/sevendigital'
-
- api_client = Sevendigital::Client.new("sevendigital.yml", :cache=>VerySimpleCache.new)
-
- track = api_client.track.search("TEST CONTENT - TEST CONTENT", :page_size=>1).first
-
- puts "purchasing track #{track.title} by #{track.artist.appears_as} from #{track.release.title}"
- puts "the price is #{track.price.formatted_price}"
-
- user = api_client.user.authenticate("USERS_EMAIL", "USERS_PASSWORD")
-
- purchase_response = user.purchase!(track.release.id, track.id, track.price.value) if track.price.value
-
- locker_track = purchase_response.locker_releases.first.locker_tracks.first
-
- puts "you can now download the track here: #{locker_track.download_urls.first.url}"
@@ -0,0 +1,18 @@
+require '../lib/sevendigital'
+require './very_simple_cache'
+
+api_client = Sevendigital::Client.new("sevendigital.yml", :cache => VerySimpleCache.new)
+
+track = api_client.track.search("TEST CONTENT - TEST CONTENT", :page_size=>1).first
+
+
+user = api_client.user.authenticate("USERS_EMAIL", "USERS_PASSWORD")
+
+puts "purchasing track #{track.title} by #{track.artist.appears_as} from #{track.release.title}"
+puts "the price is #{track.price.formatted_price}"
+
+purchase_response = user.purchase!(track.release.id, track.id, track.price.value) if track.price.value
+
+locker_track = purchase_response.locker_releases.first.locker_tracks.first
+
+puts "you can now download the track here: #{locker_track.download_urls.first.url}"
@@ -14,25 +14,24 @@ def initialize(client, cache)
def call_api(api_request)
request_cache_key = create_request_uri(api_request)
- http_response = @cache.get(request_cache_key.to_s) if !api_request.requires_signature?
+ api_response = @cache.get(request_cache_key.to_s) if !api_request.requires_signature?
puts "ApiOperatorCached: Got from cache #{request_cache_key}" if @client.verbose? && http_response
puts "but the response is out of date" if @client.verbose? && http_response && response_out_of_date?(http_response)
- if (!http_response || response_out_of_date?(http_response)) then
- http_response = make_http_request(api_request)
- @cache.set(request_cache_key.to_s, http_response) if !api_request.requires_signature?
+ if (!api_response || response_out_of_date?(api_response)) then
+ api_response = digest_http_response(make_http_request(api_request))
+ @cache.set(request_cache_key.to_s, api_response) if !api_request.requires_signature?
end
- api_response = digest_http_response(http_response)
p api_response if @client.very_verbose?
api_response
end
- def response_out_of_date?(http_response, current_time=nil)
- return true if http_response.header.nil? || http_response.header["Date"].nil? || http_response.header["cache-control"].nil?
+ def response_out_of_date?(api_response, current_time=nil)
+ return true if api_response.headers.nil? || api_response.headers["Date"].nil? || api_response.headers["cache-control"].nil?
puts "cache headers present"
- return true if !(http_response.header["cache-control"] =~ /max-age=([0-9]+)/)
+ return true if !(api_response.headers["cache-control"] =~ /max-age=([0-9]+)/)
current_time ||= Time.now.utc
- response_time = Time.parse(http_response.header["Date"])
- max_age = /max-age=([0-9]+)/.match(http_response.header["cache-control"])[1].to_i
+ response_time = Time.parse(api_response.headers["Date"])
+ max_age = /max-age=([0-9]+)/.match(api_response.headers["cache-control"])[1].to_i
response_time + max_age < current_time
end
@@ -1,5 +1,6 @@
require "spec_helper"
require 'ostruct'
+require 'time'
describe "ApiOperatorCached" do
@@ -40,7 +41,7 @@
@cache.stub!(:get).with("key").and_return(http_response)
@cached_operator.stub!(:response_out_of_date?).with(http_response).and_return(false)
@cached_operator.should_not_receive(:make_http_request)
- @cached_operator.should_receive(:digest_http_response).with(http_response)
+ @cached_operator.should_not_receive(:digest_http_response).with(http_response)
@cached_operator.call_api(@stub_api_request)
end
@@ -74,13 +75,14 @@
it "should cache uncached api response if request was not signed" do
http_response = stub_http_response()
+ api_response = stub_api_response()
@cached_operator.stub(:create_request_uri).and_return("key")
@cache.stub!(:get).with("key").and_return(nil)
@cached_operator.stub!(:make_http_request).and_return(http_response)
- @cached_operator.stub!(:digest_http_response)
+ @cached_operator.stub!(:digest_http_response).with(http_response).and_return(api_response)
- @cache.should_receive(:set).with("key", http_response).and_return(nil)
+ @cache.should_receive(:set).with("key", api_response).and_return(nil)
@cached_operator.call_api(@stub_api_request)
end
@@ -90,7 +92,7 @@
max_age = 12*60*60
http_response = stub_http_response
- http_response.stub!(:header).and_return({
+ http_response.stub!(:headers).and_return({
"cache-control" => "private, max-age=#{max_age}",
"Date" => yesterday.httpdate
})
@@ -106,7 +108,7 @@
max_age = 48*60*60
http_response = stub_http_response
- http_response.stub!(:header).and_return({
+ http_response.stub!(:headers).and_return({
"cache-control" => "private, max-age=#{max_age}",
"Date" => yesterday.httpdate
})
@@ -118,10 +120,10 @@
it "response should be out of date if it is missing caching headers" do
http_response = stub_http_response
- http_response.stub!(:header).and_return({})
+ http_response.stub!(:headers).and_return({})
@cached_operator.response_out_of_date?(http_response, Time.now.utc).should == true
- http_response.stub!(:header).and_return({
+ http_response.stub!(:headers).and_return({
"Date" => Time.now.utc.httpdate
})
@cached_operator.response_out_of_date?(http_response, Time.now.utc).should == true
@@ -131,15 +133,15 @@
it "response should be out of date if no max-age has been specified" do
http_response = stub_http_response
- http_response.stub!(:header).and_return({"cache-control" => "no-cache", "Date" => Time.now.utc.httpdate})
+ http_response.stub!(:headers).and_return({"cache-control" => "no-cache", "Date" => Time.now.utc.httpdate})
@cached_operator.response_out_of_date?(http_response, Time.now.utc).should == true
end
it "response should be out of date if it is missing Date header" do
http_response = stub_http_response
- http_response.stub!(:header).and_return({"cache-control" => "private, max-age=#{2**30}"})
+ http_response.stub!(:headers).and_return({"cache-control" => "private, max-age=#{2**30}"})
@cached_operator.response_out_of_date?(http_response, Time.now.utc).should == true
end
View
@@ -13,15 +13,15 @@
=end
-require 'spec'
+require 'rspec'
require File.expand_path(
File.join(File.dirname(__FILE__), %w[.. lib sevendigital]))
require "peachy_spec_helper_patch"
Peachy.whine
-Spec::Runner.configure do |config|
+Rspec.configure do |config|
# == Mock Framework
#
# RSpec uses it's own mocking framework by default. If you prefer to
@@ -0,0 +1,49 @@
+<response status="ok" version="1.2">
+ <purchase id="123">
+ <purchaseDate>2009-08-10T11:00:00+01:00</purchaseDate>
+ <lockerReleases>
+ <lockerRelease>
+ <release id="1">
+ <title>Ill Communication</title>
+ <version></version>
+ <type>Album</type>
+ <artist id="357">
+ <name>Beastie Boys</name>
+ <appearsAs>Beastie Boys</appearsAs>
+ <url>http://www.7digital.com/artists/beastie-boys</url>
+ </artist>
+ <url>http://www.7digital.com/artists/beastie-boys</url>
+ <image>http://cdn.7static.com/static/img/sleeveart/00/004/915/0000491573_350.jpg</image>
+ </release>
+ <lockerTracks>
+ <lockerTrack>
+ <track id="456">
+ <title>Sabotage</title>
+ <version></version>
+ <artist id="0">
+ <name>Beastie Boys</name>
+ <appearsAs>Beastie Boys</appearsAs>
+ <url>http://www.7digital.com/artists/beastie-boys</url>
+ </artist>
+ <url>
+ http://www.7digital.com/artists/beastie-boys/ill-communication-remastered-version/06-Sabotage-(2009-Digital-Remaster)/
+ </url>
+ </track>
+ <remainingDownloads>3</remainingDownloads>
+ <purchaseDate>2009-08-10T11:00:00+01:00</purchaseDate>
+ <downloadUrls>
+ <downloadUrl>
+ <url>http://media3.7digital.com/media/user/downloadtrack?formatid=1&amp;oauth_consumer_key=YOUR_KEY_HERE&amp;oauth_nonce=3254252&amp;oauth_signature_method=HMAC-SHA1&amp;oauth_timestamp=1249379136&amp;oauth_token=123456&amp;shopid=34&amp;trackid=456&amp;oauth_signature=9XyVW0dUqn%2FbrZjEzzFVbXiOYGk%3D</url>
+ <format id="1">
+ <fileFormat>MP3</fileFormat>
+ <bitRate>320</bitRate>
+ <drmFree>true</drmFree>
+ </format>
+ </downloadUrl>
+ </downloadUrls>
+ </lockerTrack>
+ </lockerTracks>
+ </lockerRelease>
+ </lockerReleases>
+ </purchase>
+</response>

0 comments on commit 49b71c5

Please sign in to comment.