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.
12
13
14
15
16
17
18
|
# File 'lib/dianping/api/client.rb', line 12
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 = Token.new(self)
end
|
Instance Attribute Details
#app_key ⇒ Object
Returns the value of attribute app_key.
9
10
11
|
# File 'lib/dianping/api/client.rb', line 9
def app_key
@app_key
end
|
#redirect_url ⇒ Object
Returns the value of attribute redirect_url.
10
11
12
|
# File 'lib/dianping/api/client.rb', line 10
def redirect_url
@redirect_url
end
|
#site ⇒ Object
Returns the value of attribute site.
9
10
11
|
# File 'lib/dianping/api/client.rb', line 9
def site
@site
end
|
#token ⇒ Object
Returns the value of attribute token.
9
10
11
|
# File 'lib/dianping/api/client.rb', line 9
def token
@token
end
|
Instance Method Details
#auth(auth_code) ⇒ Object
41
42
43
|
# File 'lib/dianping/api/client.rb', line 41
def auth(auth_code)
token.auth(auth_code)
end
|
#auth_conn ⇒ Object
45
46
47
48
49
50
51
52
|
# File 'lib/dianping/api/client.rb', line 45
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
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/dianping/api/client.rb', line 54
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
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/dianping/api/client.rb', line 20
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
31
32
33
34
|
# File 'lib/dianping/api/client.rb', line 31
def get(url = nil, params = nil, = nil)
res = connection.get(url, sign_with_share(params), )
json(res.body)
end
|
#json(text) ⇒ Object
83
84
85
|
# File 'lib/dianping/api/client.rb', line 83
def json(text)
MultiJson.load(text || '{}', symbolize_keys: true)
end
|
#post(url = nil, body = nil, headers = nil) ⇒ Object
36
37
38
39
|
# File 'lib/dianping/api/client.rb', line 36
def post(url = nil, body = nil, = nil)
res = connection.post(url, sign_with_share(body), )
json(res.body)
end
|
#refresh_token(refresh_code) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/dianping/api/client.rb', line 67
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
|
#scope_shops ⇒ Object
79
80
81
|
# File 'lib/dianping/api/client.rb', line 79
def scope_shops
get('/router/oauth/session/scope', bid: token.bid)
end
|
#share_params ⇒ Object
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/dianping/api/client.rb', line 87
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
98
99
100
101
102
103
104
105
|
# File 'lib/dianping/api/client.rb', line 98
def sign_with_share(params = {})
merged = share_params.merge(params || {}).dup
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
|