dianping-api 0.1.0 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d90607c0ebb98542b7b46e660a0e0d58e752e9e9b039605c3b775526d28f4b2
4
- data.tar.gz: ea4989f921379d2c2ed163421473edba4702f9178df563fa8af7c814704d6a0e
3
+ metadata.gz: 273a0b63e1a93932e0418017345a05999480e3b24666a79cd7d8027cf6665422
4
+ data.tar.gz: c240ad8116fc4ea92cbfaf292c4b2b8dc5cead0b321c5178da860bd96ce7a603
5
5
  SHA512:
6
- metadata.gz: c39d73c68ff6362a4956829e343d8868d095faa06cd70c3325e2c0d540fe94ec130e0d963cc5c91b9ca0e0b4ef4744141694aaa85c94ec0c6974b7d9150f6ebf
7
- data.tar.gz: f4e95c77e75256976d43cb4622b7acb544421a4f92812f8c2e261e8ee3d94c14fc7483afa519e57d38f2de03da0d67d3f6923975bb1535c490512d46fdab1853
6
+ metadata.gz: 3b381e73e5f1b843b19106794740772d67ed279f066465a6ed93d672107e10590d79f8a30a26e302f0f9f75a40f79cb9b021b9a60f8b9169ad5a98be84d3ab30
7
+ data.tar.gz: 4d62e49edc8261d014fe4108aa730723a9005c0c99cb7ba9320946a34bd001ea5a338f0940e0d7a016b3687aa54dc01a23080bffb6ba93ccfaff7651357c520b
data/Gemfile.lock CHANGED
@@ -1,9 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dianping-api (0.1.0)
4
+ dianping-api (0.1.4)
5
5
  faraday (~> 1.0)
6
- multi_json (>= 1.0)
6
+ multi_json (~> 1.0)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
@@ -13,7 +13,7 @@ GEM
13
13
  coderay (1.1.3)
14
14
  crack (0.4.4)
15
15
  diff-lcs (1.4.4)
16
- faraday (1.1.0)
16
+ faraday (1.2.0)
17
17
  multipart-post (>= 1.2, < 3)
18
18
  ruby2_keywords
19
19
  ffi (1.13.1)
@@ -82,12 +82,12 @@ PLATFORMS
82
82
  DEPENDENCIES
83
83
  bundler (~> 1.17)
84
84
  dianping-api!
85
- guard (>= 2.0)
86
- guard-rspec (>= 4.0)
85
+ guard (~> 2.0)
86
+ guard-rspec (~> 4.0)
87
87
  rake (~> 10.0)
88
- rspec (>= 3.0)
89
- rspec-its (>= 1.0)
90
- webmock (>= 3.0)
88
+ rspec (~> 3.0)
89
+ rspec-its (~> 1.0)
90
+ webmock (~> 3.0)
91
91
 
92
92
  BUNDLED WITH
