Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Remove whitespace [Gun.io WhitespaceBot]
  • Loading branch information
Gun.io Whitespace Robot committed Oct 28, 2011
1 parent 36a9259 commit 4092f02
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 186 deletions.
2 changes: 1 addition & 1 deletion .autotest
@@ -1,6 +1,6 @@
Autotest.add_hook :initialize do |at|
at.clear_mappings

at.add_mapping(%r{^test/.*_test\.rb$}) {|f, _| [f] }
at.add_mapping(%r{^lib/amazon/(.*)\.rb$}) {|_, m| ["test/#{m[1]}_test.rb"] }
at.add_mapping(%r{^test/(test_helper)\.rb$}) { at.files_matching %r{^test/.*_test\.rb$} }
Expand Down
42 changes: 21 additions & 21 deletions README.rdoc
@@ -1,33 +1,33 @@
== amazon-associate

Generic Amazon E-commerce REST API using Hpricot with configurable
default options and method call options. Uses Response and
Generic Amazon E-commerce REST API using Hpricot with configurable
default options and method call options. Uses Response and
Element wrapper classes for easy access to REST XML output. It supports ECS 4.0.

It is generic, so you can easily extend <tt>AmazonAssociate::Request</tt> to support
It is generic, so you can easily extend <tt>AmazonAssociate::Request</tt> to support
other not implemented REST operations; and it is also generic because it just wraps around
Hpricot element object, instead of providing one-to-one object/attributes to XML elements map.

If in the future, there is a change in REST XML output structure,
no changes will be required on <tt>amazon-ecs</tt> library,
If in the future, there is a change in REST XML output structure,
no changes will be required on <tt>amazon-ecs</tt> library,
instead you just need to change the element path.

NOTE: You must now specify a secret key to support request signing as
NOTE: You must now specify a secret key to support request signing as
required by Amazon.

Version: 0.6.1

== WANTS
* instance based refactoring (singletons are not ideal here)

== INSTALLATION

$ gem install dpickett-amazon_associate

== EXAMPLE

require 'amazon_associate'

# set the default options; options will be camelized and converted to REST request parameters.
AmazonAssociate::Request.configure do |options|
options[:aWS_access_key_id] = [your developer token]
Expand All @@ -54,32 +54,32 @@ Version: 0.6.1
# or return AmazonAssociate::Element instance
atts = item.search_and_convert('itemattributes')
atts.get('title')

# return first author or a string array of authors
atts.get('author') # 'Author 1'
atts.get_array('author') # ['Author 1', 'Author 2', ...]

# return an hash of children text values with the element names as the keys
item.get_hash('smallimage') # {:url => ..., :width => ..., :height => ...}

# note that '/' returns Hpricot::Elements array object, nil if not found
reviews = item/'editorialreview'

# traverse through Hpricot elements
reviews.each do |review|
# Getting hash value out of Hpricot element
AmazonAssociate::Element.get_hash(review) # [:source => ..., :content ==> ...]

# Or to get unescaped HTML values
AmazonAssociate::Element.get_unescaped(review, 'source')
AmazonAssociate::Element.get_unescaped(review, 'content')

# Or this way
el = AmazonAssociate::Element.new(review)
el.get_unescaped('source')
el.get_unescaped('content')
end

# returns AmazonAssociate::Element instead of string
item.search_and_convert('itemattributes').
end
Expand All @@ -93,20 +93,20 @@ http://www.awszone.com/scratchpads/aws/ecs.us/index.aws
== CACHING

Filesystem caching is now available.
AmazonAssociate::Request.configure do |options|

AmazonAssociate::Request.configure do |options|
options[:aWS_access_key_id] = [your developer token]
options[:scret_key] = [your secret key]
options[:caching_strategy] = :filesystem
options[:caching_options] = {
:disk_quota => 200,
:cache_path => <path where you want to store requests>,
:disk_quota => 200,
:cache_path => <path where you want to store requests>,
:sweep_frequency => 4
}
end

The above command will cache up to 200MB of requests. It will purge the cache every 4 hours or when the disk quota has been exceeded.

Every request will be stored in the cache path. On every request, AmazonAssociate::Request will check for the presence of the cached file before querying Amazon directly.

== LINKS
Expand Down
2 changes: 1 addition & 1 deletion VERSION.yml
@@ -1,4 +1,4 @@
---
---
:patch: 0
:major: 0
:minor: 7
10 changes: 5 additions & 5 deletions lib/amazon_associate/cache_factory.rb
Expand Up @@ -3,24 +3,24 @@ class CacheFactory
def self.cache(request, response, strategy)
strategy_class_hash[strategy].cache(request, response)
end

def self.initialize_options(options)
#check for a valid caching strategy
unless self.strategy_class_hash.keys.include?(options[:caching_strategy])
raise AmazonAssociate::ConfigurationError, "Invalid caching strategy"
raise AmazonAssociate::ConfigurationError, "Invalid caching strategy"
end

