yandex-delivery 0.0.2 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +21 -0
- data/README.markdown +254 -0
- data/lib/generators/yandex_delivery/install/templates/yandex_delivery.rb +2 -1
- data/lib/generators/yandex_delivery/install/templates/yandex_delivery.yml +1 -0
- data/lib/yandex-delivery/api_request.rb +9 -4
- data/lib/yandex-delivery/request.rb +8 -4
- data/lib/yandex-delivery/version.rb +1 -1
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 879dec72178429f7404eda56de3ffa39002530a8451a82753ce79e26cb64e38e
|
4
|
+
data.tar.gz: 99cb8023968d3a40c9b03a834a812b418ac410a79287fcfd72a5fe1f36f20493
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0bc51ee3f55b3fbab1c6939fc926435d6e25b27cdc06e80d9691166e579c8207a8c565353081b4aa6999aa746bad7c9ce9689800d0c11f479235e4259d3fb108
|
7
|
+
data.tar.gz: b6ea88f077d315ed92a2be1512c25a4ca202db48ee2c2ef352290004afeee27642e1183cdb75d1cf4cea2a85ad1f6810b9f07c1905d27e17616c904e5fa612fb
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2020 Pavel Osetrov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,254 @@
|
|
1
|
+
# Yandex-delivery
|
2
|
+
|
3
|
+
API wrapper для Яндекс.доставки [API](https://yandex.ru/dev/delivery-3/doc/dg/).
|
4
|
+
|
5
|
+
## Установка Ruby
|
6
|
+
|
7
|
+
$ gem install yandex-delivery
|
8
|
+
|
9
|
+
## Установка Rails
|
10
|
+
|
11
|
+
добавьте в Gemfile:
|
12
|
+
|
13
|
+
gem 'yandex-delivery'
|
14
|
+
|
15
|
+
и запустите `bundle install`.
|
16
|
+
|
17
|
+
Затем:
|
18
|
+
|
19
|
+
rails g yandex_delivery:install
|
20
|
+
|
21
|
+
## Требования
|
22
|
+
|
23
|
+
Необходимо получить [токен авторизации](https://yandex.ru/dev/delivery-3/doc/dg/concepts/access.html).
|
24
|
+
|
25
|
+
## Использование Rails
|
26
|
+
|
27
|
+
В файл `config/yandex_delivery2.yml` вставьте ваши данные из настоек яндекс доставки и токен авторизации
|
28
|
+
|
29
|
+
## Использование Ruby
|
30
|
+
|
31
|
+
Сначала создайте экземпляр объекта `YandexDelivery::Request`:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
delivery = YandexDelivery::Request.new(api_key: "your_access_token")
|
35
|
+
```
|
36
|
+
|
37
|
+
Вы можете изменять `api_key`, `timeout`, `open_timeout`, `faraday_adapter`, `proxy`, `symbolize_keys`, `logger`, и `debug`:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
YandexDelivery::Request.api_key = "your_access_token"
|
41
|
+
YandexDelivery::Request.timeout = 15
|
42
|
+
YandexDelivery::Request.open_timeout = 15
|
43
|
+
YandexDelivery::Request.symbolize_keys = true
|
44
|
+
YandexDelivery::Request.debug = false
|
45
|
+
```
|
46
|
+
|
47
|
+
Либо в файле `config/initializers/yandex_delivery2.rb` для Rails.
|
48
|
+
|
49
|
+
## Debug Logging
|
50
|
+
|
51
|
+
Pass `debug: true` to enable debug logging to STDOUT.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
delivery = YandexDelivery::Request.new(api_key: "your_access_token", debug: true)
|
55
|
+
```
|
56
|
+
|
57
|
+
### Custom logger
|
58
|
+
|
59
|
+
Ruby `Logger.new` is used by default, but it can be overrided using:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
delivery = YandexDelivery::Request.new(api_key: "your_access_token", debug: true, logger: MyLogger.new)
|
63
|
+
```
|
64
|
+
|
65
|
+
Logger can be also set by globally:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
YandexDelivery::Request.logger = MyLogger.new
|
69
|
+
```
|
70
|
+
|
71
|
+
## Примеры
|
72
|
+
|
73
|
+
### Варианты доставки
|
74
|
+
|
75
|
+
#### Поиск вариантов доставки
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
# для Rails senderId указывать не обязательно, в запрос подставится значение из config/yandex_delivery2.yml
|
79
|
+
body = {
|
80
|
+
:to=>{:location=>"Санкт-Петербург"},
|
81
|
+
:dimensions=>{:length=>1, :height=>1, :width=>1, :weight=>1}
|
82
|
+
}
|
83
|
+
|
84
|
+
YandexDelivery::Request.delivery_options.upsert(body: body)
|
85
|
+
```
|
86
|
+
|
87
|
+
#### Поиск пунктов выдачи заказов
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
YandexDelivery::Request.pickup_points.upsert(body: {locationId: 2})
|
91
|
+
```
|
92
|
+
|
93
|
+
### Операции с заказами
|
94
|
+
|
95
|
+
#### Создать черновик заказа
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
response = YandexDelivery::Request.orders.create(body: {deliveryType: 'COURIER'})
|
99
|
+
order_id = response.body
|
100
|
+
```
|
101
|
+
|
102
|
+
#### Обновить черновик заказа
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
YandexDelivery::Request.orders(order_id).upsert(body: {deliveryType: 'PICKUP'})
|
106
|
+
```
|
107
|
+
|
108
|
+
#### Оформить заказ
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
YandexDelivery::Request.orders.submit.create(body: {orderIds: [order_id]})
|
112
|
+
```
|
113
|
+
|
114
|
+
#### Получить данные о заказе
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
response = YandexDelivery::Request.orders(order_id).retrieve
|
118
|
+
order = response.body
|
119
|
+
```
|
120
|
+
|
121
|
+
#### Удалить заказ
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
YandexDelivery::Request.orders(order_id).delete
|
125
|
+
```
|
126
|
+
|
127
|
+
#### Получить ярлык заказа
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
YandexDelivery::Request.orders(order_id).label.retrieve
|
131
|
+
```
|
132
|
+
|
133
|
+
#### Поиск заказов
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
sender_ids = YandexDelivery.senders.map{|sender| sender['id']}
|
137
|
+
YandexDelivery::Request.orders.search.upsert(body: {senderIds: sender_ids, orderIds: [order_id]})
|
138
|
+
```
|
139
|
+
|
140
|
+
#### Получить историю статусов заказа
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
YandexDelivery::Request.orders(order_id).statuses.retrieve
|
144
|
+
```
|
145
|
+
|
146
|
+
#### Получить статус заказов
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
YandexDelivery::Request.orders.status.upsert
|
150
|
+
```
|
151
|
+
|
152
|
+
### Операции с отгрузками
|
153
|
+
|
154
|
+
#### Создать заявку на отгрузку
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
body = {
|
158
|
+
cabinetId: YandexDelivery.client['id'],
|
159
|
+
shipment: {
|
160
|
+
type: "WITHDRAW",
|
161
|
+
date: "2020-10-05",
|
162
|
+
warehouseFrom: YandexDelivery.warehouses.first['id'],
|
163
|
+
warehouseTo: 123,
|
164
|
+
partnerTo: 678
|
165
|
+
},
|
166
|
+
intervalId: 765478,
|
167
|
+
dimensions: {
|
168
|
+
length: 10,
|
169
|
+
width: 15,
|
170
|
+
height: 40,
|
171
|
+
weight: 5.5
|
172
|
+
},
|
173
|
+
courier: {
|
174
|
+
type: "CAR",
|
175
|
+
firstName: "Василий",
|
176
|
+
middleName: "Иванович",
|
177
|
+
lastName: "Пупкин",
|
178
|
+
phone: "+79998887766",
|
179
|
+
carNumber: "о000оо",
|
180
|
+
carBrand: "Maybach"
|
181
|
+
},
|
182
|
+
comment: "comment"
|
183
|
+
}
|
184
|
+
response = YandexDelivery::Request.shipments.application.create(body: body)
|
185
|
+
shipment_id = response.body['id']
|
186
|
+
```
|
187
|
+
|
188
|
+
#### Подтвердить отгрузку
|
189
|
+
|
190
|
+
```ruby
|
191
|
+
body = {
|
192
|
+
cabinetId: YandexDelivery.client['id'],
|
193
|
+
shipmentApplicationIds: [shipment_id]
|
194
|
+
}
|
195
|
+
YandexDelivery::Request.shipments.application.submit.create(body: body)
|
196
|
+
```
|
197
|
+
|
198
|
+
#### Получить список отгрузок
|
199
|
+
|
200
|
+
```ruby
|
201
|
+
body = {
|
202
|
+
cabinetId: YandexDelivery.client['id'],
|
203
|
+
shipmentType: "IMPORT",
|
204
|
+
dateFrom: "2020-10-05",
|
205
|
+
"dateTo": "2020-11-05",
|
206
|
+
"partnerIds":
|
207
|
+
[
|
208
|
+
239847,
|
209
|
+
98234,
|
210
|
+
54968
|
211
|
+
]
|
212
|
+
}
|
213
|
+
YandexDelivery::Request.shipments.search.upsert(body: body)
|
214
|
+
```
|
215
|
+
|
216
|
+
#### Получить интервалы самопривозов
|
217
|
+
|
218
|
+
```ruby
|
219
|
+
warehouse_id = YandexDelivery.warehouses.first['id']
|
220
|
+
YandexDelivery::Request.shipments.intervals.import.retrieve(params: {warehouseId: warehouse_id, date: '2020-10-06'})
|
221
|
+
```
|
222
|
+
|
223
|
+
#### Получить интервалы заборов
|
224
|
+
|
225
|
+
```ruby
|
226
|
+
YandexDelivery::Request.shipments.intervals.withdraw.retrieve(params: {partnerId: 106, date: '2020-10-06'})
|
227
|
+
```
|
228
|
+
|
229
|
+
#### Получить акт передачи заказов
|
230
|
+
|
231
|
+
```ruby
|
232
|
+
YandexDelivery::Request.shipments.applications(shipment_id).act.retrieve(params: {cabinetId: YandexDelivery.client['id']})
|
233
|
+
```
|
234
|
+
|
235
|
+
### Справочные данные
|
236
|
+
|
237
|
+
#### Получить полный адрес
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
YandexDelivery::Request.location.retrieve(params: {term: 'Санкт-Петербург'})
|
241
|
+
```
|
242
|
+
|
243
|
+
#### Получить индекс по адресу
|
244
|
+
|
245
|
+
```ruby
|
246
|
+
response = YandexDelivery::Request.location.postal_code.retrieve(params: {address: 'Санкт-Петербург, ул. Профессора Попова, д. 37Щ, БЦ "Сенатор"'})
|
247
|
+
postal_code = response.body.first[:postalCode]
|
248
|
+
```
|
249
|
+
|
250
|
+
#### Получить список служб доставки
|
251
|
+
|
252
|
+
```ruby
|
253
|
+
YandexDelivery::Request.delivery_services.retrieve(params: {cabinetId: YandexDelivery.client['id']})
|
254
|
+
```
|
@@ -1,10 +1,11 @@
|
|
1
1
|
require 'yandex_delivery'
|
2
2
|
|
3
3
|
YandexDelivery.setup do |config|
|
4
|
-
if File.exist?('config/
|
4
|
+
if File.exist?('config/yandex_delivery2.yml')
|
5
5
|
template = ERB.new(File.new('config/yandex_delivery2.yml').read)
|
6
6
|
processed = YAML.safe_load(template.result(binding))
|
7
7
|
|
8
|
+
config::Request.app_id = processed['YANDEX_DELIVERY_APP_ID']
|
8
9
|
config::Request.api_key = processed['YANDEX_DELIVERY_ACCESS_TOKEN']
|
9
10
|
config::Request.timeout = 15
|
10
11
|
config::Request.open_timeout = 15
|
@@ -81,6 +81,10 @@ module YandexDelivery
|
|
81
81
|
@request_builder.api_key
|
82
82
|
end
|
83
83
|
|
84
|
+
def app_id
|
85
|
+
@request_builder.app_id
|
86
|
+
end
|
87
|
+
|
84
88
|
def api_endpoint
|
85
89
|
@request_builder.api_endpoint
|
86
90
|
end
|
@@ -140,12 +144,11 @@ module YandexDelivery
|
|
140
144
|
if request
|
141
145
|
request.params.merge!(params) if params
|
142
146
|
request.headers['Content-Type'] = 'application/json'
|
143
|
-
request.headers['Authorization'] = "OAuth #{self.api_key}"
|
147
|
+
request.headers['Authorization'] = "OAuth oauth_token=\"#{self.api_key}\", oauth_client_id=\"#{self.app_id}\""
|
144
148
|
request.headers.merge!(headers) if headers
|
145
149
|
request.body = body if body
|
146
150
|
request.options.timeout = self.timeout
|
147
151
|
request.options.open_timeout = self.open_timeout
|
148
|
-
p request
|
149
152
|
end
|
150
153
|
end
|
151
154
|
|
@@ -179,10 +182,12 @@ module YandexDelivery
|
|
179
182
|
end
|
180
183
|
|
181
184
|
def validate_api_key
|
182
|
-
|
183
|
-
unless api_key && (api_key["-"] || self.api_endpoint)
|
185
|
+
unless self.api_key
|
184
186
|
raise YandexDelivery::YandexDeliveryError, "You must set an api_key prior to making a call"
|
185
187
|
end
|
188
|
+
unless self.app_id
|
189
|
+
raise YandexDelivery::YandexDeliveryError, "You must set an app_id prior to making a call"
|
190
|
+
end
|
186
191
|
end
|
187
192
|
|
188
193
|
def api_url
|
@@ -1,14 +1,16 @@
|
|
1
1
|
module YandexDelivery
|
2
2
|
class Request
|
3
|
-
attr_accessor :api_key, :api_endpoint, :timeout, :open_timeout, :proxy, :faraday_adapter, :symbolize_keys, :debug, :logger
|
3
|
+
attr_accessor :api_key, :app_id, :api_endpoint, :timeout, :open_timeout, :proxy, :faraday_adapter, :symbolize_keys, :debug, :logger
|
4
4
|
|
5
5
|
DEFAULT_TIMEOUT = 60
|
6
6
|
DEFAULT_OPEN_TIMEOUT = 60
|
7
7
|
|
8
|
-
def initialize(api_key: nil, api_endpoint: nil, timeout: nil, open_timeout: nil, proxy: nil, faraday_adapter: nil, symbolize_keys: false, debug: false, logger: nil)
|
8
|
+
def initialize(api_key: nil, app_id: nil, api_endpoint: nil, timeout: nil, open_timeout: nil, proxy: nil, faraday_adapter: nil, symbolize_keys: false, debug: false, logger: nil)
|
9
9
|
@path_parts = []
|
10
10
|
@api_key = api_key || self.class.api_key || ENV['YANDEX_DELIVERY_ACCESS_TOKEN']
|
11
11
|
@api_key = @api_key.strip if @api_key
|
12
|
+
@app_id = app_id || self.class.app_id || ENV['YANDEX_DELIVERY_APP_ID']
|
13
|
+
@app_id = @app_id.strip if @app_id
|
12
14
|
@api_endpoint = api_endpoint || self.class.api_endpoint
|
13
15
|
@timeout = timeout || self.class.timeout || DEFAULT_TIMEOUT
|
14
16
|
@open_timeout = open_timeout || self.class.open_timeout || DEFAULT_OPEN_TIMEOUT
|
@@ -80,10 +82,12 @@ module YandexDelivery
|
|
80
82
|
end
|
81
83
|
|
82
84
|
class << self
|
83
|
-
attr_accessor :api_key, :timeout, :open_timeout, :api_endpoint, :proxy, :faraday_adapter, :symbolize_keys, :debug, :logger
|
85
|
+
attr_accessor :api_key, :app_id, :timeout, :open_timeout, :api_endpoint, :proxy, :faraday_adapter, :symbolize_keys, :debug, :logger
|
84
86
|
|
85
87
|
def method_missing(sym, *args, &block)
|
86
|
-
new(api_key: self.api_key,
|
88
|
+
new(api_key: self.api_key, app_id: self.app_id, api_endpoint: self.api_endpoint, timeout: self.timeout,
|
89
|
+
open_timeout: self.open_timeout, faraday_adapter: self.faraday_adapter, symbolize_keys: self.symbolize_keys,
|
90
|
+
debug: self.debug, proxy: self.proxy, logger: self.logger).send(sym, *args, &block)
|
87
91
|
end
|
88
92
|
|
89
93
|
def respond_to_missing?(method_name, include_private = false)
|
metadata
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yandex-delivery
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Osetrov
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
date: 2020-08-11 00:00:00.000000000 Z
|
@@ -44,6 +44,8 @@ executables: []
|
|
44
44
|
extensions: []
|
45
45
|
extra_rdoc_files: []
|
46
46
|
files:
|
47
|
+
- LICENSE
|
48
|
+
- README.markdown
|
47
49
|
- lib/generators/yandex_delivery/install/USAGE
|
48
50
|
- lib/generators/yandex_delivery/install/install_generator.rb
|
49
51
|
- lib/generators/yandex_delivery/install/templates/yandex_delivery.rb
|
@@ -59,7 +61,7 @@ homepage: https://github.com/osetrov/yandex-delivery
|
|
59
61
|
licenses:
|
60
62
|
- MIT
|
61
63
|
metadata: {}
|
62
|
-
post_install_message:
|
64
|
+
post_install_message:
|
63
65
|
rdoc_options: []
|
64
66
|
require_paths:
|
65
67
|
- lib
|
@@ -75,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
77
|
version: '0'
|
76
78
|
requirements: []
|
77
79
|
rubygems_version: 3.1.4
|
78
|
-
signing_key:
|
80
|
+
signing_key:
|
79
81
|
specification_version: 4
|
80
82
|
summary: Yandex delivery
|
81
83
|
test_files: []
|