Class: Dianping::Api::Client
- Inherits:
-
Object
- Object
- Dianping::Api::Client
show all
- Includes:
- Modules::Tuangou
- Defined in:
- lib/dianping/api/client.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
#receipt_consume, #receipt_pre_code, #shop_deals
Constructor Details
#initialize(app_key, secret, **options) ⇒ Client
Returns a new instance of Client.
13
14
15
16
17
18
19
20
|
# File 'lib/dianping/api/client.rb', line 13
def initialize(app_key, secret, **options)
@app_key = app_key
@secret = secret
@site = options[:site] || 'https://openapi.dianping.com'
@redirect_url = options[:redirect_url]
@token_root = options[:token_root] || '/tmp'
@token = Token.new(self)
end
|
Instance Attribute Details
#app_key ⇒ Object
Returns the value of attribute app_key.
10
11
12
|
# File 'lib/dianping/api/client.rb', line 10
def app_key
@app_key
end
|
#redirect_url ⇒ Object
Returns the value of attribute redirect_url.
11
12
13
|
# File 'lib/dianping/api/client.rb', line 11
def redirect_url
@redirect_url
end
|
#site ⇒ Object
Returns the value of attribute site.
10
11
12
|
# File 'lib/dianping/api/client.rb', line 10
def site
@site
end
|
#token ⇒ Object
Returns the value of attribute token.
10
11
12
|
# File 'lib/dianping/api/client.rb', line 10
def token
@token
end
|
#token_root ⇒ Object
Returns the value of attribute token_root.
10
11
12
|
# File 'lib/dianping/api/client.rb', line 10
def token_root
@token_root
end
|
Instance Method Details
#auth(auth_code) ⇒ Object
43
44
45
|
# File 'lib/dianping/api/client.rb', line 43
def auth(auth_code)
token.auth(auth_code)
end
|
#auth_conn ⇒ Object
47
48
49
50
51
52
53
54
|
# File 'lib/dianping/api/client.rb', line 47
def auth_conn
Faraday.new(url: @site) do |conn|
conn.request :url_encoded
conn.response :logger
conn.response :raise_error
conn.adapter :net_http
end
end
|
#auth_token(auth_code) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/dianping/api/client.rb', line 56
def auth_token(auth_code)
res = auth_conn.post('/router/oauth/token') do |req|
req.body = {
app_key: @app_key,
app_secret: @secret,
auth_code: auth_code,
grant_type: 'authorization_code',
redirect_url: redirect_url
}
end
json(res.body)
end
|
#connection ⇒ Object
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/dianping/api/client.rb', line 22
def connection
Faraday.new(url: @site) do |conn|
conn.request :retry
conn.request :dianping, self
conn.request :url_encoded
conn.response :logger
conn.response :raise_error
conn.adapter :net_http
end
end
|
#get(url = nil, params = nil, headers = nil) ⇒ Object
33
34
35
36
|
# File 'lib/dianping/api/client.rb', line 33
def get(url = nil, params = nil, = nil)
res = connection.get(url, sign_with_share(params), )
json(res.body)
end
|
#json(text) ⇒ Object
85
86
87
|
# File 'lib/dianping/api/client.rb', line 85
def json(text)
MultiJson.load(text || '{}', symbolize_keys: true)
end
|
#merge_params(params) ⇒ Object
113
114
115
|
# File 'lib/dianping/api/client.rb', line 113
def merge_params(params)
share_params.merge(params || {}).dup.reject { |_k, v| v.nil? }
end
|
#post(url = nil, body = nil, headers = nil) ⇒ Object
38
39
40
41
|
# File 'lib/dianping/api/client.rb', line 38
def post(url = nil, body = nil, = nil)
res = connection.post(url, sign_with_share(body), )
json(res.body)
end
|
#refresh_token(refresh_code) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/dianping/api/client.rb', line 69
def refresh_token(refresh_code)
res = auth_conn.post('/router/oauth/token') do |req|
req.body = {
app_key: @app_key,
app_secret: @secret,
refresh_token: refresh_code,
grant_type: 'refresh_token'
}
end
json(res.body)
end
|
#requestid ⇒ Object
89
90
91
|
# File 'lib/dianping/api/client.rb', line 89
def requestid
SecureRandom.hex
end
|
#scope_shops ⇒ Object
81
82
83
|
# File 'lib/dianping/api/client.rb', line 81
def scope_shops
get('/router/oauth/session/scope', bid: token.bid)
end
|
#share_params ⇒ Object
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/dianping/api/client.rb', line 93
def share_params
{
app_key: app_key,
timestamp: Time.now.strftime('%F %T'),
session: token.access_token,
format: 'json',
v: 1,
sign_method: 'MD5'
}
end
|
#sign_with_share(params = {}) ⇒ Object
104
105
106
107
108
109
110
111
|
# File 'lib/dianping/api/client.rb', line 104
def sign_with_share(params = {})
merged = merge_params(params)
content = merged.to_a.sort.flatten.join.encode!('UTF-8')
Api.logger.debug { format('content: %s', content) }
sign = Digest::MD5.hexdigest([@secret, content, @secret].compact.join)
merged.merge(sign: sign)
end
|