module OpenTaobao
Constants
- API_VERSION
- REQUEST_TIMEOUT
- USER_AGENT
- VERSION
Attributes
Public Class Methods
check config
raise exception if config key missed in YAML file
# File lib/open_taobao/open_taobao.rb, line 57 def check_config list = [] %w(app_key secret_key endpoint).map do |k| list << k unless config.has_key? k end raise "[#{list.join(', ')}] not included in your yaml file." unless list.empty? end
check config and export all setting to ENV
# File lib/open_taobao/open_taobao.rb, line 49 def check_config_and_export_to_env check_config export_config_to_env end
setting ENV variables from config
ENV variables:
TAOBAO_API_KEY -> config['app_key'] TAOBAO_SECRET_KEY -> config['secret_key'] TAOBAO_ENDPOINT -> config['endpoint'] TAOBAOKE_PID -> config['pid']
# File lib/open_taobao/open_taobao.rb, line 74 def export_config_to_env ENV['TAOBAO_API_KEY'] = config['app_key'] ENV['TAOBAO_SECRET_KEY'] = config['secret_key'] ENV['TAOBAO_ENDPOINT'] = config['endpoint'] ENV['TAOBAOKE_PID'] = config['pid'] # for compatible with v0.0.3 end
Merge custom parameters with TAOBAO system parameters.
System paramters below will be merged.
timestamp v format sign_method app_key
Current Taobao API Version is ‘2.0’. format
should be json. Only sign_method
MD5 is supported so far.
# File lib/open_taobao/open_taobao.rb, line 121 def full_options(params) { :timestamp => Time.now.strftime("%F %T"), :v => API_VERSION, :format => :json, :sign_method => :md5, :app_key => config['app_key'] }.merge params end
Request by get method and return result in JSON format
# File lib/open_taobao/open_taobao.rb, line 153 def get(params) path = query_string(params) parse_result session.get(path).body end
Request by get method and return result in JSON format Raise OpenTaobao::Error
if returned with error_response
# File lib/open_taobao/open_taobao.rb, line 160 def get!(params) response = get params raise Error.new(MultiJson.encode response['error_response']) if response.has_key?('error_response') response end
Initialize http sesison
# File lib/open_taobao/open_taobao.rb, line 82 def initialize_session @session = Faraday.new :url => config['endpoint'] do |builder| begin require 'patron' builder.adapter :patron rescue LoadError builder.adapter :net_http end end end
Load a yml config, and initialize http session yml config file content should be:
app_key: "YOUR APP KEY" secret_key: "YOUR SECRET KEY" endpoint: "TAOBAO GATEWAY API URL"
# File lib/open_taobao/open_taobao.rb, line 41 def load(config_file) @config = YAML.load_file(config_file) @config = config[Rails.env] if defined? Rails check_config_and_export_to_env initialize_session end
Return a parsed JSON object.
# File lib/open_taobao/open_taobao.rb, line 148 def parse_result(data) MultiJson.decode(data) end
Request by post method and return result in JSON format
# File lib/open_taobao/open_taobao.rb, line 167 def post(params) parse_result session.post('', query_hash(params).to_query).body end
Request by post method and return result in JSON format Raise OpenTaobao::Error
if returned with error_response
# File lib/open_taobao/open_taobao.rb, line 173 def post!(params) response = post params raise Error.new(MultiJson.encode response['error_response']) if response.has_key?('error_response') response end
# File lib/open_taobao/open_taobao.rb, line 131 def query_hash(params) params = full_options params params[:sign] = sign params params end
Retrun query string with signature.
# File lib/open_taobao/open_taobao.rb, line 138 def query_string(params) "?" + query_hash(params).to_query end
Return request signature with MD5 signature method
# File lib/open_taobao/open_taobao.rb, line 94 def sign(params) Digest::MD5::hexdigest(wrap_with_secret sorted_option_string(params)).upcase end
Return sorted request parameter by request key
# File lib/open_taobao/open_taobao.rb, line 104 def sorted_option_string(options) options.map {|k, v| "#{k}#{v}" }.sort.join end
Return full url with signature.
# File lib/open_taobao/open_taobao.rb, line 143 def url(params) "%s%s" % [config['endpoint'], query_string(params)] end
wrapped with secret_key
# File lib/open_taobao/open_taobao.rb, line 99 def wrap_with_secret(s) "#{config['secret_key']}#{s}#{config['secret_key']}" end