cdek_api_client 0.2.0 → 0.3.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.
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'validatable'
4
+
5
+ module CDEKApiClient
6
+ module Entities
7
+ # Represents a barcode entity for printing barcodes in the CDEK API.
8
+ class Barcode
9
+ include Validatable
10
+
11
+ attr_accessor :orders, :copy_count, :type, :format, :lang
12
+
13
+ validates :orders, type: :array, presence: true, items: [{ type: :hash, presence: true }]
14
+ validates :copy_count, type: :integer, presence: false
15
+ validates :type, type: :string, presence: false
16
+ validates :format, type: :string, presence: false, inclusion: %w[A4 A5 A6]
17
+ validates :lang, type: :string, presence: false
18
+
19
+ # Initializes a new Barcode object.
20
+ #
21
+ # @param orders [Array<Hash>] the list of orders for barcode generation.
22
+ # @param copy_count [Integer] the number of copies (default: 1).
23
+ # @param type [String] the type of barcode.
24
+ # @param format [String] the print format (A4, A5, A6).
25
+ # @param lang [String] the language code.
26
+ # @raise [ArgumentError] if any attribute validation fails.
27
+ def initialize(orders:, copy_count: 1, type: nil, format: 'A4', lang: nil)
28
+ @orders = orders
29
+ @copy_count = copy_count
30
+ @type = type
31
+ @format = format
32
+ @lang = lang
33
+ validate!
34
+ end
35
+
36
+ # Creates a Barcode with orders UUIDs.
37
+ #
38
+ # @param orders_uuid [String, Array<String>] the order UUID(s).
39
+ # @return [Barcode] the barcode instance.
40
+ def self.with_orders_uuid(orders_uuid)
41
+ orders = Array(orders_uuid).map do |uuid|
42
+ { order_uuid: uuid }
43
+ end
44
+ new(orders: orders)
45
+ end
46
+
47
+ # Creates a Barcode with CDEK numbers.
48
+ #
49
+ # @param cdek_numbers [String, Array<String>] the CDEK number(s).
50
+ # @return [Barcode] the barcode instance.
51
+ def self.with_cdek_numbers(cdek_numbers)
52
+ orders = Array(cdek_numbers).map do |number|
53
+ { cdek_number: number }
54
+ end
55
+ new(orders: orders)
56
+ end
57
+
58
+ # Converts the Barcode object to a JSON representation.
59
+ #
60
+ # @return [String] the JSON representation of the Barcode.
61
+ def to_json(*_args)
62
+ data = { orders: @orders }
63
+ data[:copy_count] = @copy_count if @copy_count
64
+ data[:type] = @type if @type
65
+ data[:format] = @format if @format
66
+ data[:lang] = @lang if @lang
67
+ data.to_json
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'validatable'
4
+
5
+ module CDEKApiClient
6
+ module Entities
7
+ # Represents a check entity for retrieving check information in the CDEK API.
8
+ class Check
9
+ include Validatable
10
+
11
+ attr_accessor :cdek_number, :date
12
+
13
+ validates :cdek_number, type: :string, presence: false
14
+ validates :date, type: :string, presence: false
15
+
16
+ # Override validate! to allow both fields to be nil
17
+ def validate!
18
+ # Only validate non-nil fields
19
+ validate_presence(:cdek_number, @cdek_number, { presence: false }) unless @cdek_number.nil?
20
+ validate_type(:cdek_number, @cdek_number, { type: :string }) unless @cdek_number.nil?
21
+
22
+ validate_presence(:date, @date, { presence: false }) unless @date.nil?
23
+ validate_type(:date, @date, { type: :string }) unless @date.nil?
24
+ end
25
+
26
+ # Initializes a new Check object.
27
+ #
28
+ # @param cdek_number [String] the CDEK order number.
29
+ # @param date [String] the date in YYYY-MM-DD format.
30
+ # @raise [ArgumentError] if any attribute validation fails.
31
+ def initialize(cdek_number: nil, date: nil)
32
+ @cdek_number = cdek_number
33
+ @date = date
34
+ validate!
35
+ end
36
+
37
+ # Converts the Check object to a hash for query parameters.
38
+ #
39
+ # @return [Hash] the query parameters.
40
+ def to_query_params
41
+ params = {}
42
+ params[:cdek_number] = @cdek_number if @cdek_number
43
+ params[:date] = @date if @date
44
+ params
45
+ end
46
+
47
+ # Converts the Check object to a JSON representation.
48
+ #
49
+ # @return [String] the JSON representation of the Check.
50
+ def to_json(*_args)
51
+ to_query_params.to_json
52
+ end
53
+ end
54
+ end
55
+ end
@@ -29,11 +29,14 @@ module CDEKApiClient
29
29
 