strategy_class_hash[options[:caching_strategy]].initialize_options(options)
end

def self.get(request, strategy)
strategy_class_hash[strategy].get(request)
end

def self.sweep(strategy)
strategy_class_hash[strategy].sweep
end

private
def self.strategy_class_hash
{
Expand Down
6 changes: 3 additions & 3 deletions lib/amazon_associate/caching_strategy/base.rb
Expand Up @@ -5,15 +5,15 @@ class Base
def self.cache(request, response)
raise "This method must be overwritten by a caching strategy"
end

def self.initialize_options(options)
raise "This method must be overwritten by a caching strategy"
end

def self.get(request)
raise "This method must be overwritten by a caching strategy"
end

def self.sweep
raise "This method must be overwritten by a caching strategy"
end
Expand Down
40 changes: 20 additions & 20 deletions lib/amazon_associate/caching_strategy/filesystem.rb
Expand Up @@ -6,10 +6,10 @@ module CachingStrategy
class Filesystem < AmazonAssociate::CachingStrategy::Base
#disk quota in megabytes
DEFAULT_DISK_QUOTA = 200

#frequency of sweeping in hours
DEFAULT_SWEEP_FREQUENCY = 2

class << self
attr_accessor :cache_path
attr_accessor :disk_quota
Expand All @@ -19,14 +19,14 @@ def cache(request, response)
path = self.cache_path
cached_filename = Digest::SHA1.hexdigest(request)
cached_folder = cached_filename[0..2]

FileUtils.mkdir_p(File.join(path, cached_folder, cached_folder))

cached_file = File.open(File.join(path, cached_folder, cached_filename), "w")
cached_file.puts response.doc.to_s
cached_file.close
end

def get(request)
path = self.cache_path
cached_filename = Digest::SHA1.hexdigest(request)
Expand All @@ -37,63 +37,63 @@ def get(request)
nil
end
end
def initialize_options(options)

def initialize_options(options)
#check for required options
if options[:caching_options].nil? || options[:caching_options][:cache_path].nil?
raise AmazonAssociate::ConfigurationError, "You must specify caching options for filesystem caching: :cache_path is required"
end

#default disk quota to 200MB
Filesystem.disk_quota = options[:caching_options][:disk_quota] || DEFAULT_DISK_QUOTA

Filesystem.sweep_frequency = options[:caching_options][:sweep_frequency] || DEFAULT_SWEEP_FREQUENCY

Filesystem.cache_path = options[:caching_options][:cache_path]

if Filesystem.cache_path.nil? || !File.directory?(Filesystem.cache_path)
raise AmazonAssociate::ConfigurationError, "You must specify a cache path for filesystem caching"
end
return options
end

def sweep
perform_sweep if must_sweep?
end

private
def perform_sweep
FileUtils.rm_rf(Dir.glob("#{Filesystem.cache_path}/*"))

timestamp_sweep_performance
end

def timestamp_sweep_performance
#remove the timestamp
FileUtils.rm_rf(timestamp_filename)

#create a new one its place
timestamp = File.open(timestamp_filename, "w")
timestamp.puts(Time.now)
timestamp.close
end

def must_sweep?
sweep_time_expired? || disk_quota_exceeded?
end

def sweep_time_expired?
FileTest.exists?(timestamp_filename) && Time.parse(File.read(timestamp_filename).chomp) < Time.now - (sweep_frequency * 3600)
end

def disk_quota_exceeded?
cache_size > Filesystem.disk_quota
end

def timestamp_filename
File.join(Filesystem.cache_path, ".amz_timestamp")
end

def cache_size
size = 0
Find.find(Filesystem.cache_path) do|f|
Expand Down
8 changes: 4 additions & 4 deletions lib/amazon_associate/element.rb
Expand Up @@ -6,7 +6,7 @@ def initialize(element)
@element = element
end

# Returns Hpricot::Elments object
# Returns Hpricot::Elments object
def elem
@element
end
Expand Down Expand Up @@ -56,7 +56,7 @@ def self.get(element, path="")
result
end

# Similar to #get_unescaped, except an element object must be passed-in.
# Similar to #get_unescaped, except an element object must be passed-in.
def self.get_unescaped(element, path="")
result = get(element, path)
CGI::unescapeHTML(result) if result
Expand All @@ -65,7 +65,7 @@ def self.get_unescaped(element, path="")
# Similar to #get_array, except an element object must be passed-in.
def self.get_array(element, path="")
return unless element

result = element/path
if (result.is_a? Hpricot::Elements) || (result.is_a? Array)
parsed_result = []
Expand All @@ -88,7 +88,7 @@ def self.get_hash(element, path="")
result = result.children
result.each do |item|
hash[item.name.to_sym] = item.inner_html
end
end
hash
end
end
Expand Down

0 comments on commit 4092f02

Please sign in to comment.