lib/amazon/ecs.rb in amazon-ecs-0.5.0 vs lib/amazon/ecs.rb in amazon-ecs-0.5.1
- old
+ new
@@ -1,5 +1,28 @@
+#--
+# Copyright (c) 2006 Herryanto Siatono, Pluit Solutions
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#++
+
require 'net/http'
require 'hpricot'
require 'cgi'
module Amazon
@@ -9,15 +32,16 @@
class Ecs
SERVICE_URLS = {:us => 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService',
:uk => 'http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService',
:ca => 'http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService',
:de => 'http://webservices.amazon.de/onca/xml?Service=AWSECommerceService',
- :jp => 'http://webservices.amazon.de/onca/xml?Service=AWSECommerceService',
- :fr => 'http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService'
+ :jp => 'http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService',
+ :fr => 'http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService'
}
- @@debug, @@options = nil
+ @@options = {}
+ @@debug = false
# Default search options
def self.options
@@options
end
@@ -33,12 +57,17 @@
end
# Set debug flag to true or false.
def self.debug=(dbg)
@@debug = dbg
- end
+ end
+ def self.configure(&proc)
+ raise ArgumentError, "Block is required." unless block_given?
+ yield @@options
+ end
+
# Search amazon items with search terms. Default search index option is 'Books'.
# For other search type other than keywords, please specify :type => [search type param name].
def self.item_search(terms, opts = {})
opts = self.options.merge(opts) if self.options
opts[:operation] = 'ItemSearch'
@@ -58,16 +87,13 @@
def self.item_lookup(item_id, opts = {})
opts = self.options.merge(opts) if self.options
opts[:operation] = 'ItemLookup'
opts[:item_id] = item_id
- # not allowed in item_lookup
- opts.delete(:search_index)
-
self.send_request(opts)
- end
-
+ end
+
# Generic send request to ECS REST service. You have to specify the :operation parameter.
def self.send_request(opts)
request_url = prepare_url(opts)
log "Request URL: #{request_url}"
@@ -144,12 +170,14 @@
end
protected
def self.log(s)
return unless self.debug
- if RAILS_DEFAULT_LOGGER
+ if defined? RAILS_DEFAULT_LOGGER
RAILS_DEFAULT_LOGGER.error(s)
+ elsif defined? LOGGER
+ LOGGER.error(s)
else
puts s
end
end
@@ -188,9 +216,19 @@
# Find Hpricot::Elements matching the given path. Example: element/"author".
def /(path)
elements = @element/path
return nil if elements.size == 0
+ elements
+ end
+
+ # Find Hpricot::Elements matching the given path, and convert to Amazon::Element.
+ # Returns an array Amazon::Elements if more than Hpricot::Elements size is greater than 1.
+ def search_and_convert(path)
+ elements = self./(path)
+ return unless elements
+ elements = elements.map{|element| Element.new(element)}
+ return elements.first if elements.size == 1
elements
end
# Get the text value of the given path, leave empty to retrieve current element value.
def get(path='')
\ No newline at end of file