30
30
  # Converts a currency code to its corresponding integer representation.
31
31
  #
32
- # @param currency [String] the currency code to convert.
32
+ # @param currency [String, Integer] the currency code (string like 'RUB') or integer code to validate.
33
33
  # @return [Integer] the integer representation of the currency code.
34
34
  # @raise [ArgumentError] if the currency code is invalid.
35
35
  def self.to_code(currency)
36
- CURRENCY_CODES[currency] || (raise ArgumentError, "Invalid currency code: #{currency}")
36
+ return currency if currency.is_a?(Integer) && CURRENCY_CODES.value?(currency)
37
+ return CURRENCY_CODES[currency] if currency.is_a?(String) && CURRENCY_CODES.key?(currency)
38
+
39
+ raise ArgumentError, "Invalid currency code: #{currency}"
37
40
  end
38
41
  end
39
42
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'validatable'
4
+
5
+ module CDEKApiClient
6
+ module Entities
7
+ # Represents a request for available courier intake days from the CDEK API.
8
+ class IntakeAvailableDaysRequest
9
+ include Validatable
10
+
11
+ attr_accessor :from_location, :date
12
+
13
+ validates :from_location, type: :hash, presence: true
14
+ validates :date, type: :string
15
+
16
+ # Initializes a new IntakeAvailableDaysRequest object.
17
+ #
18
+ # @param from_location [Hash] the location details for the intake.
19
+ # @param date [String, nil] the date up to which to get available days (optional).
20
+ # @raise [ArgumentError] if any attribute validation fails.
21
+ def initialize(from_location:, date: nil)
22
+ @from_location = from_location
23
+ @date = date
24
+ validate!
25
+ end
26
+
27
+ # Converts the IntakeAvailableDaysRequest object to a JSON representation.
28
+ #
29
+ # @return [String] the JSON representation of the IntakeAvailableDaysRequest.
30
+ def to_json(*_args)
31
+ data = { from_location: @from_location }
32
+ data[:date] = @date if @date
33
+ data.to_json
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'validatable'
4
+
5
+ module CDEKApiClient
6
+ module Entities
7
+ # Represents a response with available courier intake days from the CDEK API.
8
+ class IntakeAvailableDaysResponse
9
+ include Validatable
10
+
11
+ attr_accessor :date, :all_days, :errors, :warnings
12
+
13
+ validates :date, type: :array
14
+ validates :all_days, type: :boolean
15
+ validates :errors, type: :array
16
+ validates :warnings, type: :array
17
+
18
+ # Initializes a new IntakeAvailableDaysResponse object.
19
+ #
20
+ # @param date [Array<String>, nil] the available dates for intake.
21
+ # @param all_days [Boolean, nil] whether all days are available.
22
+ # @param errors [Array, nil] any errors in the response.
23
+ # @param warnings [Array, nil] any warnings in the response.
24
+ # @raise [ArgumentError] if any attribute validation fails.
25
+ def initialize(date: nil, all_days: nil, errors: nil, warnings: nil)
26
+ @date = date
27
+ @all_days = all_days
28
+ @errors = errors || []
29
+ @warnings = warnings || []
30
+ validate!
31
+ end
32
+
33
+ # Converts the IntakeAvailableDaysResponse object to a JSON representation.
34
+ #
35
+ # @return [String] the JSON representation of the IntakeAvailableDaysResponse.
36
+ def to_json(*_args)
37
+ data = {}
38
+ data[:date] = @date if @date
39
+ data[:all_days] = @all_days unless @all_days.nil?
40
+ data[:errors] = @errors unless @errors.empty?
41
+ data[:warnings] = @warnings unless @warnings.empty?
42
+ data.to_json
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'validatable'
4
+
5
+ module CDEKApiClient
6
+ module Entities
7
+ # Represents an intakes entity for courier intake requests in the CDEK API.
8
+ class Intakes
9
+ include Validatable
10
+
11
+ attr_accessor :cdek_number, :intake_date, :intake_time_from, :intake_time_to,
12
+ :lunch_time_from, :lunch_time_to, :name, :need_call, :comment,
13
+ :sender, :from_location, :weight, :length, :width, :height
14
+
15
+ validates :cdek_number, type: :string, presence: true
16
+ validates :intake_date, type: :string, presence: true
17
+ validates :intake_time_from, type: :string, presence: true
18
+ validates :intake_time_to, type: :string, presence: true
19
+ validates :lunch_time_from, type: :string, presence: false
20
+ validates :lunch_time_to, type: :string, presence: false
21
+ validates :name, type: :string, presence: true
22
+ validates :need_call, type: :boolean, presence: false
23
+ validates :comment, type: :string, presence: false
24
+ validates :sender, type: :hash, presence: true
25
+ validates :from_location, type: :hash, presence: true
26
+ validates :weight, type: :numeric, presence: false
27
+ validates :length, type: :numeric, presence: false
28
+ validates :width, type: :numeric, presence: false
29
+ validates :height, type: :numeric, presence: false
30
+
31
+ # Initializes a new Intakes object.
32
+ #
33
+ # @param cdek_number [String] the CDEK order number.
34
+ # @param intake_date [String] the intake date in YYYY-MM-DD format.
35
+ # @param intake_time_from [String] the start time in HH:MM format.
36
+ # @param intake_time_to [String] the end time in HH:MM format.
37
+ # @param lunch_time_from [String] the lunch start time in HH:MM format.
38
+ # @param lunch_time_to [String] the lunch end time in HH:MM format.
39
+ # @param name [String] the name/description of the cargo.
40
+ # @param need_call [Boolean] whether a call is needed.
41
+ # @param comment [String] the comment for the intake.
42
+ # @param sender [Contact] the sender contact information.
43
+ # @param from_location [Location] the pickup location.
44
+ # @param weight [Numeric] the weight of the cargo.
45
+ # @param length [Numeric] the length of the cargo.
46
+ # @param width [Numeric] the width of the cargo.
47
+ # @param height [Numeric] the height of the cargo.
48
+ # @raise [ArgumentError] if any attribute validation fails.
49
+ def initialize(cdek_number:, intake_date:, intake_time_from:, intake_time_to:,
50
+ name:, sender:, from_location:, lunch_time_from: nil, lunch_time_to: nil, need_call: nil,
51
+ comment: nil, weight: nil, length: nil,
52
+ width: nil, height: nil)
53
+ @cdek_number = cdek_number
54
+ @intake_date = intake_date
55
+ @intake_time_from = intake_time_from
56
+ @intake_time_to = intake_time_to
57
+ @lunch_time_from = lunch_time_from
58
+ @lunch_time_to = lunch_time_to
59
+ @name = name
60
+ @need_call = need_call
61
+ @comment = comment
62
+ @sender = sender
63
+ @from_location = from_location
64
+ @weight = weight
65
+ @length = length
66
+ @width = width
67
+ @height = height
68
+ validate!
69
+ end
70
+
71
+ # Converts the Intakes object to a JSON representation.
72
+ #
73
+ # @return [String] the JSON representation of the Intakes.
74
+ def to_json(*_args)
75
+ data = {
76
+ cdek_number: @cdek_number,
77
+ intake_date: @intake_date,
78
+ intake_time_from: @intake_time_from,
79
+ intake_time_to: @intake_time_to,
80
+ name: @name,
81
+ sender: @sender,
82
+ from_location: @from_location
83
+ }
84
+ data[:lunch_time_from] = @lunch_time_from if @lunch_time_from
85
+ data[:lunch_time_to] = @lunch_time_to if @lunch_time_to
86
+ data[:need_call] = @need_call unless @need_call.nil?
87
+ data[:comment] = @comment if @comment
88
+ data[:weight] = @weight if @weight
89
+ data[:length] = @length if @length
90
+ data[:width] = @width if @width
91
+ data[:height] = @height if @height
92
+ data.to_json
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'validatable'
4
+
5
+ module CDEKApiClient
6
+ module Entities
7
+ # Represents an invoice entity for printing invoices in the CDEK API.
8
+ class Invoice
9
+ include Validatable
10
+
11
+ attr_accessor :orders, :copy_count, :type
12
+
13
+ validates :orders, type: :array, presence: true, items: [{ type: :hash, presence: true }]
14
+ validates :copy_count, type: :integer, presence: false
15
+ validates :type, type: :string, presence: false
16
+
17
+ # Initializes a new Invoice object.
18
+ #
19
+ # @param orders [Array<Hash>] the list of orders for invoice generation.
20
+ # @param copy_count [Integer] the number of copies (default: 1).
21
+ # @param type [String] the type of invoice.
22
+ # @raise [ArgumentError] if any attribute validation fails.
23
+ def initialize(orders:, copy_count: 1, type: nil)
24
+ @orders = orders
25
+ @copy_count = copy_count
26
+ @type = type
27
+ validate!
28
+ end
29
+
30
+ # Creates an Invoice with orders UUIDs.
31
+ #
32
+ # @param orders_uuid [String, Array<String>] the order UUID(s).
33
+ # @return [Invoice] the invoice instance.
34
+ def self.with_orders_uuid(orders_uuid)
35
+ orders = Array(orders_uuid).map do |uuid|
36
+ { order_uuid: uuid }
37
+ end
38
+ new(orders: orders)
39
+ end
40
+
41
+ # Creates an Invoice with CDEK numbers.
42
+ #
43
+ # @param cdek_numbers [String, Array<String>] the CDEK number(s).
44
+ # @return [Invoice] the invoice instance.
45
+ def self.with_cdek_numbers(cdek_numbers)
46
+ orders = Array(cdek_numbers).map do |number|
47
+ { cdek_number: number }
48
+ end
49
+ new(orders: orders)
50
+ end
51
+
52
+ # Converts the Invoice object to a JSON representation.
53
+ #
54
+ # @return [String] the JSON representation of the Invoice.
55
+ def to_json(*_args)
56
+ data = { orders: @orders }
57
+ data[:copy_count] = @copy_count if @copy_count
58
+ data[:type] = @type if @type
59
+ data.to_json
60
+ end
61
+ end
62
+ end
63
+ end
@@ -38,7 +38,7 @@ module CDEKApiClient
38
38
  self.class.validations.each do |attribute, rule|
