Module: JingdongFu

Defined in:
lib/rest.rb,
lib/jingdong_fu.rb,
lib/generators/jingdong_fu/install_generator.rb

Defined Under Namespace

Modules: Generators, Rest

Constant Summary collapse

SANDBOX =
'http://gw.api.sandbox.360buy.com/routerjson?'
PRODBOX =
'http://gw.api.360buy.com/routerjson?'
USER_AGENT =
'jingdong_fu/1.1'
REQUEST_TIMEOUT =
10
API_VERSION =
2.0
SIGN_ALGORITHM =
'md5'
OUTPUT_FORMAT =
'json'

Class Method Summary collapse

Class Method Details

.apply_settingsObject



36
37
38
# File 'lib/jingdong_fu.rb', line 36

def apply_settings
  @base_url = @settings['is_sandbox'] ? SANDBOX : PRODBOX
end

.delete(options = {}) ⇒ Object



55
56
# File 'lib/jingdong_fu.rb', line 55

def delete(options = {})
end

.generate_query_vars(params) ⇒ Object



73
74
75
76
# File 'lib/jingdong_fu.rb', line 73

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



78
79
80
# File 'lib/jingdong_fu.rb', line 78

def generate_sign(param_string)
  Digest::MD5.hexdigest(@settings['secret_key'] + param_string + @settings['secret_key']).upcase
end

.get(options = {}) ⇒ Object



44
45
46
47
# File 'lib/jingdong_fu.rb', line 44

def get(options = {})
  @response = TaobaoFu::Rest.get(@base_url, generate_query_vars(sorted_params(options)))
  parse_result @response
end

.load(config_file) ⇒ Object



25
26
27
28
29
# File 'lib/jingdong_fu.rb', line 25

def load(config_file)
  @settings = YAML.load_file(config_file)
  @settings = @settings[Rails.env] if defined? Rails.env
  apply_settings
end

.parse_result(data) ⇒ Object



82
83
84
# File 'lib/jingdong_fu.rb', line 82

def parse_result(data)
  Crack::JSON.parse(data)
end

.post(options = {}) ⇒ Object



49
50
# File 'lib/jingdong_fu.rb', line 49

def post(options = {})
end

.settings=(settings) ⇒ Object



31
32
33
34
# File 'lib/jingdong_fu.rb', line 31

def settings=(settings)
  @settings = settings
  apply_settings
end

.sorted_params(params) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/jingdong_fu.rb', line 58

def sorted_params(params)
  method = params.delete(:method)
  param_json = Hash[params.sort_by { |k,v| k.to_s }.map { |k, v| [k.to_s, v.to_s] }].to_json
  {
    :app_key             => @settings['app_key'],
    :access_token        => @settings['access_token'],
    :format              => OUTPUT_FORMAT,
    :v                   => API_VERSION,
    :sign_method         => SIGN_ALGORITHM,
    :timestamp           => Time.now.strftime("%Y-%m-%d %H:%M:%S"),
    :method              => method,
    :'360buy_param_json' => param_json
  }
end

.switch_to(sandbox_or_prodbox) ⇒ Object



40
41
42
# File 'lib/jingdong_fu.rb', line 40

def switch_to(sandbox_or_prodbox)
  @base_url = sandbox_or_prodbox
end

.update(options = {}) ⇒ Object



52
53
# File 'lib/jingdong_fu.rb', line 52

def update(options = {})
end