Class: Giftrocket::Order

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/giftrocket/order.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Order



9
10
11
12
13
14
15
16
17
18
# File 'lib/giftrocket/order.rb', line 9

def initialize(attributes)
  attributes = attributes.with_indifferent_access
  self.id = attributes[:id]
  self.gifts = attributes[:gifts].map do |gift_attributes|
    Gift.new(gift_attributes)
  end

  self.payment = Giftrocket::Payment.new(attributes[:payment])
  self.sender = Giftrocket::User.new(attributes[:sender])
end

Instance Attribute Details

#giftsObject

Returns the value of attribute gifts.



7
8
9
# File 'lib/giftrocket/order.rb', line 7

def gifts
  @gifts
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/giftrocket/order.rb', line 7

def id
  @id
end

#paymentObject

Returns the value of attribute payment.



7
8
9
# File 'lib/giftrocket/order.rb', line 7

def payment
  @payment
end

#senderObject

Returns the value of attribute sender.



7
8
9
# File 'lib/giftrocket/order.rb', line 7

def sender
  @sender
end

Class Method Details

.create!(funding_source_id, gifts_data_array) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/giftrocket/order.rb', line 20

def self.create!(funding_source_id, gifts_data_array)
  data_to_post = {
    funding_source_id: funding_source_id,
    gifts: gifts_data_array
  }.merge(Giftrocket.default_options)

  response = post '/', body: data_to_post.to_json, headers: { 'Content-Type' => 'application/json' }
  if response.success?
    response_json = JSON.parse(response.body).with_indifferent_access
    Giftrocket::Order.new(response_json[:order])
  else
    raise Giftrocket::Error.new(response)
  end
end

.listObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/giftrocket/order.rb', line 35

def self.list
  response = get '/', query: Giftrocket.default_options, format: 'json'
  if response.success?
    response_json = JSON.parse(response.body).with_indifferent_access
    response_json[:orders].map do |order_attributes|
      Giftrocket::Order.new(order_attributes)
    end
  else
    raise Giftrocket::Error.new(response)
  end
end

.retrieve(id) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/giftrocket/order.rb', line 47

def self.retrieve(id)
  response = get "/#{id}", query: Giftrocket.default_options, format: 'json'
  if response.success?
    response_json = JSON.parse(response.body).with_indifferent_access
    Giftrocket::Order.new(response_json[:order])
  else
    raise Giftrocket::Error.new(response)
  end
end