39
39
  value = send(attribute)
40
40
  validate_presence(attribute, value, rule)
41
- validate_type(attribute, value, rule)
41
+ validate_type(attribute, value, rule) unless value.nil?
42
42
  end
43
43
  end
44
44
 
@@ -160,6 +160,8 @@ module CDEKApiClient
160
160
  # @param rule [Hash] the validation rule.
161
161
  # @raise [RuntimeError] if the validation fails.
162
162
  def validate_hash(_attribute, hash, rule)
163
+ return unless rule[:schema]
164
+
163
165
  rule[:schema].each do |attr, validation_rule|
164
166
  value = hash[attr]
165
167
  validate_presence(attr, value, validation_rule)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CDEKApiClient
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
@@ -1,12 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'cdek_api_client/api/courier'
3
4
  require_relative 'cdek_api_client/api/location'
4
5
  require_relative 'cdek_api_client/api/order'
6
+ require_relative 'cdek_api_client/api/payment'
7
+ require_relative 'cdek_api_client/api/print'
5
8
  require_relative 'cdek_api_client/api/tariff'
6
9
  require_relative 'cdek_api_client/api/track_order'
7
10
  require_relative 'cdek_api_client/api/webhook'
8
11
  require_relative 'cdek_api_client/client'
12
+ require_relative 'cdek_api_client/entities/agreement'
13
+ require_relative 'cdek_api_client/entities/barcode'
14
+ require_relative 'cdek_api_client/entities/check'
9
15
  require_relative 'cdek_api_client/entities/currency_mapper'
