2008-12-06 23:41:28 -05:00
|
|
|
require 'rubygems'
|
2010-06-08 17:15:55 -04:00
|
|
|
require 'active_support'
|
2020-12-28 06:55:20 -05:00
|
|
|
require 'active_support/core_ext/hash'
|
|
|
|
require 'active_support/core_ext/string'
|
2008-12-06 23:41:28 -05:00
|
|
|
|
2008-07-28 12:45:06 -04:00
|
|
|
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
|
|
require File.join(dir, 'httparty')
|
|
|
|
require 'pp'
|
2015-04-18 14:38:40 +02:00
|
|
|
config = YAML.load(File.read(File.join(ENV['HOME'], '.aaws')))
|
2008-07-28 12:45:06 -04:00
|
|
|
|
|
|
|
module AAWS
|
|
|
|
class Book
|
|
|
|
include HTTParty
|
|
|
|
base_uri 'http://ecs.amazonaws.com'
|
2014-05-09 15:56:01 -04:00
|
|
|
default_params Service: 'AWSECommerceService', Operation: 'ItemSearch', SearchIndex: 'Books'
|
2013-04-18 21:09:21 +09:00
|
|
|
|
2008-07-28 12:45:06 -04:00
|
|
|
def initialize(key)
|
2020-12-28 06:55:20 -05:00
|
|
|
@auth = { AWSAccessKeyId: key }
|
2008-07-28 12:45:06 -04:00
|
|
|
end
|
2013-04-18 21:09:21 +09:00
|
|
|
|
2015-04-18 12:27:50 +02:00
|
|
|
def search(options = {})
|
2008-07-28 12:45:06 -04:00
|
|
|
raise ArgumentError, 'You must search for something' if options[:query].blank?
|
2013-04-18 21:09:21 +09:00
|
|
|
|
2008-07-28 17:03:14 -04:00
|
|
|
# amazon uses nasty camelized query params
|
2020-12-28 06:55:20 -05:00
|
|
|
options[:query] = options[:query]
|
|
|
|
.reverse_merge(@auth)
|
|
|
|
.transform_keys { |k| k.to_s.camelize }
|
2013-04-18 21:09:21 +09:00
|
|
|
|
2008-07-28 12:58:40 -04:00
|
|
|
# make a request and return the items (NOTE: this doesn't handle errors at this point)
|
2008-07-28 12:45:06 -04:00
|
|
|
self.class.get('/onca/xml', options)['ItemSearchResponse']['Items']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
aaws = AAWS::Book.new(config[:access_key])
|
2016-11-08 15:20:36 +01:00
|
|
|
pp aaws.search(query: { title: 'Ruby On Rails' })
|