Class: WhiplashApi::OrderItem

Inherits:
Base
  • Object
show all
Defined in:
lib/whiplash_api/order_item.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connection, headers, reset_headers!, testing!

Class Method Details

.delete(*args) ⇒ Object



45
46
47
48
49
# File 'lib/whiplash_api/order_item.rb', line 45

def delete(*args)
  super
rescue WhiplashApi::RecordNotFound
  raise RecordNotFound, "No order item was found with given ID."
end

.find(*args) ⇒ Object

Finders for OrderItem require ‘order_id` key to be present. The following routine enforces this condition.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/whiplash_api/order_item.rb', line 8

def find(*args)
  scope   = args.slice!(0)
  options = args.slice!(0) || {}

  if [:all, :first, :last, :one].include?(scope)
    invalid = options[:params].to_s.empty? || options[:params][:order_id].to_s.empty?
    raise Error, "You must supply an Order ID (as parameter) to retrieve order items for." if invalid
  end

  super(scope, options)
end

.find_or_create_by_originator_id(id, args = {}) ⇒ Object



27
28
29
# File 'lib/whiplash_api/order_item.rb', line 27

def find_or_create_by_originator_id(id, args={})
  originator(id) || create(args.merge(originator_id: id))
end

.originator(id, args = {}) ⇒ Object



20
21
22
23
24
25
# File 'lib/whiplash_api/order_item.rb', line 20

def originator(id, args={})
  self.collection_name = "order_items/originator"
  order = self.find(id, args) rescue nil
  self.collection_name = "order_items"
  order
end

.separate(id, args = {}) ⇒ Object



38
39
40
41
42
43
# File 'lib/whiplash_api/order_item.rb', line 38

def separate(id, args={})
  response = self.post("#{id}/separate")
  sanitize_as_resource JSON.parse(response.body)
rescue WhiplashApi::RecordNotFound
  raise RecordNotFound, "No order item found with given ID."
end

.update(id, args = {}) ⇒ Object



31
32
33
34
35
36
# File 'lib/whiplash_api/order_item.rb', line 31

def update(id, args={})
  response = self.put(id, {}, args.to_json)
  response.code.to_i >= 200 && response.code.to_i < 300
rescue WhiplashApi::RecordNotFound
  raise RecordNotFound, "No order item found with given ID."
end

Instance Method Details

#destroyObject

instance methods



54
55
56
# File 'lib/whiplash_api/order_item.rb', line 54

def destroy
  self.class.delete(self.id)
end

#separateObject



58
59
60
# File 'lib/whiplash_api/order_item.rb', line 58

def separate
  self.class.separate(self.id)
end