module OpenJd

Constants

API_VERSION
REQUEST_TIMEOUT
USER_AGENT
VERSION

Attributes

config[RW]
session[RW]

Public Class Methods

check_config() click to toggle source

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_to_env() click to toggle source

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
export_config_to_env() click to toggle source

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
full_options(params) click to toggle source

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
get(params) click to toggle source

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
get!(params) click to toggle source

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_session() click to toggle source

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
load(config_file) click to toggle source

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
parse_result(data) click to toggle source

Return a parsed JSON object.

# File lib/open_jd/open_jd.rb, line 132
def parse_result(data)
  MultiJson.decode(data)
end
post(params) click to toggle source

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
post!(params) click to toggle source

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
query_hash(params) click to toggle source
# File lib/open_jd/open_jd.rb, line 115
def query_hash(params)
  params = full_options params
  params[:sign] = sign params
  params
end
query_string(params) click to toggle source

Return query string with signature.

# File lib/open_jd/open_jd.rb, line 122
def query_string(params)
  '?' + query_hash(params).to_query
end
sign(params) click to toggle source

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
sorted_option_string(options) click to toggle source

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
url(params) click to toggle source

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
wrap_with_secret(s) click to toggle source

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