Skip to content

Commit

Permalink
fix caching to properly cache unsigned url
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Pickett committed Aug 1, 2009
1 parent ec9fa31 commit cd97682
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.swp
tags
1 change: 1 addition & 0 deletions lib/amazon_associate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "base64"
require "hmac-sha2"
require "digest/sha1"
require "digest/sha2"

require "amazon_associate/request"
Expand Down
2 changes: 1 addition & 1 deletion lib/amazon_associate/caching_strategy/filesystem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class << self

def cache(request, response)
path = self.cache_path
cached_filename = Digest::SHA1.hexdigest(response.request_url)
cached_filename = Digest::SHA1.hexdigest(request)
cached_folder = cached_filename[0..2]

FileUtils.mkdir_p(File.join(path, cached_folder, cached_folder))
Expand Down
5 changes: 3 additions & 2 deletions lib/amazon_associate/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ def self.send_request(opts)
end

response = Response.new(res.body, request_url)
response.unsigned_url = unsigned_url

if caching_enabled?
cache_response(unsigned_url, response, self.options[:caching_strategy])
Expand All @@ -258,7 +259,7 @@ def self.send_request(opts)
response
end

attr_accessor :request_url
attr_accessor :request_url, :unsigned_url

protected
def self.log(s)
Expand Down Expand Up @@ -293,7 +294,7 @@ def self.prepare_unsigned_url(opts)
qs << "&#{camelize(k.to_s)}=#{URI.encode(v.to_s)}"
}

"#{url}#{qs}"
@unsigned_url = "#{url}#{qs}"
end

def self.prepare_signed_url(opts)
Expand Down
4 changes: 2 additions & 2 deletions lib/amazon_associate/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module AmazonAssociate
# Response object returned after a REST call to Amazon service.
class Response

attr_accessor :request_url
attr_accessor :request_url, :unsigned_url
# XML input is in string format
def initialize(xml, request_url)
@doc = Hpricot(xml)
Expand Down Expand Up @@ -71,4 +71,4 @@ def total_pages
@total_pages
end
end
end
end
7 changes: 1 addition & 6 deletions test/amazon_associate/caching_strategy/filesystem_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class AmazonAssociate::CachingStrategy::FilesystemTest < Test::Unit::TestCase
get_cache_directory
get_valid_caching_options
@resp = AmazonAssociate::Request.item_lookup("0974514055")
@filename = Digest::SHA1.hexdigest(@resp.request_url)
@filename = Digest::SHA1.hexdigest(@resp.unsigned_url)
end

teardown do
Expand Down Expand Up @@ -122,11 +122,6 @@ class AmazonAssociate::CachingStrategy::FilesystemTest < Test::Unit::TestCase
do_request
end

should "return the same response as the original request" do
original = @resp.doc.to_s
do_request
assert_equal(original, @resp.doc.to_s)
end
end

context "sweeping cached requests" do
Expand Down
1 change: 0 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require "test/unit"
require "shoulda"
require "mocha"
require "fake_web"

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__) , '..', 'lib'))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__) , 'utilities'))
Expand Down

0 comments on commit cd97682

Please sign in to comment.