Class: Fulfillment::Shipment

Inherits:
ModelBase show all
Defined in:
lib/fulfillment/shipment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, data) ⇒ Shipment

Returns a new instance of Shipment.



6
7
8
9
# File 'lib/fulfillment/shipment.rb', line 6

def initialize(client, data)
  @client = client
  make_getter_methods(data)
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



4
5
6
# File 'lib/fulfillment/shipment.rb', line 4

def client
  @client
end

Class Method Details

.add(client, order_public_id, shipment_public_id, shipment_items_array) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/fulfillment/shipment.rb', line 25

def add(client, order_public_id, shipment_public_id, shipment_items_array)
  shipment_payload = { "fulfillment_order_items" => shipment_items_array }
  curl = Curl::Easy.http_put(client.build_auth_url("/orders/#{order_public_id}/shipments/#{shipment_public_id}/add"), shipment_payload.to_json) do |curl|
    client.configure_http(curl)
  end

  raise Fulfillment::ClientException.new("Could not add shipment information about shipment #{shipment_public_id} to order #{order_public_id}:\n\n Response Body:\n #{curl.body_str}") unless curl.response_code == 200

  new(client, JSON.parse(curl.body_str))
end

.close(client, order_public_id, shipment_public_id) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/fulfillment/shipment.rb', line 88

def close(client, order_public_id, shipment_public_id)
  curl = Curl::Easy.http_put(client.build_auth_url("/orders/#{order_public_id}/shipments/#{shipment_public_id}/close"), {}.to_json) do |curl|
    client.configure_http(curl)
  end

  raise Fulfillment::ClientException.new("Could not close shipment #{shipment_public_id} for order #{order_public_id}:\n\n Response Body:\n #{curl.body_str}") unless curl.response_code == 200

  new(client, JSON.parse(curl.body_str))
end

.create(client, order_public_id, shipment_hash) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/fulfillment/shipment.rb', line 36

def create(client, order_public_id, shipment_hash)
  curl = Curl::Easy.http_post(client.build_auth_url("/orders/#{order_public_id}/shipments"), shipment_hash.to_json) do |curl|
    client.configure_http(curl)
  end

  raise Fulfillment::CreationException.new("Could not create shipment for order #{order_public_id}:\n\n Response Body:\n #{curl.body_str}") unless curl.response_code == 201

  new(client, JSON.parse(curl.body_str))
end

.list(client, first_page_num = 1) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fulfillment/shipment.rb', line 72

def list(client, first_page_num = 1)
  Fulfillment::PagedResult.construct(first_page_num) do |page_num|
    curl = Curl::Easy.http_get(client.build_auth_url("/shipments")) do |curl|
      client.configure_http(curl)
      client.set_request_page(curl, page_num)
    end
  
    raise Fulfillment::ClientException.new("Could not get index of shipments:\n\n Response Body:\n #{curl.body_str}") unless curl.response_code == 200
  
    shipment_hashes = JSON.parse(curl.body_str)
    result = shipment_hashes.map { |sh| new(client, sh) }
    
    Fulfillment::PagingEnvelope.envelop(curl, result)
  end  
end

.shipment_items(client, shipment_public_id, first_page_num = 1) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fulfillment/shipment.rb', line 46

def shipment_items(client, shipment_public_id, first_page_num = 1)
  Fulfillment::PagedResult.construct(first_page_num) do |page_num|
    curl = Curl::Easy.http_get(client.build_auth_url("/shipments/#{shipment_public_id}/items")) do |curl|
      client.configure_http(curl)
      client.set_request_page(curl, page_num)
    end

    raise Fulfillment::ClientException.new("Could not load index of items for shipment #{shipment_public_id}: \n\n Response Body:\n #{curl.body_str}") unless curl.response_code == 200

    shipment_items_hashes = JSON.parse(curl.body_str)
    result = shipment_items_hashes.map { |sh| Fulfillment::ShipmentItem.new(client, sh) }

    Fulfillment::PagingEnvelope.envelop(curl, result)
  end
end

.show(client, shipment_public_id) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/fulfillment/shipment.rb', line 62

def show(client, shipment_public_id)
  curl = Curl::Easy.http_get(client.build_auth_url("/shipments/#{shipment_public_id}")) do |curl|
    client.configure_http(curl)
  end

  raise Fulfillment::ClientException.new("Could not get shipment #{shipment_public_id}:\n\n Response Body:\n #{curl.body_str}") unless curl.response_code == 200

  new(client, JSON.parse(curl.body_str))
end

Instance Method Details

#add(shipment_items_array) ⇒ Object



15
16
17
# File 'lib/fulfillment/shipment.rb', line 15

def add(shipment_items_array)
  Fulfillment::Shipment.add(self.client, self.fulfillment_order['id'], self.public_id, shipment_items_array)
end

#closeObject



19
20
21
# File 'lib/fulfillment/shipment.rb', line 19

def close
  Fulfillment::Shipment.close(self.client, self.fulfillment_order['id'], self.public_id)
end

#shipment_items(first_page_num = 1) ⇒ Object



11
12
13
# File 'lib/fulfillment/shipment.rb', line 11

def shipment_items(first_page_num = 1)
  Fulfillment::Shipment.shipment_items(self.client, self.public_id, first_page_num)
end