Class: CDEKApiClient::Entities::Barcode

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

Overview

Represents a barcode entity for printing barcodes 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, format: 'A4', lang: nil) ⇒ Barcode

Initializes a new Barcode object.

Parameters:

  • orders (Array<Hash>)

    the list of orders for barcode generation.

  • copy_count (Integer) (defaults to: 1)

    the number of copies (default: 1).

  • type (String) (defaults to: nil)

    the type of barcode.

  • format (String) (defaults to: 'A4')

    the print format (A4, A5, A6).

  • lang (String) (defaults to: nil)

    the language code.

Raises:

  • (ArgumentError)

    if any attribute validation fails.



27
28
29
30
31
32
33
34
# File 'lib/cdek_api_client/entities/barcode.rb', line 27

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

Instance Attribute Details

#copy_countObject

Returns the value of attribute copy_count.



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

def copy_count
  @copy_count
end

#formatObject

Returns the value of attribute format.



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

def format
  @format
end

#langObject

Returns the value of attribute lang.



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

def lang
  @lang
end

#ordersObject

Returns the value of attribute orders.



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

def orders
  @orders
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.with_cdek_numbers(cdek_numbers) ⇒ Barcode

Creates a Barcode with CDEK numbers.

Parameters:

  • cdek_numbers (String, Array<String>)

    the CDEK number(s).

Returns:

  • (Barcode)

    the barcode instance.



51
52
53
54
55
56
# File 'lib/cdek_api_client/entities/barcode.rb', line 51

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) ⇒ Barcode

Creates a Barcode with orders UUIDs.

Parameters:

  • orders_uuid (String, Array<String>)

    the order UUID(s).

Returns:

  • (Barcode)

    the barcode instance.



40
41
42
43
44
45
# File 'lib/cdek_api_client/entities/barcode.rb', line 40

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 Barcode object to a JSON representation.

Returns:

  • (String)

    the JSON representation of the Barcode.



61
62
63
64
65
66
67
68
# File 'lib/cdek_api_client/entities/barcode.rb', line 61

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