ne_api 0.0.22 → 0.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/BUNDLING_API.md +54 -0
- data/README.md +23 -0
- data/config/api.yaml +7 -0
- data/lib/ne_api/testing/fake_master.rb +2 -2
- data/lib/ne_api/version.rb +1 -1
- data/lib/ne_api.rb +70 -19
- data/spec/bundling_dispatch_spec.rb +90 -0
- data/spec/token_refresh_spec.rb +105 -0
- metadata +11 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: cafa6c991b17f90ff2dee98f2434a77dc217772e7f4b042c52029a991aeaed14
|
|
4
|
+
data.tar.gz: a910e846c1c0151e4b18ee9530e27a39b60130a79aeacc397f70fe377016db40
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c24b58eae6fd8cfa38890c46cf8ee86f6e31b281bc83d74c60de2ac83b0efde524870e69f7f66699e93cf34e5c60b4e0cf04b981ae20078b58590f33a28b5e9f
|
|
7
|
+
data.tar.gz: 0ddd2d0c9cbf1e7ac7d6e0d1937bad95cb6b5979318aec8715774719e185bb97bd090aba90b3267e258fd321bde5bd45647ec035dc457c8bd39a4148fa2f9de7
|
data/BUNDLING_API.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# 同梱処理で使う NE API 一覧(ne_api gem)
|
|
2
|
+
|
|
3
|
+
ne_bundling(同梱処理アプリ)が利用する NE API と、その gem 呼び出し名の一覧。
|
|
4
|
+
v0.0.23 で「同梱系API」を追加した。呼び出しは `NeAPI::Master#<model>_<method>` 形式。
|
|
5
|
+
|
|
6
|
+
## v0.0.23 で追加した同梱系API
|
|
7
|
+
|
|
8
|
+
| gem 呼び出し | NE エンドポイント | 用途 | 主なパラメータ | 戻り値(get_key) |
|
|
9
|
+
| --- | --- | --- | --- | --- |
|
|
10
|
+
| `receiveorder_base_bundle` | `POST /api_v1_receiveorder_base/bundle` | 受注伝票の一括同梱(NE標準同梱処理を実行) | `params: { data: <JSON文字列>, receive_order_recalculate_flag: "1"/"0" }` | `results`(ジョブ毎の同梱結果配列) |
|
|
11
|
+
| `receiveorder_base_bundle_candidate_groups` | `POST /api_v1_receiveorder_base/bundle_candidate_groups` | 同梱候補グループ取得(同梱可能な受注をグループ化) | `params: { target_receive_order_ids: "1,2,3" }`(省略時は全受注対象) | `groups`(受注番号配列の配列) |
|
|
12
|
+
| `receiveorder_groupingtag_search` | `POST /api_v1_receiveorder_groupingtag/search` | 受注分類タグ検索 | `fields:`, `query:`(`grouping_tag_*`) | `data` |
|
|
13
|
+
|
|
14
|
+
### `receiveorder_base_bundle` の `data` 構造
|
|
15
|
+
|
|
16
|
+
```json
|
|
17
|
+
[
|
|
18
|
+
{
|
|
19
|
+
"receive_order_id": "同梱先(親)の受注伝票番号",
|
|
20
|
+
"receive_order_last_modified_date": "YYYY-MM-DD HH:MM:SS", // 楽観ロック用
|
|
21
|
+
"bundles": [
|
|
22
|
+
{ "receive_order_id": "同梱元(子)の受注伝票番号",
|
|
23
|
+
"receive_order_last_modified_date": "YYYY-MM-DD HH:MM:SS" }
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
]
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
- `data` 配列は 1〜100 ジョブ。`receive_order_last_modified_date` が DB の最終更新日と不一致の伝票はスキップされる。
|
|
30
|
+
- `results[].bundled_receive_order_ids` に成功した子注文、`skipped_receive_order_ids` / `skipped_receive_order_reasons` にスキップ分が返る。
|
|
31
|
+
- 同梱元が全件スキップになるとエラー(コード `028013`)。固有エラーは `028001`〜`028021`。
|
|
32
|
+
|
|
33
|
+
### `bundle_candidate_groups` のグループ判定条件
|
|
34
|
+
|
|
35
|
+
同一グループは次を全て満たす受注: ①同じ店舗 ②有効な伝票(未キャンセル)③受注状態が起票済/納品書印刷待ち/納品書印刷済 ④送り先名一致 ⑤送り先住所一致 ⑥送り先電話番号一致 ⑦送り先未マスク。
|
|
36
|
+
固有エラー: `029001`(1万件超) / `029002`(非数値) / `029003`。
|
|
37
|
+
|
|
38
|
+
## 同梱フローで使う既存API(v0.0.22 時点で定義済み)
|
|
39
|
+
|
|
40
|
+
| gem 呼び出し | NE エンドポイント | 用途 |
|
|
41
|
+
| --- | --- | --- |
|
|
42
|
+
| `receiveorder_row_search` / `_count` | `/api_v1_receiveorder_row/{search,count}` | 受注明細の検索・件数(同梱対象の絞り込み) |
|
|
43
|
+
| `receiveorder_base_search` / `_count` | `/api_v1_receiveorder_base/{search,count}` | 受注伝票の検索・件数(配送先・状態の取得) |
|
|
44
|
+
| `receiveorder_base_update` | `/api_v1_receiveorder_base/update` | 受注更新(商品追加・確認・タグ付与: XML) |
|
|
45
|
+
| `receiveorder_base_bulkupdate` | `/api_v1_receiveorder_base/bulkupdate` | 子注文の一括更新(キャンセル等: XML) |
|
|
46
|
+
| `master_goods_search` / `_count` | `/api_v1_master_goods/{search,count}` | 商品マスタ取得(商品名等の付与) |
|
|
47
|
+
| `master_shop_search` | `/api_v1_master_shop/search` | 店舗一覧取得(shop_id 絞り込み用) |
|
|
48
|
+
| `notice_execution_add` | `/api_v1_notice_execution/add` | 実行結果お知らせ登録 |
|
|
49
|
+
| `login_company_info` / `login_user_info` | `/api_v1_login_{company,user}/info` | 認証・企業/ユーザー情報 |
|
|
50
|
+
|
|
51
|
+
## 補足
|
|
52
|
+
|
|
53
|
+
- 現行の同梱アプリ(`ne_bundling`)は `receiveorder_base_update` + `bulkupdate` を XML で組み立ててクライアント側で同梱を実装している。NE標準の `receiveorder_base_bundle` / `bundle_candidate_groups` を使うと、グループ判定〜同梱実行を NE 側に委譲でき、ロジックを大幅に簡素化できる(改修方針の選択肢)。
|
|
54
|
+
- 全エンドポイントの詳細仕様は ne_bundling 側 `doc/ne_api_reference.txt`(出典 https://developer.next-engine.com/llms-full.txt )を参照。
|
data/README.md
CHANGED
|
@@ -266,6 +266,9 @@ This allows easy injection of `FakeMaster` in tests.
|
|
|
266
266
|
5. Create a new Pull Request
|
|
267
267
|
|
|
268
268
|
## Update
|
|
269
|
+
### ver0.0.23
|
|
270
|
+
* 同梱系APIに対応(受注伝票一括同梱 `receiveorder_base_bundle` / 同梱候補グループ取得 `receiveorder_base_bundle_candidate_groups` / 受注分類タグ検索 `receiveorder_groupingtag_search`)。詳細は [`BUNDLING_API.md`](BUNDLING_API.md) 参照
|
|
271
|
+
* `method_missing` を複数語メソッド対応に改修(登録モデル名の最長前方一致で解決。単一語メソッドは従来通りで後方互換)
|
|
269
272
|
### ver0.0.22
|
|
270
273
|
* bug fix
|
|
271
274
|
### ver0.0.21
|
|
@@ -297,3 +300,23 @@ This allows easy injection of `FakeMaster` in tests.
|
|
|
297
300
|
### ver0.0.6
|
|
298
301
|
* Ruby2.2対応
|
|
299
302
|
* リトライ回数のデフォルト値変更
|
|
303
|
+
|
|
304
|
+
## トークンの永続化 (0.0.24〜)
|
|
305
|
+
|
|
306
|
+
レスポンスにトークンが含まれていたとき(エラー応答・混雑時の再試行の途中を含む)に呼ばれるフックを設定できます。
|
|
307
|
+
次の呼び出しの前にトークンを保存しておくことで、古いトークンによる `002002` を防げます。
|
|
308
|
+
|
|
309
|
+
```ruby
|
|
310
|
+
master = NeAPI::Master.new(access_token: user.access_token, refresh_token: user.refresh_token)
|
|
311
|
+
master.on_token_refresh = ->(m, _body) { user.update!(access_token: m.access_token, refresh_token: m.refresh_token) }
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
`NeAPIException` からはエラーコードとエラー応答を取得できます(メッセージは従来どおり `"コード:メッセージ"`)。
|
|
315
|
+
|
|
316
|
+
```ruby
|
|
317
|
+
rescue NeAPIException => e
|
|
318
|
+
e.code #=> "020006"
|
|
319
|
+
e.response #=> { "result" => "error", "code" => "020006", "message" => "..." }
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
テストなどで HTTP 接続を差し替えるには `NeAPI.connection = Faraday.new { ... }` を設定します(`nil` で元に戻ります)。
|
data/config/api.yaml
CHANGED
|
@@ -218,6 +218,8 @@
|
|
|
218
218
|
- divide
|
|
219
219
|
- bulkupdate
|
|
220
220
|
- bulkupdatereceipted
|
|
221
|
+
- bundle
|
|
222
|
+
- bundle_candidate_groups
|
|
221
223
|
:prefix: receive_order
|
|
222
224
|
:fields: shop_id,id,shop_cut_form_id,date,import_date,important_check_id,important_check_name,confirm_check_id,confirm_check_name,confirm_ids,mail_status,gruoping_tag,import_type_id,import_type_name,cancel_type_id,cancel_type_name,cancel_date,closed_after_edit_date,order_status_id,order_status_name,delivery_id,delivery_name,payment_method_id,payment_method_name,total_amount,tax_amount,charge_amount,delivery_fee_amount,other_amount,point_amount,goods_amount,deposit_amount,deposit_type_id,deposit_type_name,deposit_date,foreign_total_amount,foreign_tax_amount,foreign_charge_amount,foreign_delivery_fee_amount,foreign_other_amount,foreign_goods_amount,foreign_deposit_amount,note,include_possible_order_id,include_to_order_id,multi_delivery_parent_order_id,multi_delivery_parent_flag,statement_delivery_instruct_printing_date,statement_delivery_printing_date,statement_delivery_text,send_date,send_plan_date,send_sequence,worker_text,picking_instruct,picking_min_supplier_id,picking_min_goods_id,label_print_date,label_print_flag,hope_delivery_date,hope_delivery_time_slot_id,hope_delivery_time_slot_name,delivery_method_id,delivery_method_name,seal1_id,seal1_name,seal2_id,seal2_name,seal3_id,seal3_name,seal4_id,seal4_name,business_office_stop_id,business_office_stop_name,invoice_id,invoice_name,temperature_id,temperature_name,business_office_name,gift_flag,delivery_cut_form_id,delivery_cut_form_note,credit_type_id,credit_type_name,credit_approval_no,credit_approval_amount,credit_approval_type_id,credit_approval_type_name,credit_approval_date,credit_approval_rate,credit_number_payments,credit_authorization_center_id,credit_authorization_center_name,credit_approval_fax_printing_date,customer_type_id,customer_type_name,customer_id,purchaser_name,purchaser_kana,purchaser_zip_code,purchaser_address1,purchaser_address2,purchaser_tel,purchaser_fax,purchaser_mail_address,consignee_name,consignee_kana,consignee_zip_code,consignee_address1,consignee_address2,consignee_tel,consignee_fax,reminder_start_date,reminder_last_date,reminder_count,important_check_pic_id,important_check_pic_name,pic_id,pic_name,send_pic_id,send_pic_name,deleted_flag,creation_date,last_modified_date,last_modified_null_safe_date,creator_id,creator_name,last_modified_by_id,last_modified_by_null_safe_id,last_modified_by_name,last_modified_by_null_safe_name
|
|
223
225
|
:receiveorder_option:
|
|
@@ -245,6 +247,11 @@
|
|
|
245
247
|
- update
|
|
246
248
|
:prefix: confirm
|
|
247
249
|
:fields: id,name,display_order,html,value,valid_flag
|
|
250
|
+
:receiveorder_groupingtag:
|
|
251
|
+
:method:
|
|
252
|
+
- search
|
|
253
|
+
:prefix: grouping_tag
|
|
254
|
+
:fields: id,name,color,str_color,memo,creation_date,last_modified_date,last_modified_null_safe_date,creator_id,creator_name,last_modified_by_id,last_modified_by_null_safe_id,last_modified_by_name,last_modified_by_null_safe_name
|
|
248
255
|
:receiveorder_paymentdeliveryconvert:
|
|
249
256
|
:method:
|
|
250
257
|
- count
|
|
@@ -13,7 +13,7 @@ module NeAPI
|
|
|
13
13
|
#
|
|
14
14
|
class FakeMaster
|
|
15
15
|
attr_accessor :access_token, :refresh_token, :wait_flag, :retry_num, :wait_interval
|
|
16
|
-
attr_accessor :access_token_end_date, :refresh_token_end_date
|
|
16
|
+
attr_accessor :access_token_end_date, :refresh_token_end_date, :on_token_refresh
|
|
17
17
|
attr_reader :call_history
|
|
18
18
|
|
|
19
19
|
def initialize(access_token: "test_access_token", refresh_token: "test_refresh_token")
|
|
@@ -106,7 +106,7 @@ module NeAPI
|
|
|
106
106
|
@call_history << { method: method_name.to_sym, args: args, time: Time.now }
|
|
107
107
|
|
|
108
108
|
if @error_mode
|
|
109
|
-
raise NeAPIException
|
|
109
|
+
raise NeAPIException.new("#{@error_code}:#{@error_message}", code: @error_code)
|
|
110
110
|
end
|
|
111
111
|
|
|
112
112
|
if @stubbed_responses.key?(method_name.to_sym)
|
data/lib/ne_api/version.rb
CHANGED
data/lib/ne_api.rb
CHANGED
|
@@ -9,12 +9,19 @@ require 'dotenv'
|
|
|
9
9
|
module NeAPI
|
|
10
10
|
API_SERVER_HOST = "https://api.next-engine.org"
|
|
11
11
|
NE_SERVER_HOST = "https://base.next-engine.org"
|
|
12
|
+
# メイン機能が混み合っているときのエラーコード。間隔を空けて再試行する
|
|
13
|
+
RETRYABLE_CODES = ["003001","003002","008003","009005","011007"].freeze
|
|
14
|
+
|
|
15
|
+
class << self
|
|
16
|
+
# Faraday のコネクションを差し替える(テストやスタブ用)。nil のときは API_SERVER_HOST へ接続する
|
|
17
|
+
attr_accessor :connection
|
|
18
|
+
end
|
|
12
19
|
|
|
13
20
|
private
|
|
14
21
|
|
|
15
22
|
|
|
16
23
|
def conn
|
|
17
|
-
@conn ||=
|
|
24
|
+
@conn ||= NeAPI.connection || Faraday.new(:url => API_SERVER_HOST) do |faraday|
|
|
18
25
|
faraday.request :url_encoded # form-encode POST params
|
|
19
26
|
faraday.response :logger # log requests to STDOUT
|
|
20
27
|
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
|
@@ -25,10 +32,10 @@ module NeAPI
|
|
|
25
32
|
def response response
|
|
26
33
|
body = JSON.parse response.body
|
|
27
34
|
if body["result"] != "success"
|
|
28
|
-
if
|
|
35
|
+
if RETRYABLE_CODES.include?(body["code"])
|
|
29
36
|
return false
|
|
30
37
|
else
|
|
31
|
-
raise NeAPIException
|
|
38
|
+
raise NeAPIException.new(sprintf("%s:%s", body["code"], body["message"]), code: body["code"], response: body)
|
|
32
39
|
end
|
|
33
40
|
return false
|
|
34
41
|
end
|
|
@@ -38,6 +45,9 @@ module NeAPI
|
|
|
38
45
|
class Master
|
|
39
46
|
include NeAPI
|
|
40
47
|
attr_accessor :access_token, :refresh_token, :wait_flag, :retry_num, :wait_interval, :access_token_end_date, :refresh_token_end_date
|
|
48
|
+
# レスポンスにトークンが含まれていたとき(エラー応答を含む)に呼ばれるフック。
|
|
49
|
+
# ->(master, response_body) { ... } を設定すると、次の呼び出しの前にトークンを永続化できる
|
|
50
|
+
attr_accessor :on_token_refresh
|
|
41
51
|
PATH_PREFIX="/api_v1_"
|
|
42
52
|
|
|
43
53
|
def initialize access_token: "", refresh_token: ""
|
|
@@ -73,33 +83,61 @@ module NeAPI
|
|
|
73
83
|
post_args = {access_token: @access_token, refresh_token: @refresh_token}.merge(params)
|
|
74
84
|
end
|
|
75
85
|
res = false
|
|
76
|
-
|
|
86
|
+
|
|
77
87
|
self.retry_num.times do
|
|
78
|
-
|
|
79
|
-
|
|
88
|
+
body = JSON.parse((conn.post PATH_PREFIX+model.to_s+ "/" + method, post_args).body)
|
|
89
|
+
# エラー応答でもトークンが更新されることがある。取りこぼすと次の呼び出しが 002002 になるので必ず取り込む
|
|
90
|
+
store_tokens(body)
|
|
91
|
+
if body["result"] == "success"
|
|
92
|
+
res = body
|
|
93
|
+
break
|
|
94
|
+
end
|
|
95
|
+
unless RETRYABLE_CODES.include?(body["code"])
|
|
96
|
+
raise NeAPIException.new(sprintf("%s:%s", body["code"], body["message"]), code: body["code"], response: body)
|
|
97
|
+
end
|
|
98
|
+
# 再試行は更新後のトークンで行う
|
|
99
|
+
post_args[:access_token] = @access_token
|
|
100
|
+
post_args[:refresh_token] = @refresh_token
|
|
80
101
|
sleep(self.wait_interval)
|
|
81
102
|
end
|
|
82
103
|
if res == false
|
|
83
|
-
raise NeAPIException
|
|
84
|
-
return false
|
|
104
|
+
raise NeAPIException.new("003001:Next Engineが大変混み合っています。APIの接続を#{self.retry_num}回、#{self.wait_interval}秒間隔でアクセスを試みましたが、失敗をしました", code: "003001")
|
|
85
105
|
end
|
|
86
|
-
|
|
87
|
-
@access_token = res["access_token"] if res["access_token"].present?
|
|
88
|
-
@refresh_token = res["refresh_token"] if res["refresh_token"].present?
|
|
89
|
-
@access_token_end_date = res["access_token_end_date"] if res["access_token_end_date"].present?
|
|
90
|
-
@refresh_token_end_date = res["refresh_token_end_date"] if res["refresh_token_end_date"].present?
|
|
91
106
|
|
|
92
107
|
get_key.present? ? res[get_key] : res
|
|
93
108
|
end
|
|
109
|
+
# レスポンスに含まれるトークンと有効期限を取り込み、on_token_refresh フックを呼ぶ
|
|
110
|
+
def store_tokens(body)
|
|
111
|
+
return unless body.is_a?(Hash) && body["access_token"].present?
|
|
112
|
+
|
|
113
|
+
@access_token = body["access_token"]
|
|
114
|
+
@refresh_token = body["refresh_token"] if body["refresh_token"].present?
|
|
115
|
+
@access_token_end_date = body["access_token_end_date"] if body["access_token_end_date"].present?
|
|
116
|
+
@refresh_token_end_date = body["refresh_token_end_date"] if body["refresh_token_end_date"].present?
|
|
117
|
+
on_token_refresh.call(self, body) if on_token_refresh
|
|
118
|
+
end
|
|
119
|
+
|
|
94
120
|
def method_missing(path, args={})
|
|
95
121
|
super if @@params.nil? || path.nil?
|
|
96
|
-
|
|
97
|
-
|
|
122
|
+
|
|
123
|
+
# path を「モデル名_メソッド名」に分解する。
|
|
124
|
+
# 旧実装は最後の "_" で分割していたが、bundle_candidate_groups のような
|
|
125
|
+
# 複数語メソッドに対応できなかった。登録済みモデル名との前方一致(最長一致優先)で
|
|
126
|
+
# 解決することで、単語数によらず正しい model / method を取り出す。
|
|
127
|
+
# 単一語メソッドでは旧実装と同じ結果になるため後方互換。
|
|
128
|
+
model = nil
|
|
129
|
+
method = nil
|
|
130
|
+
@@params.keys.sort_by { |key| -key.to_s.length }.each do |key|
|
|
131
|
+
prefix = "#{key}_"
|
|
132
|
+
next unless path.to_s.start_with?(prefix)
|
|
133
|
+
candidate = path.to_s[prefix.length..-1]
|
|
134
|
+
next unless @@params[key][:method].include?(candidate)
|
|
135
|
+
model = key
|
|
136
|
+
method = candidate
|
|
137
|
+
break
|
|
98
138
|
end
|
|
99
|
-
model = models.captures.first.to_sym
|
|
100
|
-
method = path.to_s.split("_").last
|
|
101
139
|
|
|
102
|
-
if
|
|
140
|
+
if model
|
|
103
141
|
get_key = nil
|
|
104
142
|
query = (args[:query].present? ? args[:query] : nil)
|
|
105
143
|
fields = (args[:fields].present? ? args[:fields] : nil)
|
|
@@ -119,6 +157,12 @@ module NeAPI
|
|
|
119
157
|
get_key = "result"
|
|
120
158
|
when "divide"
|
|
121
159
|
get_key = "receive_order_id"
|
|
160
|
+
when "bundle"
|
|
161
|
+
# 受注伝票一括同梱: ジョブ毎の結果配列(results)を返す
|
|
162
|
+
get_key = "results"
|
|
163
|
+
when "bundle_candidate_groups"
|
|
164
|
+
# 同梱候補グループ取得: 同梱可能な受注番号配列の配列(groups)を返す
|
|
165
|
+
get_key = "groups"
|
|
122
166
|
when "checkconnect"
|
|
123
167
|
fields = nil
|
|
124
168
|
get_key = nil
|
|
@@ -161,5 +205,12 @@ module NeAPI
|
|
|
161
205
|
end
|
|
162
206
|
end
|
|
163
207
|
class NeAPIException < StandardError
|
|
164
|
-
|
|
208
|
+
# code: NEのエラーコード(例 "002004")、response: エラー応答のボディ(Hash)。メッセージは従来どおり "コード:メッセージ"
|
|
209
|
+
attr_reader :code, :response
|
|
165
210
|
|
|
211
|
+
def initialize(message = nil, code: nil, response: nil)
|
|
212
|
+
@code = code || message.to_s[/\A(\d{6}):/, 1]
|
|
213
|
+
@response = response
|
|
214
|
+
super(message)
|
|
215
|
+
end
|
|
216
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# 同梱系API追加(v0.0.23)に伴う method_missing ディスパッチの検証。
|
|
2
|
+
# 実APIには接続せず、post を差し替えて model / method / get_key の解決のみを検証する。
|
|
3
|
+
#
|
|
4
|
+
# 実行: rspec spec/bundling_dispatch_spec.rb (bundle exec 不要 / グローバルgem使用)
|
|
5
|
+
|
|
6
|
+
require_relative "../lib/ne_api"
|
|
7
|
+
|
|
8
|
+
RSpec.describe NeAPI::Master do
|
|
9
|
+
subject(:master) { described_class.new(access_token: "a", refresh_token: "r") }
|
|
10
|
+
|
|
11
|
+
# post を差し替え、method_missing が組み立てた引数をそのまま返させる
|
|
12
|
+
before do
|
|
13
|
+
allow(master).to receive(:post) { |**kwargs| kwargs }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
describe "既存メソッドのディスパッチ(後方互換)" do
|
|
17
|
+
it "receiveorder_base_search は search に解決される" do
|
|
18
|
+
result = master.receiveorder_base_search
|
|
19
|
+
expect(result).to include(model: :receiveorder_base, method: "search", get_key: "data")
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it "件数取得は count / get_key=count に解決される" do
|
|
23
|
+
expect(master.receiveorder_base_count).to include(method: "count", get_key: "count")
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "info系は info / get_key=nil に解決される" do
|
|
27
|
+
expect(master.login_company_info).to include(model: :login_company, method: "info", get_key: nil)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "一括更新は bulkupdate / get_key=result に解決される" do
|
|
31
|
+
expect(master.receiveorder_base_bulkupdate).to include(method: "bulkupdate", get_key: "result")
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
describe "モデル名の前方一致が曖昧なケースの解決" do
|
|
36
|
+
it "master_goods_search は master_goods に解決される" do
|
|
37
|
+
expect(master.master_goods_search).to include(model: :master_goods, method: "search")
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "master_goods_page_search はより長い master_goods_page に解決される" do
|
|
41
|
+
expect(master.master_goods_page_search).to include(model: :master_goods_page, method: "search")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it "master_goods_page_variation_search は最長一致の master_goods_page_variation に解決される" do
|
|
45
|
+
expect(master.master_goods_page_variation_search)
|
|
46
|
+
.to include(model: :master_goods_page_variation, method: "search")
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
describe "同梱系API(v0.0.23 で追加)" do
|
|
51
|
+
it "receiveorder_base_bundle は bundle / get_key=results に解決される" do
|
|
52
|
+
result = master.receiveorder_base_bundle(params: { data: "[]", receive_order_recalculate_flag: 1 })
|
|
53
|
+
expect(result).to include(model: :receiveorder_base, method: "bundle", get_key: "results")
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it "bundle は params をそのまま post へ渡す" do
|
|
57
|
+
params = { data: "[{\"receive_order_id\":\"1\"}]", receive_order_recalculate_flag: 1 }
|
|
58
|
+
expect(master.receiveorder_base_bundle(params: params)).to include(params: params)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it "複数語メソッド bundle_candidate_groups が groups に解決される" do
|
|
62
|
+
result = master.receiveorder_base_bundle_candidate_groups(params: { target_receive_order_ids: "1,2,3" })
|
|
63
|
+
expect(result).to include(
|
|
64
|
+
model: :receiveorder_base,
|
|
65
|
+
method: "bundle_candidate_groups",
|
|
66
|
+
get_key: "groups"
|
|
67
|
+
)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it "receiveorder_groupingtag_search は新規モデルとして search に解決される" do
|
|
71
|
+
result = master.receiveorder_groupingtag_search
|
|
72
|
+
expect(result).to include(model: :receiveorder_groupingtag, method: "search", get_key: "data")
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
it "groupingtag_search はデフォルトfieldsに grouping_tag_ プレフィックスを付与する" do
|
|
76
|
+
expect(master.receiveorder_groupingtag_search[:fields]).to start_with("grouping_tag_id,")
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
describe "未定義メソッド" do
|
|
81
|
+
it "存在しないAPIは NoMethodError を送出する" do
|
|
82
|
+
expect { master.nonexistent_foo }.to raise_error(NoMethodError)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it "存在するモデルでも未定義のメソッドは NoMethodError を送出する" do
|
|
86
|
+
# receiveorder_groupingtag は search のみ。update は未定義。
|
|
87
|
+
expect { master.receiveorder_groupingtag_update }.to raise_error(NoMethodError)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
require 'ne_api'
|
|
2
|
+
|
|
3
|
+
# 実APIには接続せず、Faraday のテストアダプタで応答を差し替える
|
|
4
|
+
describe 'トークンの取り込みとエラー' do
|
|
5
|
+
def connection(responses, requests = [])
|
|
6
|
+
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
|
|
7
|
+
stub.post(%r{\A/api_v1_}) do |env|
|
|
8
|
+
requests << Faraday::Utils.parse_query(env.body)
|
|
9
|
+
[200, {}, JSON.generate(responses.shift)]
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
Faraday.new { |f| f.request :url_encoded; f.adapter :test, stubs }
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
let(:master) do
|
|
16
|
+
NeAPI::Master.new(access_token: "a0", refresh_token: "r0").tap { |m| m.wait_interval = 0; m.retry_num = 3 }
|
|
17
|
+
end
|
|
18
|
+
let(:tokens) { { "access_token" => "a1", "refresh_token" => "r1", "access_token_end_date" => "2026-11-20 00:00:00", "refresh_token_end_date" => "2026-11-22 00:00:00" } }
|
|
19
|
+
|
|
20
|
+
after { NeAPI.connection = nil }
|
|
21
|
+
|
|
22
|
+
it '成功応答のトークンを取り込み、on_token_refresh を呼ぶ' do
|
|
23
|
+
NeAPI.connection = connection([{ "result" => "success", "data" => [] }.merge(tokens)])
|
|
24
|
+
seen = []
|
|
25
|
+
master.on_token_refresh = ->(m, body) { seen << [m.access_token, body["result"]] }
|
|
26
|
+
expect(master.master_shop_search(fields: "shop_id")).to eq([])
|
|
27
|
+
expect([master.access_token, master.refresh_token, master.refresh_token_end_date]).to eq(["a1", "r1", "2026-11-22 00:00:00"])
|
|
28
|
+
expect(seen).to eq([["a1", "success"]])
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'エラー応答でもトークンを取り込んでから例外を上げる' do
|
|
32
|
+
NeAPI.connection = connection([{ "result" => "error", "code" => "020006", "message" => "更新されています" }.merge(tokens)])
|
|
33
|
+
seen = []
|
|
34
|
+
master.on_token_refresh = ->(m, _body) { seen << m.access_token }
|
|
35
|
+
expect { master.receiveorder_base_update(params: { receive_order_id: 1 }) }.to raise_error(NeAPIException) { |e|
|
|
36
|
+
expect(e.code).to eq("020006")
|
|
37
|
+
expect(e.message).to eq("020006:更新されています")
|
|
38
|
+
expect(e.response["message"]).to eq("更新されています")
|
|
39
|
+
}
|
|
40
|
+
expect(master.access_token).to eq("a1")
|
|
41
|
+
expect(seen).to eq(["a1"])
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it '混雑エラーは再試行し、再試行には更新後のトークンを使う' do
|
|
45
|
+
requests = []
|
|
46
|
+
NeAPI.connection = connection([{ "result" => "error", "code" => "003002", "message" => "busy" }.merge(tokens),
|
|
47
|
+
{ "result" => "success", "count" => "5" }], requests)
|
|
48
|
+
expect(master.master_shop_count).to eq("5")
|
|
49
|
+
expect(requests.map { |r| r["access_token"] }).to eq(["a0", "a1"])
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it '混雑が続いたら 003001 の例外' do
|
|
53
|
+
NeAPI.connection = connection(Array.new(3) { { "result" => "error", "code" => "003002", "message" => "busy" } })
|
|
54
|
+
expect { master.master_shop_count }.to raise_error(NeAPIException) { |e| expect(e.code).to eq("003001") }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
describe '後方互換(既存アプリの呼び出し方)' do
|
|
58
|
+
it 'search は data、count は count、update は result、info はボディ全体を返す' do
|
|
59
|
+
NeAPI.connection = connection([
|
|
60
|
+
{ "result" => "success", "data" => [{ "shop_id" => "1" }] },
|
|
61
|
+
{ "result" => "success", "count" => "3" },
|
|
62
|
+
{ "result" => "success" },
|
|
63
|
+
{ "result" => "success", "data" => [{ "company_id" => "c" }] }
|
|
64
|
+
])
|
|
65
|
+
expect(master.master_shop_search(fields: "shop_id", query: { "shop_deleted_flag-eq" => 0 })).to eq([{ "shop_id" => "1" }])
|
|
66
|
+
expect(master.receiveorder_base_count(query: { "receive_order_id-eq" => 1 })).to eq("3")
|
|
67
|
+
expect(master.receiveorder_base_update(params: { receive_order_id: 1, data: "<root/>" })).to eq("success")
|
|
68
|
+
expect(master.login_company_info["data"].first["company_id"]).to eq("c")
|
|
69
|
+
expect(master.access_token).to eq("a0") # 応答にトークンが無ければ変わらない
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it 'query・fields・params・wait_flag の送り方は変わらない' do
|
|
73
|
+
requests = []
|
|
74
|
+
NeAPI.connection = connection([{ "result" => "success", "data" => [] }], requests)
|
|
75
|
+
master.force_import
|
|
76
|
+
master.receiveorder_base_search(fields: "receive_order_id", query: { "receive_order_id-in" => "1,2" }, params: { offset: 10 })
|
|
77
|
+
expect(requests.first).to eq("access_token" => "a0", "refresh_token" => "r0", "fields" => "receive_order_id",
|
|
78
|
+
"receive_order_id-in" => "1,2", "offset" => "10", "wait_flag" => "1")
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it 'フックを設定しなくても動き、例外は従来どおり NeAPIException でメッセージは "コード:メッセージ"' do
|
|
82
|
+
NeAPI.connection = connection([{ "result" => "error", "code" => "004002", "message" => "fieldsが不正です" }])
|
|
83
|
+
expect { master.master_shop_search(fields: "x") }.to raise_error(NeAPIException, "004002:fieldsが不正です")
|
|
84
|
+
expect { raise NeAPIException, "plain" }.to raise_error(NeAPIException, "plain")
|
|
85
|
+
expect { raise NeAPIException }.to raise_error(NeAPIException)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
it 'トークンが無ければ従来どおり no token!' do
|
|
89
|
+
expect { NeAPI::Master.new(access_token: nil, refresh_token: nil).master_shop_count }.to raise_error(NeAPIException, "no token!")
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it 'テスト用スタブ FakeMaster の使い方も変わらない' do
|
|
93
|
+
require 'ne_api/testing'
|
|
94
|
+
fake = NeAPI::Testing::FakeMaster.new.stub(:master_shop_search, [{ "shop_id" => "1" }])
|
|
95
|
+
expect(fake.master_shop_search).to eq([{ "shop_id" => "1" }])
|
|
96
|
+
fake.simulate_error(code: "001001", message: "認証エラー")
|
|
97
|
+
expect { fake.master_shop_search }.to raise_error(NeAPIException, "001001:認証エラー")
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
it '従来どおりメッセージだけで生成した例外からもコードを引ける' do
|
|
102
|
+
expect(NeAPIException.new("002004:expired").code).to eq("002004")
|
|
103
|
+
expect(NeAPIException.new("no token!").code).to be_nil
|
|
104
|
+
end
|
|
105
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ne_api
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.24
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Yuuna Kurita
|
|
8
8
|
- Mika Koizumi
|
|
9
|
+
autorequire:
|
|
9
10
|
bindir: bin
|
|
10
11
|
cert_chain: []
|
|
11
|
-
date:
|
|
12
|
+
date: 2026-11-18 00:00:00.000000000 Z
|
|
12
13
|
dependencies:
|
|
13
14
|
- !ruby/object:Gem::Dependency
|
|
14
15
|
name: bundler
|
|
@@ -145,6 +146,7 @@ extra_rdoc_files: []
|
|
|
145
146
|
files:
|
|
146
147
|
- ".env"
|
|
147
148
|
- ".gitignore"
|
|
149
|
+
- BUNDLING_API.md
|
|
148
150
|
- Gemfile
|
|
149
151
|
- LICENSE.txt
|
|
150
152
|
- README.md
|
|
@@ -158,11 +160,14 @@ files:
|
|
|
158
160
|
- lib/ne_api/testing/rspec_helpers.rb
|
|
159
161
|
- lib/ne_api/version.rb
|
|
160
162
|
- ne_api.gemspec
|
|
163
|
+
- spec/bundling_dispatch_spec.rb
|
|
161
164
|
- spec/ne_api_spec.rb
|
|
165
|
+
- spec/token_refresh_spec.rb
|
|
162
166
|
homepage: http://github.com/infinity-octaver/ne_api/
|
|
163
167
|
licenses:
|
|
164
168
|
- MIT
|
|
165
169
|
metadata: {}
|
|
170
|
+
post_install_message:
|
|
166
171
|
rdoc_options: []
|
|
167
172
|
require_paths:
|
|
168
173
|
- lib
|
|
@@ -177,8 +182,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
177
182
|
- !ruby/object:Gem::Version
|
|
178
183
|
version: '0'
|
|
179
184
|
requirements: []
|
|
180
|
-
rubygems_version: 4.
|
|
185
|
+
rubygems_version: 3.4.19
|
|
186
|
+
signing_key:
|
|
181
187
|
specification_version: 4
|
|
182
188
|
summary: Next Engine API for ruby
|
|
183
189
|
test_files:
|
|
190
|
+
- spec/bundling_dispatch_spec.rb
|
|
184
191
|
- spec/ne_api_spec.rb
|
|
192
|
+
- spec/token_refresh_spec.rb
|