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
Constructor Details
#initialize(app_key, secret, **options) ⇒ Client
Returns a new instance of Client.
11
12
13
14
15
16
17
|
# File 'lib/dianping/api/client.rb', line 11
def initialize(app_key, secret, **options)
@app_key = app_key
@secret = secret
@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.
9
10
11
|
# File 'lib/dianping/api/client.rb', line 9
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
40
41
42
|
# File 'lib/dianping/api/client.rb', line 40
def auth(auth_code)
token.auth(auth_code)
end
|
#auth_conn ⇒ Object
44
45
46
47
48
49
50
51
|
# File 'lib/dianping/api/client.rb', line 44
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
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/dianping/api/client.rb', line 53
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
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/dianping/api/client.rb', line 19
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
30
31
32
33
|
# File 'lib/dianping/api/client.rb', line 30
def get(url = nil, params = nil, = nil)
res = connection.get(url, sign_with_share(params), )
json(res.body)
end
|
#json(text) ⇒ Object
78
79
80
|
# File 'lib/dianping/api/client.rb', line 78
def json(text)
MultiJson.load(text || '{}', symbolize_keys: true)
end
|
#post(url = nil, body = nil, headers = nil) ⇒ Object
35
36
37
38
|
# File 'lib/dianping/api/client.rb', line 35
def post(url = nil, body = nil, = nil)
res = connection.post(url, sign_with_share(body), )
json(res.body)
end
|
#refresh_token(refresh_code) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/dianping/api/client.rb', line 66
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
|
#share_params ⇒ Object
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/dianping/api/client.rb', line 82
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
93
94
95
96
97
98
99
|
# File 'lib/dianping/api/client.rb', line 93
def sign_with_share(params = {})
merged = share_params.merge(params || {}).dup
content = merged.to_a.sort.flatten.join.encode!('UTF-8')
sign = Digest::MD5.hexdigest(@secret + content + @secret)
merged.merge(sign: sign)
end
|