Class: Myparcel::API::Shipments

Inherits:
Base
  • Object
show all
Defined in:
lib/myparcel/api/shipments.rb

Overview

Class for getting shipments

Instance Attribute Summary

Attributes inherited from Base

#authentication

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Myparcel::API::Base

Instance Method Details

#allObject



5
6
7
# File 'lib/myparcel/api/shipments.rb', line 5

def all
  find
end

#create(options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/myparcel/api/shipments.rb', line 19

def create(options = {})
  shipment_type = options.fetch(:shipment_type, :standard)
  shipments = options.fetch(:shipments, [])
  raise 'options[:shipments] must have at least one shipment.' if shipments.empty?

  options[:headers] ||= {}
  options[:headers]['Content-Type'] = headers_for_shipment(shipment_type)

  options[:body] ||= {}
  options[:body] = JSON.generate(data: { shipments: shipments })

  response = request :post, path, options
  response['data']['ids']
end

#delete(options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/myparcel/api/shipments.rb', line 34

def delete(options = {})
  shipment_ids = options.fetch :shipment_ids, []
  raise 'options[:shipment_ids] cannot be empty or nil' if shipment_ids.empty?

  shipment_ids = shipment_ids.join(';')
  full_path = [path, shipment_ids].join('/')

  response = request :delete, full_path, options
  response['data']
end

#find(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/myparcel/api/shipments.rb', line 9

def find(options = {})
  shipment_ids = options.fetch(:shipment_ids, [])
  shipment_ids = shipment_ids.join(';') if shipment_ids.is_a?(Array)

  full_path = shipment_ids.empty? ? path : [path, shipment_ids].join('/')

  response = request :get, full_path, options
  response['data']['shipments']
end

#pathObject



45
46
47
# File 'lib/myparcel/api/shipments.rb', line 45

def path
  'shipments'
end