Last active
November 19, 2015 06:43
-
-
Save koki-h/4d86cd1752e4bfbda86f to your computer and use it in GitHub Desktop.
MWSのサンプルコードをオブジェクト指向っぽくしてみた。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- encoding: utf-8 -*- | |
| # Original code is http://aws.typepad.com/jp_mws/2012/12/amazon-mws-ruby-sample.html | |
| require 'uri' | |
| require 'time' | |
| require 'openssl' | |
| require 'base64' | |
| require "net/https" | |
| require "pp" | |
| require 'active_support' | |
| require 'active_support/core_ext' | |
| module MWS | |
| class Base | |
| METHOD="GET" | |
| ENDPOINT_SCHEME = 'https://' | |
| ENDPOINT_URI = '/Products/2011-10-01' | |
| def initialize(config) | |
| @c = self.class | |
| @params = { | |
| "AWSAccessKeyId" => config[:access_key_id], | |
| "MarketplaceId" => @c::MARKET_PLACE_ID, | |
| "SellerId" => config[:seller_id], | |
| "SignatureMethod" => "HmacSHA256", | |
| "SignatureVersion"=> "2", | |
| "Version" => "2011-10-01", | |
| } | |
| @private_key = config[:private_key] | |
| end | |
| def ListMatchingProducts(query, query_context_text_id) | |
| params = { | |
| "Action" => "ListMatchingProducts", | |
| "Query" => query, | |
| "QueryContextId" => query_context_text_id, | |
| } | |
| self.request(params) | |
| end | |
| def request(params) | |
| params["Timestamp"] = Time.now.utc.iso8601 | |
| req_params = @params.merge(params) | |
| #Sorting parameters - パラメータのソート | |
| values = req_params.keys.sort.collect {|key| [url_encode(key), url_encode(req_params[key].to_s)].join("=") } | |
| param=values.join("&") | |
| #Creating Signature String - 電子署名の作成 | |
| signtemp = METHOD+"\n"+@c::ENDPOINT_HOST+"\n"+ENDPOINT_URI+"\n"+param | |
| signature_raw = Base64.encode64(OpenSSL::HMAC.digest('sha256',@private_key,signtemp)).delete("\n") | |
| signature=URI.escape(signature_raw,Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) | |
| param+="&Signature="+signature | |
| #Creating URL - URLの作成 | |
| url=ENDPOINT_SCHEME+@c::ENDPOINT_HOST+ENDPOINT_URI+"?"+param | |
| puts url | |
| uri=URI.parse(url) | |
| http = Net::HTTP.new(uri.host, uri.port) | |
| http.use_ssl = true | |
| http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
| #performing HTTP access - HTTPアクセスを実行 | |
| request = Net::HTTP::Get.new(uri.request_uri) | |
| response = http.request(request) | |
| #output results - 結果を出力 | |
| Hash.from_xml(response.body) | |
| end | |
| def url_encode(string) | |
| return URI.escape(string,Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) | |
| end | |
| end | |
| class US < Base | |
| ENDPOINT_HOST = 'mws.amazonservices.com' #Endpoint to US MWS | |
| MARKET_PLACE_ID = 'ATVPDKIKX0DER'; #Markeplace is US | |
| end | |
| class JP < Base | |
| ENDPOINT_HOST ='mws.amazonservices.jp' #Endpoint to JP MWS | |
| MARKET_PLACE_ID ='A1VC38T7YXB528'; #Markeplace is JP | |
| end | |
| end | |
| if $0 == __FILE__ | |
| #####################CONFIG-各種設定項目##################### | |
| ACCESS_KEY_ID='************' | |
| PRIVATE_KEY='************' | |
| SELLER_ID='************' | |
| #####################CONFIG END################## | |
| #####################INPUT-入力項目####################### | |
| QUERY="red october" | |
| QUERYCONTEXTID="Books" | |
| #####################INPUT END################### | |
| config = { | |
| :access_key_id => ACCESS_KEY_ID, | |
| :private_key => PRIVATE_KEY, | |
| :seller_id => SELLER_ID, | |
| } | |
| mws = MWS::US.new(config) | |
| puts mws.ListMatchingProducts(QUERY,QUERYCONTEXTID) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment