Module: TaobaoFu
- Defined in:
- lib/rest.rb,
lib/taobao_fu.rb,
lib/generators/taobao_fu/install_generator.rb
Defined Under Namespace
Modules: Generators, Rest
Constant Summary
collapse
- SANDBOX =
'http://gw.api.tbsandbox.com/router/rest?'
- PRODBOX =
'http://gw.api.taobao.com/router/rest?'
- USER_AGENT =
'why404-taobao_fu/1.0.0.beta5'
- REQUEST_TIMEOUT =
10
- API_VERSION =
2.0
- SIGN_ALGORITHM =
'md5'
- OUTPUT_FORMAT =
'json'
Class Method Summary
collapse
Class Method Details
.apply_settings ⇒ Object
35
36
37
38
39
|
# File 'lib/taobao_fu.rb', line 35
def apply_settings
@base_url = @settings['is_sandbox'] ? SANDBOX : PRODBOX
initialize_session if @settings['use_curl']
end
|
.delete(options = {}) ⇒ Object
73
74
|
# File 'lib/taobao_fu.rb', line 73
def delete(options = {})
end
|
.generate_query_string(params) ⇒ Object
94
95
96
97
98
99
|
# File 'lib/taobao_fu.rb', line 94
def generate_query_string(params)
params_array = params.sort_by { |k,v| k.to_s }
sign_token = generate_sign(params_array.flatten.join)
total_param = params_array.map { |key, value| key.to_s+"="+value.to_s } + ["sign=#{sign_token}"]
URI.escape(total_param.join("&"))
end
|
.generate_query_vars(params) ⇒ Object
89
90
91
92
|
# File 'lib/taobao_fu.rb', line 89
def generate_query_vars(params)
params[:sign] = generate_sign(params.sort_by { |k,v| k.to_s }.flatten.join)
params
end
|
.generate_sign(param_string) ⇒ Object
101
102
103
|
# File 'lib/taobao_fu.rb', line 101
def generate_sign(param_string)
Digest::MD5.hexdigest(@settings['secret_key'] + param_string + @settings['secret_key']).upcase
end
|
.get(options = {}) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/taobao_fu.rb', line 53
def get(options = {})
if @sess
@response = @sess.get(generate_query_string(sorted_params(options))).body
else
@response = TaobaoFu::Rest.get(@base_url, generate_query_vars(sorted_params(options)))
end
if options.fetch(:method).start_with?("taobao.wangwang")
parse_result_xml @response
else
parse_result_json @response
end
end
|
.initialize_session ⇒ Object
41
42
43
44
45
46
|
# File 'lib/taobao_fu.rb', line 41
def initialize_session
@sess = Patron::Session.new
@sess.base_url = @base_url
@sess.['User-Agent'] = USER_AGENT
@sess.timeout = REQUEST_TIMEOUT
end
|
.load(config_file) ⇒ Object
24
25
26
27
28
|
# File 'lib/taobao_fu.rb', line 24
def load(config_file)
@settings = YAML.load_file(config_file)
@settings = @settings[Rails.env] if defined? Rails.env
apply_settings
end
|
.parse_result_json(data) ⇒ Object
105
106
107
|
# File 'lib/taobao_fu.rb', line 105
def parse_result_json(data)
Crack::JSON.parse(data)
end
|
.parse_result_xml(data) ⇒ Object
109
110
111
|
# File 'lib/taobao_fu.rb', line 109
def parse_result_xml(data)
Crack::XML.parse(data)
end
|
.post(options = {}) ⇒ Object
67
68
|
# File 'lib/taobao_fu.rb', line 67
def post(options = {})
end
|
.settings=(settings) ⇒ Object
30
31
32
33
|
# File 'lib/taobao_fu.rb', line 30
def settings=(settings)
@settings = settings
apply_settings
end
|
.sorted_params(options) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/taobao_fu.rb', line 76
def sorted_params(options)
output_format = OUTPUT_FORMAT
output_format = 'xml' if options.fetch(:method).start_with?("taobao.wangwang")
{
:app_key => @settings['app_key'],
:session => @settings['session'],
:format => output_format,
:v => API_VERSION,
:sign_method => SIGN_ALGORITHM,
:timestamp => Time.now.strftime("%Y-%m-%d %H:%M:%S")
}.merge!(options)
end
|
.switch_to(sandbox_or_prodbox) ⇒ Object
48
49
50
51
|
# File 'lib/taobao_fu.rb', line 48
def switch_to(sandbox_or_prodbox)
@base_url = sandbox_or_prodbox
@sess.base_url = @base_url if @sess
end
|
.update(options = {}) ⇒ Object
70
71
|
# File 'lib/taobao_fu.rb', line 70
def update(options = {})
end
|