Class: CDEKApiClient::Entities::Invoice

Inherits:
Object
  • Object
show all
Includes:
Validatable
Defined in:
lib/cdek_api_client/entities/invoice.rb

Overview

Represents an invoice entity for printing invoices in the CDEK API.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validatable

included, #validate!

Constructor Details

#initialize(orders:, copy_count: 1, type: nil) ⇒ Invoice

Initializes a new Invoice object.

Parameters:

  • orders (Array<Hash>)

    the list of orders for invoice generation.

  • copy_count (Integer) (defaults to: 1)

    the number of copies (default: 1).

  • type (String) (defaults to: nil)

    the type of invoice.

Raises:

  • (ArgumentError)

    if any attribute validation fails.



23
24
25
26
27
28
# File 'lib/cdek_api_client/entities/invoice.rb', line 23

def initialize(orders:, copy_count: 1, type: nil)
  @orders = orders
  @copy_count = copy_count
  @type = type
  validate!
end

Instance Attribute Details

#copy_countObject

Returns the value of attribute copy_count.



11
12
13
# File 'lib/cdek_api_client/entities/invoice.rb', line 11

def copy_count
  @copy_count
end

#ordersObject

Returns the value of attribute orders.



11
12
13
# File 'lib/cdek_api_client/entities/invoice.rb', line 11

def orders
  @orders
end

#typeObject

Returns the value of attribute type.



11
12
13
# File 'lib/cdek_api_client/entities/invoice.rb', line 11

def type
  @type
end

Class Method Details

.with_cdek_numbers(cdek_numbers) ⇒ Invoice

Creates an Invoice with CDEK numbers.

Parameters:

  • cdek_numbers (String, Array<String>)

    the CDEK number(s).

Returns:

  • (Invoice)

    the invoice instance.



45
46
47
48
49
50
# File 'lib/cdek_api_client/entities/invoice.rb', line 45

def self.with_cdek_numbers(cdek_numbers)
  orders = Array(cdek_numbers).map do |number|
    { cdek_number: number }
  end
  new(orders: orders)
end

.with_orders_uuid(orders_uuid) ⇒ Invoice

Creates an Invoice with orders UUIDs.

Parameters:

  • orders_uuid (String, Array<String>)

    the order UUID(s).

Returns:

  • (Invoice)

    the invoice instance.



34
35
36
37
38
39
# File 'lib/cdek_api_client/entities/invoice.rb', line 34

def self.with_orders_uuid(orders_uuid)
  orders = Array(orders_uuid).map do |uuid|
    { order_uuid: uuid }
  end
  new(orders: orders)
end

Instance Method Details

#to_json(*_args) ⇒ String

Converts the Invoice object to a JSON representation.

Returns:

  • (String)

    the JSON representation of the Invoice.



55
56
57
58
59
60
# File 'lib/cdek_api_client/entities/invoice.rb', line 55

def to_json(*_args)
  data = { orders: @orders }
  data[:copy_count] = @copy_count if @copy_count
  data[:type] = @type if @type
  data.to_json
end