Class: TaobaoSDK::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/taobaoSDK/session.rb

Constant Summary collapse

REQUEST_TIMEOUT =
10
API_VERSION =
'2.0'
USER_AGENT =
"taobaoSDK-v#{VERSION}"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



13
14
15
# File 'lib/taobaoSDK/session.rb', line 13

def config
  @config
end

.sessionObject

Returns the value of attribute session.



13
14
15
# File 'lib/taobaoSDK/session.rb', line 13

def session
  @session
end

Class Method Details

.authorize_hashObject

authorize hash



43
44
45
46
47
48
49
50
51
# File 'lib/taobaoSDK/session.rb', line 43

def authorize_hash
  {
    "response_type" => 'code',
    "client_id" => config['api_key'],
    "redirect_uri" => config['authorize_redirect_uri'],
    "state" => 1212,
    "view" => 'web' 
  }
end

.authorize_urlObject

获取taobao oauth authorize url



39
40
41
# File 'lib/taobaoSDK/session.rb', line 39

def authorize_url
  "#{config['authorize']}?response_type=code&client_id=#{config['app_key']}&redirect_uri=#{config['authorize_redirect_uri']}&state=1212&view=web" 
end

.invoke(params) ⇒ Object

Return response in object format



29
30
31
32
33
34
35
36
37
# File 'lib/taobaoSDK/session.rb', line 29

def invoke(params)
  params = merge_params(params)
  response_body = RestClient.post(config['endpoint'],params).body
  res = parse_result(response_body)
  if res.is_a? TaobaoSDK::ErrorResponse
    raise res.msg
  end
  res
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"


22
23
24
25
26
# File 'lib/taobaoSDK/session.rb', line 22

def load(config_file)
  @config = YAML.load_file(config_file)
  @config = config[::Rails.env] if defined? ::Rails
  check_config_and_export_to_env
end

.token(code) ⇒ Object

获取token params授权码 return hash 解析返回参数后的hash



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/taobaoSDK/session.rb', line 55

def token(code)
  params = {
    :grant_type => "authorization_code",
    :code  => code,
    :client_id => config["app_key"],
    :client_secret => config["secret_key"],
    :redirect_uri =>  config['token_redirect_uri'],
    :view =>  'web'
  }
  JSON(RestClient.post(config['token'],params).body)
end