Class: Apruve::Shipment

Inherits:
ApruveObject show all
Defined in:
lib/apruve/resources/shipment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApruveObject

#logger, logger, #to_hash, #to_json

Constructor Details

#initialize(params) ⇒ Shipment

Returns a new instance of Shipment.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/apruve/resources/shipment.rb', line 18

def initialize(params)
  super
  # hydrate payment items if appropriate
  if @shipment_items.nil?
    @shipment_items = []
  elsif @shipment_items.is_a?(Array) && @shipment_items.first.is_a?(Hash)
    hydrated_items = []
    @shipment_items.each do |item|
      hydrated_items << Apruve::ShipmentItem.new(item)
    end
    @shipment_items = hydrated_items
  end
  @currency = Apruve.default_currency if currency.nil?
end

Instance Attribute Details

#amount_centsObject

Returns the value of attribute amount_cents.



3
4
5
# File 'lib/apruve/resources/shipment.rb', line 3

def amount_cents
  @amount_cents
end

#currencyObject

Returns the value of attribute currency.



3
4
5
# File 'lib/apruve/resources/shipment.rb', line 3

def currency
  @currency
end

#delivered_atObject

Returns the value of attribute delivered_at.



3
4
5
# File 'lib/apruve/resources/shipment.rb', line 3

def delivered_at
  @delivered_at
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/apruve/resources/shipment.rb', line 3

def id
  @id
end

#invoice_idObject

Returns the value of attribute invoice_id.



3
4
5
# File 'lib/apruve/resources/shipment.rb', line 3

def invoice_id
  @invoice_id
end

#merchant_notesObject

Returns the value of attribute merchant_notes.



3
4
5
# File 'lib/apruve/resources/shipment.rb', line 3

def merchant_notes
  @merchant_notes
end

#merchant_shipment_idObject

Returns the value of attribute merchant_shipment_id.



3
4
5
# File 'lib/apruve/resources/shipment.rb', line 3

def merchant_shipment_id
  @merchant_shipment_id
end

#shipment_itemsObject

Returns the value of attribute shipment_items.



3
4
5
# File 'lib/apruve/resources/shipment.rb', line 3

def shipment_items
  @shipment_items
end

#shipped_atObject

Returns the value of attribute shipped_at.



3
4
5
# File 'lib/apruve/resources/shipment.rb', line 3

def shipped_at
  @shipped_at
end

#shipperObject

Returns the value of attribute shipper.



3
4
5
# File 'lib/apruve/resources/shipment.rb', line 3

def shipper
  @shipper
end

#shipping_centsObject

Returns the value of attribute shipping_cents.



3
4
5
# File 'lib/apruve/resources/shipment.rb', line 3

def shipping_cents
  @shipping_cents
end

#statusObject

Returns the value of attribute status.



3
4
5
# File 'lib/apruve/resources/shipment.rb', line 3

def status
  @status
end

#tax_centsObject

Returns the value of attribute tax_cents.



3
4
5
# File 'lib/apruve/resources/shipment.rb', line 3

def tax_cents
  @tax_cents
end

#tracking_numberObject

Returns the value of attribute tracking_number.



3
4
5
# File 'lib/apruve/resources/shipment.rb', line 3

def tracking_number
  @tracking_number
end

Class Method Details

.find(invoice_id, id) ⇒ Object



7
8
9
10
# File 'lib/apruve/resources/shipment.rb', line 7

def self.find(invoice_id, id)
  response = Apruve.get("invoices/#{invoice_id}/shipments/#{id}")
  Shipment.new(response.body)
end

.find_all(invoice_id) ⇒ Object



12
13
14
15
16
# File 'lib/apruve/resources/shipment.rb', line 12

def self.find_all(invoice_id)
  response = Apruve.get("invoices/#{invoice_id}/shipments")
  logger.debug response.body
  response.body.map { |shipment| Shipment.new(shipment) }
end

Instance Method Details

#save!Object



39
40
41
42
43
# File 'lib/apruve/resources/shipment.rb', line 39

def save!
  validate
  response = Apruve.post("invoices/#{self.invoice_id}/shipments", self.to_json)
  self.id = response.body['id']
end

#update!Object



45
46
47
48
# File 'lib/apruve/resources/shipment.rb', line 45

def update!
  validate
  Apruve.patch("invoices/#{self.invoice_id}/shipments/#{id}", self.to_json)
end

#validateObject



33
34
35
36
37
# File 'lib/apruve/resources/shipment.rb', line 33

def validate
  errors = []
  errors << 'invoice_id must be set' if invoice_id.nil?
  raise Apruve::ValidationError.new(errors) if errors.length > 0
end