Module: OpenJd

Defined in:
lib/open_jd/open_jd.rb,
lib/open_jd/railtie.rb,
lib/open_jd/version.rb,
lib/generators/open_jd/install_generator.rb

Defined Under Namespace

Modules: Generators Classes: Error, Railtie

Constant Summary collapse

REQUEST_TIMEOUT =
10
API_VERSION =
'2.0'
USER_AGENT =
"open_jd-v#{VERSION}"
'http://item.jd.com'
VERSION =
'0.0.4'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



10
11
12
# File 'lib/open_jd/open_jd.rb', line 10

def config
  @config
end

.sessionObject

Returns the value of attribute session.



10
11
12
# File 'lib/open_jd/open_jd.rb', line 10

def session
  @session
end

Class Method Details

.check_configObject

check config

raise exception if config key missed in YAML file



39
40
41
42
43
44
45
46
# 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_envObject

check config and export all setting to ENV



31
32
33
34
# 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_envObject

setting ENV variables from config

ENV variables:

JD_API_KEY    -> config['app_key']
JD_SECRET_KEY -> config['secret_key']
JD_ENDPOINT   -> config['endpoint']


55
56
57
58
59
# 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) ⇒ Object

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.



103
104
105
106
107
108
109
110
111
112
113
# 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) ⇒ Object

Request by get method and return result in JSON format



137
138
139
140
# 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) ⇒ Object

Request by get method and return result in JSON format Raise OpenTaobao::Error if returned with error_response

Raises:



144
145
146
147
148
# 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_sessionObject

Initialize http sesison



62
63
64
65
66
67
68
69
70
71
# 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


12
13
14
# File 'lib/open_jd/open_jd.rb', line 12

def jd_link(id)
  "#{PRODUCT_LINK}/#{id}.html"
end

.load(config_file) ⇒ Object

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"


23
24
25
26
27
28
# 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) ⇒ Object

Return a parsed JSON object.



132
133
134
# File 'lib/open_jd/open_jd.rb', line 132

def parse_result(data)
  MultiJson.decode(data)
end

.post(params) ⇒ Object

Request by post method and return result in JSON format



151
152
153
# 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) ⇒ Object

Request by post method and return result in JSON format Raise OpenTaobao::Error if returned with error_response

Raises:



157
158
159
160
161
# 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) ⇒ Object



115
116
117
118
119
# 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) ⇒ Object

Return query string with signature.



122
123
124
# File 'lib/open_jd/open_jd.rb', line 122

def query_string(params)
  '?' + query_hash(params).to_query
end

.sign(params) ⇒ Object

Return request signature with MD5 signature method



74
75
76
# 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) ⇒ Object

Return sorted request parameter by request key



84
85
86
# 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) ⇒ Object

Return full url with signature.



127
128
129
# 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) ⇒ Object

wrapped with secret_key



79
80
81
# File 'lib/open_jd/open_jd.rb', line 79

def wrap_with_secret(s)
  "#{config['secret_key']}#{s}#{config['secret_key']}"
end