ozon-logistics 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 51a82b34eec6a8509a958d4f9a2a2c722aec9327a3f7403a0a87c042e94e556a
4
+ data.tar.gz: 6099311d8a81268a660d8446c388f79a8a1d20a34411120e7c5623315dd34ef4
5
+ SHA512:
6
+ metadata.gz: bab04aed9fb8a470f75c75e27b0838e7d851d08fe110d932d596f51e65b8fac9ae24d49c0e0a7d19a1315c1d5b8987644ea0a209ebb266d44da2399753e1e1f7
7
+ data.tar.gz: d4ce55c2c8418fd53ee275a6de9535488c7262d3225440ff28fecd3da8d3caf680145daa49db3793ec3a48b1ec46c55892714a71847d05df77703d2d3c3f614e
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,307 @@
1
+ # Ozon-logistics
2
+
3
+ API wrapper для Ozon.Logistics [API](https://api-stg.ozonru.me/principal-integration-api/swagger/index.html).
4
+
5
+ ## Установка Ruby
6
+
7
+ $ gem install ozon-logistics
8
+
9
+ ## Установка Rails
10
+
11
+ добавьте в Gemfile:
12
+
13
+ gem 'ozon-logistics'
14
+
15
+ и запустите `bundle install`.
16
+
17
+ Затем:
18
+
19
+ rails g ozon-logistics:install
20
+
21
+ ## Требования
22
+
23
+ Необходимо запросить CLIENT_ID и CLIENT_SECRET для production
24
+
25
+ ## Использование Rails
26
+
27
+ В файл `config/ozon_logistics.yml` вставьте ваши данные
28
+
29
+ ## Использование Ruby
30
+
31
+ Сначала cгенерируйте access_token
32
+ Затем создайте экземпляр объекта `OzonLogistics::Request`:
33
+
34
+ ```ruby
35
+ access_token = OzonLogistics.generate_access_token('ApiTest_11111111-1111-1111-1111-111111111111', 'SRYksX3PBPUYj73A6cNqbQYRSaYNpjSodIMeWoSCQ8U=', 'client_credentials')
36
+ delivery = OzonLogistics::Request.new(access_token: access_token)
37
+ ```
38
+
39
+ Вы можете изменять `access_token`, `timeout`, `open_timeout`, `faraday_adapter`, `proxy`, `symbolize_keys`, `logger`, и `debug`:
40
+
41
+ ```ruby
42
+ OzonLogistics::Request.access_token = "your_access_token"
43
+ OzonLogistics::Request.timeout = 15
44
+ OzonLogistics::Request.open_timeout = 15
45
+ OzonLogistics::Request.symbolize_keys = true
46
+ OzonLogistics::Request.debug = false
47
+ ```
48
+
49
+ Либо в файле `config/initializers/ozon_logistics.rb` для Rails.
50
+
51
+ ## Debug Logging
52
+
53
+ Pass `debug: true` to enable debug logging to STDOUT.
54
+
55
+ ```ruby
56
+ delivery = OzonLogistics::Request.new(access_token: "your_access_token", debug: true)
57
+ ```
58
+
59
+ ### Custom logger
60
+
61
+ Ruby `Logger.new` is used by default, but it can be overrided using:
62
+
63
+ ```ruby
64
+ delivery = OzonLogistics::Request.new(access_token: "your_access_token", debug: true, logger: MyLogger.new)
65
+ ```
66
+
67
+ Logger can be also set by globally:
68
+
69
+ ```ruby
70
+ OzonLogistics::Request.logger = MyLogger.new
71
+ ```
72
+
73
+ ## Примеры (для версии от 26.04.2021)
74
+
75
+ ### Delivery
76
+
77
+ #### Получение списка городов доставки
78
+
79
+ ```ruby
80
+ response = OzonLogistics::Request.delivery.cities.retrieve
81
+ cities = response.body[:data]
82
+ ```
83
+
84
+ #### Получение информации о способе доставки по адресу
85
+
86
+ ```ruby
87
+
88
+ body = {
89
+ deliveryType: 'Courier',
90
+ address: 'Санкт-Петербург, ул. Профессора Попова, д. 37Щ',
91
+ radius: 5,
92
+ packages: [{
93
+ count: 1,
94
+ dimensions: {length: 1, height: 1, width: 1, weight: 1},
95
+ price: 999
96
+ }]
97
+ }
98
+
99
+ response = OzonLogistics::Request.delivery.variants.byaddress.create(body: body)
100
+ variants = response.body[:data]
101
+ ```
102
+
103
+ #### Получение ИД способа доставки по адресу
104
+
105
+ ```ruby
106
+ body = {
107
+ "deliveryTypes": [
108
+ "Courier"
109
+ ],
110
+ address: 'Санкт-Петербург, ул. Профессора Попова, д. 37Щ',
111
+ radius: 5,
112
+ packages: [{
113
+ count: 1,
114
+ dimensions: {length: 1, height: 1, width: 1, weight: 1},
115
+ price: 999
116
+ }]
117
+ }
118
+
119
+ response = OzonLogistics::Request.delivery.variants.byaddress.short.create(body: body)
120
+ delivery_variant_ids = response.body[:deliveryVariantIds]
121
+ ```
122
+
123
+ #### Получение списка способов доставки
124
+ ```ruby
125
+ params = {
126
+ cityName: 'Санкт-Петербург',
127
+ payloadIncludes: {
128
+ includeWorkingHours: true,
129
+ includePostalCode: true
130
+ },
131
+ pagination: {
132
+ size: 100,
133
+ token: 0
134
+ }
135
+ }
136
+ response = OzonLogistics::Request.delivery.variants.retrieve(params: params)
137
+ variants = response.body[:data]
138
+ first_variant_id = variants.first[:id]
139
+ ```
140
+
141
+ #### Список складов передачи отправлений (OZON)
142
+ ```ruby
143
+ response = OzonLogistics::Request.delivery.from_places.retrieve
144
+ places = response.body[:data]
145
+ first_place_id = places.first[:id]
146
+ ```
147
+ #### Расчёт стоимости доставки
148
+ ```ruby
149
+ params = {
150
+ deliveryVariantId: first_variant_id,
151
+ weight: 1,
152
+ fromPlaceId: first_place_id
153
+ }
154
+ response = OzonLogistics::Request.delivery.calculate.retrieve(params: params)
155
+ amount = response.body[:amount]
156
+ ```
157
+
158
+ ### Order
159
+ #### Метод загрузки многокоробочных отправлений
160
+ ```ruby
161
+ body = {
162
+ orderNumber: '1',
163
+ buyer: {
164
+ name: 'Павел',
165
+ phone: '+71234567890',
166
+ email: 'test@test.ru',
167
+ type: 'NaturalPerson'
168
+ },
169
+ recipient: {
170
+ name: 'Павел',
171
+ phone: '+71234567890',
172
+ email: 'test@test.ru',
173
+ type: 'NaturalPerson'
174
+ },
175
+ deliveryInformation: {
176
+ deliveryVariantId: first_variant_id,
177
+ address: 'Санкт-Петербург, ул. Профессора Попова, д. 37Щ'
178
+ },
179
+ payment: {
180
+ type: 'FullPrepayment',
181
+ prepaymentAmount: 777,
182
+ recipientPaymentAmount: 0,
183
+ deliveryPrice: 0
184
+ },
185
+ packages: [
186
+ {
187
+ packageNumber: 1,
188
+ dimensions: {length: 1, height: 1, width: 1, weight: 1},
189
+ barCode: '76384576384756'
190
+ }
191
+ ],
192
+ orderLines: [
193
+ lineNumber: 1,
194
+ articleNumber: '11398',
195
+ name: 'Сетевое зарядное устройство USB-C + USB A, PD 3.0, QC 3.0, 20Вт',
196
+ weight: 1,
197
+ sellingPrice: 1190,
198
+ estimatedPrice: 1190,
199
+ quantity: 1,
200
+ resideInPackages: ['1']
201
+ ]
202
+ }
203
+ response = OzonLogistics::Request.order.create(body: body)
204
+ order = response[:data]
205
+ ```
206
+ ### Tariff
207
+ #### Получение списка тарифов
208
+ ```ruby
209
+ response = OzonLogistics::Request.tariff.list.retrieve
210
+ tariff = response.body[:items]
211
+ ```
212
+
213
+ ### Manifest
214
+ #### Получение списка отправлений
215
+ ```ruby
216
+ params = {
217
+ pagination: {
218
+ size: 100,
219
+ token: 0
220
+ }
221
+ }
222
+ response = OzonLogistics::Request.manifest.unprocessed.retrieve(params: params)
223
+ tracks = response.body[:data]
224
+ track_id = tracks.first[:posting][:id]
225
+ ```
226
+
227
+ ### Posting
228
+ #### Получение этикетки отправления
229
+ ```ruby
230
+ params = {
231
+ postingId: track_id
232
+ }
233
+ response = OzonLogistics::Request.posting.ticket.retrieve(params: params)
234
+ barcode = response.body[:barcode]
235
+ ```
236
+
237
+ ### Document
238
+ #### Создание электронных документов
239
+ ```ruby
240
+ body = {
241
+ postingIds: [track_id]
242
+ }
243
+ response = OzonLogistics::Request.document.create?.create(body: body)
244
+ document = response.body
245
+ document_number = document[:documentId]
246
+ ```
247
+ #### Получение списка созданных документов
248
+ ```ruby
249
+ params = {
250
+ pagination: {
251
+ size: 100,
252
+ token: 0
253
+ },
254
+ number: document_number,
255
+ dateFrom: DateTime.now - 7.days,
256
+ dateTo: DateTime.now
257
+ }
258
+ response = OzonLogistics::Request.document.list.retrieve(params: params)
259
+ list = response.body[:data]
260
+ ```
261
+ #### Запрос печатной формы документа (Base64)
262
+ ```ruby
263
+ params = {
264
+ id: document_number,
265
+ type: 'T12Sales',
266
+ format: 'PDF'
267
+ }
268
+ response = OzonLogistics::Request.document.retrieve(params: params)
269
+ document = response.body[:data]
270
+ ```
271
+
272
+ ### Tracking
273
+ #### Трекинг по номеру отправления
274
+ ```ruby
275
+ params = {
276
+ postingNumber: 1
277
+ }
278
+ response = OzonLogistics::Request.tracking.bypostingnumber.retrieve(params: params)
279
+ bypostingnumber = response.body
280
+ ```
281
+ #### Трекинг по штрихкоду отправления
282
+ ```ruby
283
+ params = {
284
+ postingBarcode: '76384576384756'
285
+ }
286
+ response = OzonLogistics::Request.tracking.bybarcode.retrieve(params: params)
287
+ bybarcode = response.body
288
+ ```
289
+ #### Трекинг группы отправлений
290
+ ```ruby
291
+ body = {
292
+ articles: [
293
+ "1"
294
+ ]
295
+ }
296
+ response = OzonLogistics::Request.tracking.list.create(body: body)
297
+ list = response.body
298
+ ```
299
+
300
+ #### Получение детальной информации по отправлению
301
+ ```ruby
302
+ params = {
303
+ postingBarcode: '76384576384756'
304
+ }
305
+ response = OzonLogistics::Request.tracking.article.retrieve(params: params)
306
+ article = response.body
307
+ ```
@@ -0,0 +1,2 @@
1
+ Description:
2
+ ozon_logistics:install
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ module OzonLogistics
4
+ class InstallGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('templates', __dir__)
6
+
7
+ def generate_install
8
+ copy_file 'ozon_logistics.yml', 'config/ozon_logistics.yml'
9
+ copy_file 'ozon_logistics.rb', 'config/initializers/ozon_logistics.rb'
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,17 @@
1
+ require 'ozon-logistics'
2
+
3
+ OzonLogistics.setup do |config|
4
+ if File.exist?('config/ozon_logistics.yml')
5
+ processed = YAML.load_file('config/ozon_logistics.yml')[Rails.env]
6
+
7
+ config::Request.access_token = ENV['OZON_LOGISTICS_ACCESS_TOKEN'] || OzonLogistics.generate_access_token.try(:dig, "access_token")
8
+ config::Request.timeout = 15
9
+ config::Request.open_timeout = 15
10
+ config::Request.symbolize_keys = true
11
+ config::Request.debug = false
12
+
13
+ processed.each do |k, v|
14
+ config::register k.underscore.to_sym, v
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ defaults: &defaults
2
+ GRANT_TYPE: 'client_credentials'
3
+ CLIENT_ID: 'ApiTest_11111111-1111-1111-1111-111111111111'
4
+ CLIENT_SECRET: 'SRYksX3PBPUYj73A6cNqbQYRSaYNpjSodIMeWoSCQ8U='
5
+ URL_TOKEN: 'https://api-stg.ozonru.me/principal-auth-api/connect/token'
6
+ HOST: 'https://api-stg.ozonru.me/principal-integration-api'
7
+ production:
8
+ <<: *defaults
9
+ URL_TOKEN: 'https://api.ozon.ru/principal-auth-api/connect/token'
10
+ HOST: 'https://api.ozon.ru/principal-integration-api'
11
+ CLIENT_ID: ''
12
+ CLIENT_SECRET: ''
13
+ development:
14
+ <<: *defaults
15
+ test:
16
+ <<: *defaults
@@ -0,0 +1,59 @@
1
+ require 'ozon-logistics/ozon_logistics_error'
2
+ require 'ozon-logistics/ozon_error'
3
+ require 'ozon-logistics/request'
4
+ require 'ozon-logistics/api_request'
5
+ require 'ozon-logistics/response'
6
+
7
+ module OzonLogistics
8
+ class << self
9
+
10
+ def generate_access_token(client_id=OzonLogistics.client_id, client_secret=OzonLogistics.client_secret, grant_type=OzonLogistics.grant_type)
11
+ response = Faraday.post(OzonLogistics.url_token, "grant_type=#{grant_type}&client_id=#{client_id}&client_secret=#{client_secret}")
12
+ JSON.parse(response.body)
13
+ end
14
+
15
+ def setup
16
+ yield self
17
+ end
18
+
19
+ def register(name, value, type = nil)
20
+ cattr_accessor "#{name}_setting".to_sym
21
+
22
+ add_reader(name)
23
+ add_writer(name, type)
24
+ send "#{name}=", value
25
+ end
26
+
27
+ def add_reader(name)
28
+ define_singleton_method(name) do |*args|
29
+ send("#{name}_setting").value(*args)
30
+ end
31
+ end
32
+
33
+ def add_writer(name, type)
34
+ define_singleton_method("#{name}=") do |value|
35
+ send("#{name}_setting=", DynamicSetting.build(value, type))
36
+ end
37
+ end
38
+ end
39
+
40
+ class DynamicSetting
41
+ def self.build(setting, type)
42
+ (type ? klass(type) : self).new(setting)
43
+ end
44
+
45
+ def self.klass(type)
46
+ klass = "#{type.to_s.camelcase}Setting"
47
+ raise ArgumentError, "Unknown type: #{type}" unless OzonLogistics.const_defined?(klass)
48
+ OzonLogistics.const_get(klass)
49
+ end
50
+
51
+ def initialize(setting)
52
+ @setting = setting
53
+ end
54
+
55
+ def value(*_args)
56
+ @setting
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,193 @@
1
+ module OzonLogistics
2
+ class APIRequest
3
+
4
+ def initialize(builder: nil)
5
+ @request_builder = builder
6
+ end
7
+
8
+ def post(params: nil, headers: nil, body: {})
9
+ validate_access_token
10
+
11
+ begin
12
+ response = self.rest_client.post do |request|
13
+ configure_request(request: request, params: params, headers: headers, body: MultiJson.dump(body))
14
+ end
15
+ parse_response(response)
16
+ rescue => e
17
+ handle_error(e)
18
+ end
19
+ end
20
+
21
+ def patch(params: nil, headers: nil, body: {})
22
+ validate_access_token
23
+
24
+ begin
25
+ response = self.rest_client.patch do |request|
26
+ body[:senderId] = OzonLogistics.senders.first["id"] if body[:senderId].nil?
27
+ configure_request(request: request, params: params, headers: headers, body: MultiJson.dump(body))
28
+ end
29
+ parse_response(response)
30
+ rescue => e
31
+ handle_error(e)
32
+ end
33
+ end
34
+
35
+ def put(params: nil, headers: nil, body: {})
36
+ validate_access_token
37
+
38
+ begin
39
+ response = self.rest_client.put do |request|
40
+ configure_request(request: request, params: params, headers: headers, body: MultiJson.dump(body))
41
+ end
42
+ parse_response(response)
43
+ rescue => e
44
+ handle_error(e)
45
+ end
46
+ end
47
+
48
+ def get(params: nil, headers: nil)
49
+ validate_access_token
50
+
51
+ begin
52
+ response = self.rest_client.get do |request|
53
+ configure_request(request: request, params: params, headers: headers)
54
+ end
55
+ parse_response(response)
56
+ rescue => e
57
+ handle_error(e)
58
+ end
59
+ end
60
+
61
+ def delete(params: nil, headers: nil)
62
+ validate_access_token
63
+
64
+ begin
65
+ response = self.rest_client.delete do |request|
66
+ configure_request(request: request, params: params, headers: headers)
67
+ end
68
+ parse_response(response)
69
+ rescue => e
70
+ handle_error(e)
71
+ end
72
+ end
73
+
74
+ protected
75
+
76
+ # Convenience accessors
77
+
78
+ def access_token
79
+ @request_builder.access_token
80
+ end
81
+
82
+ def api_endpoint
83
+ @request_builder.api_endpoint
84
+ end
85
+
86
+ def timeout
87
+ @request_builder.timeout
88
+ end
89
+
90
+ def open_timeout
91
+ @request_builder.open_timeout
92
+ end
93
+
94
+ def proxy
95
+ @request_builder.proxy
96
+ end
97
+
98
+ def adapter
99
+ @request_builder.faraday_adapter
100
+ end
101
+
102
+ def symbolize_keys
103
+ @request_builder.symbolize_keys
104
+ end
105
+
106
+ # Helpers
107
+
108
+ def handle_error(error)
109
+ error_params = {}
110
+
111
+ begin
112
+ if error.is_a?(Faraday::ClientError) && error.response
113
+ error_params[:status_code] = error.response[:status]
114
+ error_params[:raw_body] = error.response[:body]
115
+
116
+ parsed_response = MultiJson.load(error.response[:body], symbolize_keys: symbolize_keys)
117
+
118
+ if parsed_response
119
+ error_params[:body] = parsed_response
120
+
121
+ title_key = symbolize_keys ? :title : "title"
122
+ detail_key = symbolize_keys ? :detail : "detail"
123
+
124
+ error_params[:title] = parsed_response[title_key] if parsed_response[title_key]
125
+ error_params[:detail] = parsed_response[detail_key] if parsed_response[detail_key]
126
+ end
127
+
128
+ end
129
+ rescue MultiJson::ParseError
130
+ end
131
+
132
+ error_to_raise = OzonError.new(error.message, error_params)
133
+
134
+ raise error_to_raise
135
+ end
136
+
137
+ def configure_request(request: nil, params: nil, headers: nil, body: nil)
138
+ if request
139
+ request.params.merge!(params) if params
140
+ request.headers['Content-Type'] = 'application/json'
141
+ request.headers['Authorization'] = "Bearer #{self.access_token}"
142
+ request.headers.merge!(headers) if headers
143
+ request.body = body if body
144
+ request.options.timeout = self.timeout
145
+ request.options.open_timeout = self.open_timeout
146
+ end
147
+ end
148
+
149
+ def rest_client
150
+ client = Faraday.new(self.api_url, proxy: self.proxy, ssl: { version: "TLSv1_2" }) do |faraday|
151
+ faraday.response :raise_error
152
+ faraday.adapter adapter
153
+ if @request_builder.debug
154
+ faraday.response :logger, @request_builder.logger, bodies: true
155
+ end
156
+ end
157
+ client
158
+ end
159
+
160
+ def parse_response(response)
161
+ parsed_response = nil
162
+
163
+ if response.body && !response.body.empty?
164
+ begin
165
+ headers = response.headers
166
+ body = MultiJson.load(response.body, symbolize_keys: symbolize_keys)
167
+ parsed_response = Response.new(headers: headers, body: body)
168
+ rescue MultiJson::ParseError
169
+ error_params = { title: "UNPARSEABLE_RESPONSE", status_code: 500 }
170
+ error = OzonError.new("Unparseable response: #{response.body}", error_params)
171
+ raise error
172
+ end
173
+ end
174
+
175
+ parsed_response
176
+ end
177
+
178
+ def validate_access_token
179
+ access_token = self.access_token
180
+ unless access_token && (access_token["-"] || self.api_endpoint)
181
+ raise OzonLogistics::OzonLogisticsError, "You must set an access_token prior to making a call"
182
+ end
183
+ end
184
+
185
+ def api_url
186
+ base_api_url + @request_builder.path
187
+ end
188
+
189
+ def base_api_url
190
+ "#{OzonLogistics.host}/v1/"
191
+ end
192
+ end
193
+ end
@@ -0,0 +1,29 @@
1
+ module OzonLogistics
2
+ class OzonError < StandardError
3
+ attr_reader :title, :detail, :body, :raw_body, :status_code
4
+
5
+ def initialize(message = "", params = {})
6
+ @title = params[:title]
7
+ @detail = params[:detail]
8
+ @body = params[:body]
9
+ @raw_body = params[:raw_body]
10
+ @status_code = params[:status_code]
11
+
12
+ super(message)
13
+ end
14
+
15
+ def to_s
16
+ super + " " + instance_variables_to_s
17
+ end
18
+
19
+ private
20
+
21
+ def instance_variables_to_s
22
+ [:title, :detail, :body, :raw_body, :status_code].map do |attr|
23
+ attr_value = send(attr)
24
+
25
+ "@#{attr}=#{attr_value.inspect}"
26
+ end.join(", ")
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module OzonLogistics
2
+ class OzonLogisticsError < StandardError; end
3
+ end
@@ -0,0 +1,95 @@
1
+ module OzonLogistics
2
+ class Request
3
+ attr_accessor :access_token, :api_endpoint, :timeout, :open_timeout, :proxy, :faraday_adapter, :symbolize_keys, :debug, :logger, :test
4
+
5
+ DEFAULT_TIMEOUT = 60
6
+ DEFAULT_OPEN_TIMEOUT = 60
7
+
8
+ def initialize(access_token: nil, api_endpoint: nil, timeout: nil, open_timeout: nil, proxy: nil, faraday_adapter: nil, symbolize_keys: false, debug: false, logger: nil, test: false)
9
+ @path_parts = []
10
+ @access_token = access_token || self.class.access_token || OzonLogistics.generate_access_token.try(:dig, "access_token")
11
+ @access_token = @access_token.strip if @access_token
12
+ @api_endpoint = api_endpoint || self.class.api_endpoint
13
+ @timeout = timeout || self.class.timeout || DEFAULT_TIMEOUT
14
+ @open_timeout = open_timeout || self.class.open_timeout || DEFAULT_OPEN_TIMEOUT
15
+ @proxy = proxy || self.class.proxy || ENV['OZON_LOGISTICS_PROXY']
16
+ @faraday_adapter = faraday_adapter || self.class.faraday_adapter || Faraday.default_adapter
17
+ @symbolize_keys = symbolize_keys || self.class.symbolize_keys || false
18
+ @debug = debug || self.class.debug || false
19
+ @test = test || self.class.test || false
20
+ @logger = logger || self.class.logger || ::Logger.new(STDOUT)
21
+ end
22
+
23
+ def method_missing(method, *args)
24
+ # To support underscores, we replace them with hyphens when calling the API
25
+ @path_parts << method.to_s.gsub("_", "-").downcase
26
+ @path_parts << args if args.length > 0
27
+ @path_parts.flatten!
28
+ self
29
+ end
30
+
31
+ def respond_to_missing?(method_name, include_private = false)
32
+ true
33
+ end
34
+
35
+ def send(*args)
36
+ if args.length == 0
37
+ method_missing(:send, args)
38
+ else
39
+ __send__(*args)
40
+ end
41
+ end
42
+
43
+ def path
44
+ @path_parts.join('/')
45
+ end
46
+
47
+ def create(params: nil, headers: nil, body: {})
48
+ APIRequest.new(builder: self).post(params: params, headers: headers, body: body)
49
+ ensure
50
+ reset
51
+ end
52
+
53
+ def update(params: nil, headers: nil, body: {})
54
+ APIRequest.new(builder: self).patch(params: params, headers: headers, body: body)
55
+ ensure
56
+ reset
57
+ end
58
+
59
+ def upsert(params: nil, headers: nil, body: {})
60
+ APIRequest.new(builder: self).put(params: params, headers: headers, body: body)
61
+ ensure
62
+ reset
63
+ end
64
+
65
+ def retrieve(params: nil, headers: nil)
66
+ APIRequest.new(builder: self).get(params: params, headers: headers)
67
+ ensure
68
+ reset
69
+ end
70
+
71
+ def delete(params: nil, headers: nil)
72
+ APIRequest.new(builder: self).delete(params: params, headers: headers)
73
+ ensure
74
+ reset
75
+ end
76
+
77
+ protected
78
+
79
+ def reset
80
+ @path_parts = []
81
+ end
82
+
83
+ class << self
84
+ attr_accessor :access_token, :timeout, :open_timeout, :api_endpoint, :proxy, :faraday_adapter, :symbolize_keys, :debug, :logger, :test
85
+
86
+ def method_missing(sym, *args, &block)
87
+ new(access_token: self.access_token, api_endpoint: self.api_endpoint, timeout: self.timeout, open_timeout: self.open_timeout, faraday_adapter: self.faraday_adapter, symbolize_keys: self.symbolize_keys, debug: self.debug, proxy: self.proxy, logger: self.logger, test: self.test).send(sym, *args, &block)
88
+ end
89
+
90
+ def respond_to_missing?(method_name, include_private = false)
91
+ true
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,10 @@
1
+ module OzonLogistics
2
+ class Response
3
+ attr_accessor :body, :headers
4
+
5
+ def initialize(body: {}, headers: {})
6
+ @body = body
7
+ @headers = headers
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module OzonLogistics
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ozon-logistics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pavel Osetrov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-04-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.16.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.16.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.11.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.11.0
41
+ description: ''
42
+ email: pavel.osetrov@me.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - LICENSE
48
+ - README.markdown
49
+ - lib/generators/ozon_logistics/install/USAGE
50
+ - lib/generators/ozon_logistics/install/install_generator.rb
51
+ - lib/generators/ozon_logistics/install/templates/ozon_logistics.rb
52
+ - lib/generators/ozon_logistics/install/templates/ozon_logistics.yml
53
+ - lib/ozon-logistics.rb
54
+ - lib/ozon-logistics/api_request.rb
55
+ - lib/ozon-logistics/ozon_error.rb
56
+ - lib/ozon-logistics/ozon_logistics_error.rb
57
+ - lib/ozon-logistics/request.rb
58
+ - lib/ozon-logistics/response.rb
59
+ - lib/ozon-logistics/version.rb
60
+ homepage: https://github.com/osetrov/ozon-logistics
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 2.3.8
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubygems_version: 3.1.4
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Ozon logistics
83
+ test_files: []