16
+ require_relative 'cdek_api_client/entities/intakes'
17
+ require_relative 'cdek_api_client/entities/invoice'
10
18
  require_relative 'cdek_api_client/entities/item'
11
19
  require_relative 'cdek_api_client/entities/location'
12
20
  require_relative 'cdek_api_client/entities/order_data'
@@ -18,6 +26,10 @@ require_relative 'cdek_api_client/entities/service'
18
26
  require_relative 'cdek_api_client/entities/tariff_data'
19
27
  require_relative 'cdek_api_client/entities/validatable'
20
28
  require_relative 'cdek_api_client/entities/webhook'
29
+ require_relative 'cdek_api_client/entities/auth_response'
30
+ require_relative 'cdek_api_client/entities/auth_error_response'
31
+ require_relative 'cdek_api_client/entities/intake_available_days_request'
32
+ require_relative 'cdek_api_client/entities/intake_available_days_response'
21
33
  require_relative 'cdek_api_client/version'
22
34
 
23
35
  # frozen_string_literal: true
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cdek_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damir Mukimov
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-07-22 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: This gem provides a Ruby client for interacting with the CDEK API, including
14
13
  order creation, tracking, and tariff calculation.
@@ -18,15 +17,29 @@ executables: []
18
17
  extensions: []