93
93
  1.17.3
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Dianping::Api
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dianping/api`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ 美团点评北极星平台Open API SDK for Ruby
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,19 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ 注册 https://open.dianping.com 按开发测试说明使用
24
+
25
+ ```ruby
26
+
27
+ # redirect_url 专门给获取access_token (session)使用
28
+ client = Dianping::Api::Client.new 'app_key', 'app_secrent', redirect_url: 'https://example.org/callback'
29
+
30
+ # 公共请求参数会自动提供并签名
31
+ # 正常结果 `code 200` 以外的返回报文会抛出异常
32
+ body_json = client.post '/routers/xxxx', biz_key1: 1, biz_key2: 2
33
+ body_json = client.get '/routers/xxxx', url_key1: 1, url_key2: 2
34
+
35
+ ```
26
36
 
27
37
  ## Development
28
38
 
@@ -0,0 +1 @@
1
+ require 'dianping/api'
data/lib/dianping/api.rb CHANGED
@@ -1,18 +1,36 @@
1
- require "dianping/api/version"
1
+ require 'dianping/api/version'
2
2
  require 'faraday'
3
+ require 'logger'
3
4
 
4
5
  module Dianping
5
6
  module Api
6
7
  class TokenExpireError < Faraday::RetriableResponse; end
7
- class TokenMissingError < StandardError; end
8
- class UsageError < StandardError; end
9
8
  class Error < StandardError; end
9
+ class TokenMissingError < Error; end
10
+ class UsageError < Error; end
10
11
 
12
+ def self.logger
13
+ @logger ||= defined?(Rails) ? Rails.logger : ::Logger.new(STDOUT)
14
+ end
15
+
16
+ def self.logger=(logger)
17
+ @logger = logger
18
+ end
19
+
20
+ def self.client
21
+ return @client = yield(Client) if block_given?
22
+
23
+ @client || raise(::Dianping::Api::Error, 'initialize client with block first')
24
+ end
25
+
26
+ module Modules; end
11
27
  end
12
28
  end
13
29
 
30
+ require 'dianping/api/modules/tuangou'
31
+
14
32
  require 'dianping/api/middle_ware'
15
33
  require 'dianping/api/token'
16
34
  require 'dianping/api/client'
17
35
 
18
- Faraday::Request.register_middleware dianping: ::Dianping::Api::MiddleWare
36
+ Faraday::Request.register_middleware dianping: ::Dianping::Api::MiddleWare
@@ -1,16 +1,21 @@
1
1
  require 'faraday'
2
+ require 'securerandom'
2
3
 
3
4
  module Dianping
4
5
  module Api
5
6
  class Client
6
7
 
7
- attr_reader :app_key, :site, :token, :redirect_url
8
+ include Modules::Tuangou
9
+
10
+ attr_reader :app_key, :site, :token, :token_root
11
+ attr_accessor :redirect_url
8
12
 
9
13
  def initialize(app_key, secret, **options)
10
14
  @app_key = app_key
11
15
  @secret = secret
12
- @site = 'https://openapi.dianping.com'
16
+ @site = options[:site] || 'https://openapi.dianping.com'
13
17
  @redirect_url = options[:redirect_url]
18
+ @token_root = options[:token_root] || '/tmp'
14
19
  @token = Token.new(self)
15
20
  end
16
21
 
@@ -73,10 +78,18 @@ module Dianping
73
78
  json(res.body)
74
79
  end
75
80
 
81
+ def scope_shops
82
+ get('/router/oauth/session/scope', bid: token.bid)
83
+ end
84
+
76
85
  def json(text)
77
86
  MultiJson.load(text || '{}', symbolize_keys: true)
78
87
  end
79
88
 
89
+ def requestid
90
+ SecureRandom.hex
91
+ end
92
+
80
93
  def share_params
81
94
  {
82
95
  app_key: app_key,
@@ -89,12 +102,17 @@ module Dianping
89
102
  end
90
103
 
91
104
  def sign_with_share(params = {})
92
- merged = share_params.merge(params || {}).dup
105
+ merged = merge_params(params)
93
106
  content = merged.to_a.sort.flatten.join.encode!('UTF-8')
94
107
  # puts @secret + content
95
- sign = Digest::MD5.hexdigest(@secret + content + @secret)
108
+ Api.logger.debug { format('content: %s', content) }
109
+ sign = Digest::MD5.hexdigest([@secret, content, @secret].compact.join)
96
110
  merged.merge(sign: sign)
97
111
  end
112
+
113
+ def merge_params(params)
114
+ share_params.merge(params || {}).dup.reject { |_k, v| v.nil? }
115
+ end
98
116
  end
99
117
  end
100
- end
118
+ end
@@ -9,9 +9,12 @@ module Dianping
9
9
  end
10
10
 
11
11
  def call(env)
12
+ Api.logger.debug { { request: env } }
12
13
  check_session(env)
13
14
  @app.call(env).on_complete do |response_env|
14
- check_response(response_env)
15
+ Api.logger.debug { { response: response_env } }
16
+ hash = MultiJson.load response_env.body, symbolize_keys: true
17
+ check_response(hash)
15
18
  end
16
19
  end
17
20
 
@@ -23,7 +26,17 @@ module Dianping
23
26
  raise TokenExpireError
24
27
  end
25
28
 
26
- def check_response(env)
29
+ def check_response(body)
30
+ code = body[:code].to_i
31
+ msg = format('[%<code>d]%<msg>s', code: code, msg: body[:msg])
32
+
33
+ Api.logger.debug { { response: body, msg: msg } }
34
+
35
+ Api.logger.warn body unless code == 200
36
+
37
+ raise TokenExpireError, msg if code == 608
38
+ raise UsageError, msg if code >= 800
39
+ raise Error, msg unless code == 200
27
40
  end
28
41
  end
29
42
  end
@@ -0,0 +1,26 @@
1
+ module Dianping
2
+ module Api
3
+ module Modules
4
+ module Tuangou
5
+ def receipt_pre_code(shop_uuid, code)
6
+ post('/router/tuangou/receipt/prepare', open_shop_uuid: shop_uuid, receipt_code: code)
7
+ end
8
+
9
+ def receipt_consume(shop_uuid, code, count = 1, request_id = nil, **params)
10
+ params.merge! open_shop_uuid: shop_uuid,
11
+ receipt_code: code,
12
+ requestid: request_id || requestid,
13
+ count: count
14
+ keys = %i[requestid receipt_code count open_shop_uuid app_shop_account app_shop_accountname]
15
+ raise "missing keys #{keys - params.keys}" unless (keys - params.keys).empty?
16
+
17
+ post '/router/tuangou/receipt/consume', params
18
+ end
19
+
20
+ def shop_deals(shop_deals)
21
+ get('/tuangou/deal/queryshopdeal', open_shop_uuid: shop_deals)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -7,20 +7,24 @@ module Dianping
7
7
 
8
8
  def initialize(client)
9
9
  @client = client
10
- @token_file = File.join('/tmp', "dianping-api-#{client.app_key}")
10
+ @token_file = File.join(client.token_root || 'tmp', "dianping-api-#{client.app_key}")
11
11
  end
12
12
 
13
13
  def access_hash
14
- @access_hash ||=
15
- begin
16
- token = MultiJson.load(File.read(@token_file), symbolize_keys: true)
17
- token[:access_hash] || (raise 'empty token')
18
- rescue Errno::ENOENT
19
- {}
20
- end
14
+ @access_hash ||= load_token
15
+ end
16
+
17
+ def load_token
18
+ token = MultiJson.load(File.read(@token_file), symbolize_keys: true)
19
+ token[:access_hash] || (raise 'empty token')
20
+ rescue Errno::ENOENT
21
+ {}
21
22
  end
22
23
 
23
24
  def refresh
25
+ @access_hash = load_token # try to use shared token first
26
+ return unless expired?
27
+
24
28
  raise Error, 'no refresh_token' unless refresh_token && remain_refresh_count > 1
25
29
 
26
30
  save_token(client.refresh_token(@access_hash[:refresh_token]))
@@ -49,12 +53,16 @@ module Dianping
49
53
  access_hash[:expires_in]
50
54
  end
51
55
 
56
+ def bid
57
+ access_hash[:bid]
58
+ end
59
+
52
60
  def updated_at
53
- Time.parse(access_hash[:updated_at])
61
+ DateTime.parse(access_hash[:updated_at]).to_time
54
62
  end
55
63
 
56
64
  def expires_at
57
- updated_at + expires_in
65
+ updated_at + expires_in rescue nil
58
66
  end
59
67
 
60
68
  def expired?
@@ -1,5 +1,5 @@
1
1
  module Dianping
2
2
  module Api
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.5"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dianping-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Wong
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-25 00:00:00.000000000 Z
11
+ date: 2021-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -156,9 +156,11 @@ files:
156
156
  - bin/console
157
157
  - bin/setup
158
158
  - dianping-api.gemspec
159
+ - lib/dianping-api.rb
159
160
  - lib/dianping/api.rb
160
161
  - lib/dianping/api/client.rb
161
162
  - lib/dianping/api/middle_ware.rb
163
+ - lib/dianping/api/modules/tuangou.rb
162
164
  - lib/dianping/api/token.rb
163
165
  - lib/dianping/api/version.rb
164
166
  homepage: https://github.com/lazing/dianping-api