module OpenJd
Constants
- API_VERSION
- PRODUCT_LINK
- REQUEST_TIMEOUT
- USER_AGENT
- VERSION
Attributes
Public Class Methods
check config
raise exception if config key missed in YAML file
# File lib/open_jd/open_jd.rb, line 39 def check_config list = [] %w(app_key secret_key endpoint).map do |k| list << k unless config.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_jd/open_jd.rb, line 31 def check_config_and_export_to_env check_config export_config_to_env end
setting ENV variables from config
ENV variables:
JD_API_KEY -> config['app_key'] JD_SECRET_KEY -> config['secret_key'] JD_ENDPOINT -> config['endpoint']
# File lib/open_jd/open_jd.rb, line 55 def export_config_to_env ENV['JD_API_KEY'] = config['app_key'] ENV['JD_SECRET_KEY'] = config['secret_key'] ENV['JD_ENDPOINT'] = config['endpoint'] end
Merge custom parameters with JD system parameters.
System paramters below will be merged.
timestamp v format sign_method app_key method params_key
Current JD API Version is ‘2.0’. format
should be json. Only sign_method
MD5 is supported so far.
# File lib/open_jd/open_jd.rb, line 103 def full_options(params) { timestamp: Time.zone.now.strftime('%F %T'), v: API_VERSION, format: :json, sign_method: :md5, app_key: config['app_key'], method: params[:method], '360buy_param_json': params[:fields].to_json.to_s } end
Request by get method and return result in JSON format
# File lib/open_jd/open_jd.rb, line 137 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_jd/open_jd.rb, line 144 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_jd/open_jd.rb, line 62 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
# File lib/open_jd/open_jd.rb, line 12 def jd_link(id) "#{PRODUCT_LINK}/#{id}.html" 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_jd/open_jd.rb, line 23 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_jd/open_jd.rb, line 132 def parse_result(data) MultiJson.decode(data) end
Request by post method and return result in JSON format
# File lib/open_jd/open_jd.rb, line 151 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_jd/open_jd.rb, line 157 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_jd/open_jd.rb, line 115 def query_hash(params) params = full_options params params[:sign] = sign params params end
Return query string with signature.
# File lib/open_jd/open_jd.rb, line 122 def query_string(params) '?' + query_hash(params).to_query end
Return request signature with MD5 signature method
# File lib/open_jd/open_jd.rb, line 74 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_jd/open_jd.rb, line 84 def sorted_option_string(options) options.map {|k, v| "#{k}#{v}" }.sort.join end
Return full url with signature.
# File lib/open_jd/open_jd.rb, line 127 def url(params) format('%s%s', config['endpoint'], query_string(params)) end
wrapped with secret_key
# File lib/open_jd/open_jd.rb, line 79 def wrap_with_secret(s) "#{config['secret_key']}#{s}#{config['secret_key']}" end