19
18
  extra_rdoc_files: []
20
19
  files:
20
+ - CHANGELOG.md
21
21
  - README.md
22
22
  - lib/cdek_api_client.rb
23
+ - lib/cdek_api_client/api/courier.rb
23
24
  - lib/cdek_api_client/api/location.rb
24
25
  - lib/cdek_api_client/api/order.rb
26
+ - lib/cdek_api_client/api/payment.rb
27
+ - lib/cdek_api_client/api/print.rb
25
28
  - lib/cdek_api_client/api/tariff.rb
26
29
  - lib/cdek_api_client/api/track_order.rb
27
30
  - lib/cdek_api_client/api/webhook.rb
28
31
  - lib/cdek_api_client/client.rb
32
+ - lib/cdek_api_client/config.rb
33
+ - lib/cdek_api_client/entities/agreement.rb
34
+ - lib/cdek_api_client/entities/auth_error_response.rb
35
+ - lib/cdek_api_client/entities/auth_response.rb
36
+ - lib/cdek_api_client/entities/barcode.rb
37
+ - lib/cdek_api_client/entities/check.rb
29
38
  - lib/cdek_api_client/entities/currency_mapper.rb
39
+ - lib/cdek_api_client/entities/intake_available_days_request.rb
40
+ - lib/cdek_api_client/entities/intake_available_days_response.rb
41
+ - lib/cdek_api_client/entities/intakes.rb
42
+ - lib/cdek_api_client/entities/invoice.rb
30
43
  - lib/cdek_api_client/entities/item.rb
31
44
  - lib/cdek_api_client/entities/location.rb
32
45
  - lib/cdek_api_client/entities/order_data.rb
@@ -39,15 +52,16 @@ files:
39
52
  - lib/cdek_api_client/entities/validatable.rb
40
53
  - lib/cdek_api_client/entities/webhook.rb
41
54
  - lib/cdek_api_client/version.rb
42
- homepage: http://glowing-pixels.com/cdek_api_client
55
+ homepage: https://glowing-pixels.com/cdek_api_client
43
56
  licenses:
44
57
  - MIT
45
58
  metadata:
46
- github_repo: git@github.com/SamyRai/cdek_api_client.git
47
- homepage_uri: http://glowing-pixels.com/cdek_api_client
48
- source_code_uri: http://github.com/SamyRai/cdek_api_client
49
- changelog_uri: http://www.glowing-pixels.com/cdek_api_client/CHANGELOG.md
50
- post_install_message:
59
+ homepage_uri: https://glowing-pixels.com/cdek_api_client
60
+ source_code_uri: https://github.com/SamyRai/cdek_api_client
61
+ bug_tracker_uri: https://github.com/SamyRai/cdek_api_client/issues
62
+ changelog_uri: https://github.com/SamyRai/cdek_api_client/blob/main/CHANGELOG.md
63
+ documentation_uri: https://www.rubydoc.info/gems/cdek_api_client
64
+ rubygems_mfa_required: 'true'
51
65
  rdoc_options: []
52
66
  require_paths:
53
67
  - lib
@@ -62,8 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
76
  - !ruby/object:Gem::Version
63
77
  version: '0'
64
78
  requirements: []
65
- rubygems_version: 3.5.11
66
- signing_key:
79
+ rubygems_version: 3.7.2
67
80
  specification_version: 4
68
81
  summary: A Ruby client for the CDEK API
69
82
  test_files: []