1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00
httparty/examples/aaws.rb
Adrián Mugnolo cba87407e5 Fix Amazon search example
It moves the auth param to an instance variable. Also, it fixes how Active
Support core extensions are loaded.

https://guides.rubyonrails.org/active_support_core_extensions.html#cherry-picking-a-definition
2020-12-28 06:55:20 -05:00

36 lines
1.1 KiB
Ruby

require 'rubygems'
require 'active_support'
require 'active_support/core_ext/hash'
require 'active_support/core_ext/string'
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'httparty')
require 'pp'
config = YAML.load(File.read(File.join(ENV['HOME'], '.aaws')))
module AAWS
class Book
include HTTParty
base_uri 'http://ecs.amazonaws.com'
default_params Service: 'AWSECommerceService', Operation: 'ItemSearch', SearchIndex: 'Books'
def initialize(key)
@auth = { AWSAccessKeyId: key }
end
def search(options = {})
raise ArgumentError, 'You must search for something' if options[:query].blank?
# amazon uses nasty camelized query params
options[:query] = options[:query]
.reverse_merge(@auth)
.transform_keys { |k| k.to_s.camelize }
# make a request and return the items (NOTE: this doesn't handle errors at this point)
self.class.get('/onca/xml', options)['ItemSearchResponse']['Items']
end
end
end
aaws = AAWS::Book.new(config[:access_key])
pp aaws.search(query: { title: 'Ruby On Rails' })