Class: Belpost::Models::CustomsDeclaration

Inherits:
Object
  • Object
show all
Defined in:
lib/belpost/models/customs_declaration.rb

Constant Summary collapse

VALID_CATEGORIES =
%w[gift documents sample returned_goods merchandise other].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ CustomsDeclaration

Returns a new instance of CustomsDeclaration.



10
11
12
13
14
15
16
17
18
19
# File 'lib/belpost/models/customs_declaration.rb', line 10

def initialize(data = {})
  @items = data[:items] || []
  @price = data[:price] || {}
  @category = data[:category]
  @explanation = data[:explanation]
  @comments = data[:comments]
  @invoice = data[:invoice]
  @licences = data[:licences] || []
  @certificates = data[:certificates] || []
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



8
9
10
# File 'lib/belpost/models/customs_declaration.rb', line 8

def category
  @category
end

#certificatesObject (readonly)

Returns the value of attribute certificates.



8
9
10
# File 'lib/belpost/models/customs_declaration.rb', line 8

def certificates
  @certificates
end

#commentsObject (readonly)

Returns the value of attribute comments.



8
9
10
# File 'lib/belpost/models/customs_declaration.rb', line 8

def comments
  @comments
end

#explanationObject (readonly)

Returns the value of attribute explanation.



8
9
10
# File 'lib/belpost/models/customs_declaration.rb', line 8

def explanation
  @explanation
end

#invoiceObject (readonly)

Returns the value of attribute invoice.



8
9
10
# File 'lib/belpost/models/customs_declaration.rb', line 8

def invoice
  @invoice
end

#itemsObject (readonly)

Returns the value of attribute items.



8
9
10
# File 'lib/belpost/models/customs_declaration.rb', line 8

def items
  @items
end

#licencesObject (readonly)

Returns the value of attribute licences.



8
9
10
# File 'lib/belpost/models/customs_declaration.rb', line 8

def licences
  @licences
end

#priceObject (readonly)

Returns the value of attribute price.



8
9
10
# File 'lib/belpost/models/customs_declaration.rb', line 8

def price
  @price
end

Instance Method Details

#add_item(item_data) ⇒ Object



21
22
23
# File 'lib/belpost/models/customs_declaration.rb', line 21

def add_item(item_data)
  @items << item_data
end

#set_category(category) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/belpost/models/customs_declaration.rb', line 29

def set_category(category)
  unless VALID_CATEGORIES.include?(category)
    raise ValidationError, "Invalid category. Must be one of: #{VALID_CATEGORIES.join(', ')}"
  end

  @category = category
end

#set_price(currency, value) ⇒ Object



25
26
27
# File 'lib/belpost/models/customs_declaration.rb', line 25

def set_price(currency, value)
  @price = { currency: currency, value: value }
end

#to_hObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/belpost/models/customs_declaration.rb', line 37

def to_h
  {
    items: @items,
    price: @price,
    category: @category,
    explanation: @explanation,
    comments: @comments,
    invoice: @invoice,
    licences: @licences,
    certificates: @certificates
  }.compact
end

#valid?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/belpost/models/customs_declaration.rb', line 50

def valid?
  return false if @category && !VALID_CATEGORIES.include?(@category)
  return false if @category == "other" && @explanation.nil?

  if %w[merchandise sample returned_goods].include?(@category)
    return false if @items.empty?
    return false if @price.empty?
  end

  true
end