cdek_api_client 0.1.0 → 0.2.0
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/README.md +72 -14
- data/lib/cdek_api_client/api/location.rb +74 -0
- data/lib/cdek_api_client/api/order.rb +43 -0
- data/lib/cdek_api_client/api/tariff.rb +23 -0
- data/lib/cdek_api_client/api/track_order.rb +48 -0
- data/lib/cdek_api_client/api/webhook.rb +41 -0
- data/lib/cdek_api_client/client.rb +98 -71
- data/lib/cdek_api_client/entities/currency_mapper.rb +6 -0
- data/lib/cdek_api_client/entities/item.rb +15 -0
- data/lib/cdek_api_client/entities/location.rb +11 -0
- data/lib/cdek_api_client/entities/order_data.rb +20 -0
- data/lib/cdek_api_client/entities/package.rb +15 -0
- data/lib/cdek_api_client/entities/payment.rb +11 -1
- data/lib/cdek_api_client/entities/recipient.rb +11 -0
- data/lib/cdek_api_client/entities/sender.rb +11 -0
- data/lib/cdek_api_client/entities/service.rb +11 -0
- data/lib/cdek_api_client/entities/tariff_data.rb +15 -0
- data/lib/cdek_api_client/entities/validatable.rb +88 -7
- data/lib/cdek_api_client/entities/webhook.rb +11 -0
- data/lib/cdek_api_client/version.rb +1 -1
- data/lib/cdek_api_client.rb +41 -13
- metadata +8 -51
- data/lib/cdek_api_client/location.rb +0 -32
- data/lib/cdek_api_client/order.rb +0 -43
- data/lib/cdek_api_client/recipient.rb +0 -37
- data/lib/cdek_api_client/tariff.rb +0 -32
- data/lib/cdek_api_client/track_order.rb +0 -29
- data/lib/cdek_api_client/webhook.rb +0 -54
@@ -1,43 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'cdek_api_client/entities/order_data'
|
4
|
-
|
5
|
-
module CDEKApiClient
|
6
|
-
class Order
|
7
|
-
BASE_URL = ENV.fetch('CDEK_API_URL', 'https://api.edu.cdek.ru/v2')
|
8
|
-
ORDERS_URL = "#{BASE_URL}/orders".freeze
|
9
|
-
|
10
|
-
def initialize(client)
|
11
|
-
@client = client
|
12
|
-
end
|
13
|
-
|
14
|
-
def create(order_data)
|
15
|
-
validate_order_data(order_data)
|
16
|
-
|
17
|
-
response = @client.auth_connection.post(ORDERS_URL) do |req|
|
18
|
-
req.headers['Content-Type'] = 'application/json'
|
19
|
-
req.body = order_data.to_json
|
20
|
-
end
|
21
|
-
handle_response(response)
|
22
|
-
end
|
23
|
-
|
24
|
-
def track(order_uuid)
|
25
|
-
@client.validate_uuid(order_uuid)
|
26
|
-
|
27
|
-
response = @client.auth_connection.get("#{ORDERS_URL}/#{order_uuid}") do |req|
|
28
|
-
req.headers['Content-Type'] = 'application/json'
|
29
|
-
end
|
30
|
-
handle_response(response)
|
31
|
-
end
|
32
|
-
|
33
|
-
private
|
34
|
-
|
35
|
-
def validate_order_data(order_data)
|
36
|
-
raise 'order_data must be a Hash' unless order_data.is_a?(Entities::OrderData)
|
37
|
-
end
|
38
|
-
|
39
|
-
def handle_response(response)
|
40
|
-
@client.send(:handle_response, response)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module CDEKApiClient
|
4
|
-
module Entities
|
5
|
-
class Recipient
|
6
|
-
attr_accessor :name, :phones, :email
|
7
|
-
|
8
|
-
VALIDATION_RULES = {
|
9
|
-
name: { type: :string, presence: true },
|
10
|
-
phones: { type: :array, presence: true, items: [{ type: :string, presence: true }] },
|
11
|
-
email: { type: :string, presence: true }
|
12
|
-
}.freeze
|
13
|
-
|
14
|
-
def initialize(name:, phones:, email:)
|
15
|
-
@name = name
|
16
|
-
@phones = phones
|
17
|
-
@email = email
|
18
|
-
|
19
|
-
Validator.validate(
|
20
|
-
{
|
21
|
-
name: @name,
|
22
|
-
phones: @phones,
|
23
|
-
email: @email
|
24
|
-
}, VALIDATION_RULES
|
25
|
-
)
|
26
|
-
end
|
27
|
-
|
28
|
-
def to_json(*_args)
|
29
|
-
{
|
30
|
-
name: @name,
|
31
|
-
phones: @phones,
|
32
|
-
email: @email
|
33
|
-
}.to_json
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module CDEKApiClient
|
4
|
-
class Tariff
|
5
|
-
BASE_URL = ENV.fetch('CDEK_API_URL', 'https://api.edu.cdek.ru/v2')
|
6
|
-
TARIFF_URL = "#{BASE_URL}/calculator/tariff".freeze
|
7
|
-
|
8
|
-
def initialize(client)
|
9
|
-
@client = client
|
10
|
-
end
|
11
|
-
|
12
|
-
def calculate(tariff_data)
|
13
|
-
validate_tariff_data(tariff_data)
|
14
|
-
|
15
|
-
response = @client.auth_connection.post(TARIFF_URL) do |req|
|
16
|
-
req.headers['Content-Type'] = 'application/json'
|
17
|
-
req.body = tariff_data.to_json
|
18
|
-
end
|
19
|
-
handle_response(response)
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
def validate_tariff_data(tariff_data)
|
25
|
-
raise 'tariff_data must be a Hash' unless tariff_data.is_a?(Entities::TariffData)
|
26
|
-
end
|
27
|
-
|
28
|
-
def handle_response(response)
|
29
|
-
@client.send(:handle_response, response)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module CDEKApiClient
|
4
|
-
class TrackOrder
|
5
|
-
BASE_URL = ENV.fetch('CDEK_API_URL', 'https://api.edu.cdek.ru/v2')
|
6
|
-
TRACK_ORDER_URL = "#{BASE_URL}/orders/%<uuid>s".freeze
|
7
|
-
|
8
|
-
def initialize(client)
|
9
|
-
@client = client
|
10
|
-
end
|
11
|
-
|
12
|
-
def get(order_uuid)
|
13
|
-
validate_uuid(order_uuid)
|
14
|
-
|
15
|
-
response = @client.auth_connection.get(format(TRACK_ORDER_URL, uuid: order_uuid))
|
16
|
-
handle_response(response)
|
17
|
-
end
|
18
|
-
|
19
|
-
private
|
20
|
-
|
21
|
-
def validate_uuid(uuid)
|
22
|
-
@client.send(:validate_uuid, uuid)
|
23
|
-
end
|
24
|
-
|
25
|
-
def handle_response(response)
|
26
|
-
@client.send(:handle_response, response)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,54 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module CDEKApiClient
|
4
|
-
class Webhook
|
5
|
-
BASE_URL = ENV.fetch('CDEK_API_URL', 'https://api.edu.cdek.ru/v2')
|
6
|
-
WEBHOOKS_URL = "#{BASE_URL}/webhooks".freeze
|
7
|
-
|
8
|
-
def initialize(client)
|
9
|
-
@client = client
|
10
|
-
end
|
11
|
-
|
12
|
-
def register(webhook_data)
|
13
|
-
validate_webhook_data(webhook_data)
|
14
|
-
|
15
|
-
response = @client.auth_connection.post(WEBHOOKS_URL) do |req|
|
16
|
-
req.headers['Content-Type'] = 'application/json'
|
17
|
-
req.body = webhook_data.to_json
|
18
|
-
end
|
19
|
-
handle_response(response)
|
20
|
-
end
|
21
|
-
|
22
|
-
def list
|
23
|
-
response = @client.auth_connection.get(WEBHOOKS_URL) do |req|
|
24
|
-
req.headers['Content-Type'] = 'application/json'
|
25
|
-
end
|
26
|
-
handle_response(response)
|
27
|
-
end
|
28
|
-
|
29
|
-
def delete(webhook_id)
|
30
|
-
validate_uuid(webhook_id)
|
31
|
-
|
32
|
-
response = @client.auth_connection.delete("#{WEBHOOKS_URL}/#{webhook_id}") do |req|
|
33
|
-
req.headers['Content-Type'] = 'application/json'
|
34
|
-
end
|
35
|
-
handle_response(response)
|
36
|
-
end
|
37
|
-
|
38
|
-
private
|
39
|
-
|
40
|
-
def validate_webhook_data(webhook_data)
|
41
|
-
raise 'webhook_data must be a Webhook' unless webhook_data.is_a?(CDEKApiClient::Entities::Webhook)
|
42
|
-
end
|
43
|
-
|
44
|
-
def validate_uuid(uuid)
|
45
|
-
return if uuid.match?(/\A[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}\z/)
|
46
|
-
|
47
|
-
raise 'Invalid UUID format'
|
48
|
-
end
|
49
|
-
|
50
|
-
def handle_response(response)
|
51
|
-
@client.send(:handle_response, response)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|