Class: AruxApp::API::Cart

Inherits:
Object
  • Object
show all
Defined in:
lib/arux_app/api/cart.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Cart

Returns a new instance of Cart.



16
17
18
19
20
21
22
# File 'lib/arux_app/api/cart.rb', line 16

def initialize(options = {})
  self.access_token = options[:access_token]
  self.auth = self.access_token.auth

  raise API::InitializerError.new(:access_token, "can't be blank") if self.access_token.nil?
  raise API::InitializerError.new(:access_token, "must be of class type AruxApp::API::Auth::AccessToken") if !self.access_token.is_a?(AruxApp::API::Auth::AccessToken)
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



14
15
16
# File 'lib/arux_app/api/cart.rb', line 14

def access_token
  @access_token
end

#authObject

Returns the value of attribute auth.



14
15
16
# File 'lib/arux_app/api/cart.rb', line 14

def auth
  @auth
end

Class Method Details

.server_uriObject



4
5
6
7
8
9
10
11
12
# File 'lib/arux_app/api/cart.rb', line 4

def self.server_uri
  if AruxApp::API.standardmode?
    "https://cart.arux.app"
  elsif AruxApp::API.testmode?
    "https://cart.arux.blue"
  elsif AruxApp::API.devmode?
    "https://cart.#{HOSTNAME}"
  end
end

Instance Method Details

#add_item(params) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/arux_app/api/cart.rb', line 92

def add_item(params)
  path = %(/api/#{self.generate_cart_path}/items)

  request = HTTPI::Request.new
  request.url = "#{self.class.server_uri}#{path}"
  if params.keys.first.to_s != 'item'
    params = {:item => params}
  end
  request.body = params.to_json
  request.headers = self.generate_headers

  response = HTTPI.post(request)

  if !response.error?
    JSON.parse(response.body)
  else
    raise(API::Error.new(response.code, JSON.parse(response.body)["message"]))
  end
end

#create(params) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/arux_app/api/cart.rb', line 163

def create(params)
  path = "/api/#{self.generate_cart_path}/carts"
  request = HTTPI::Request.new
  request.url = "#{self.class.server_uri}#{path}"
  request.headers = self.generate_headers
  request.body = params.to_json

  response = HTTPI.post(request)

  if !response.error?
    JSON.parse(response.body)
  else
    raise(API::Error.new(response.code, JSON.parse(response.body)["message"]))
  end
end

#delete_item(item_identifier) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/arux_app/api/cart.rb', line 132

def delete_item(item_identifier)
  path = %(/api/#{self.generate_cart_path}/items/#{AruxApp::API.uri_escape(item_identifier)})

  request = HTTPI::Request.new
  request.url = "#{self.class.server_uri}#{path}"
  request.headers = self.generate_headers

  response = HTTPI.delete(request)

  if !response.error?
    JSON.parse(response.body)
  else
    raise(API::Error.new(response.code, JSON.parse(response.body)["message"]))
  end
end

#destroy(uuid) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/arux_app/api/cart.rb', line 148

def destroy(uuid)
  path = "/api/#{self.generate_cart_path}/carts/#{uuid}"
  request = HTTPI::Request.new
  request.url = "#{self.class.server_uri}#{path}"
  request.headers = self.generate_headers

  response = HTTPI.delete(request)

  if !response.error?
    JSON.parse(response.body)
  else
    raise(API::Error.new(response.code, JSON.parse(response.body)["message"]))
  end
end

#get(uuid = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/arux_app/api/cart.rb', line 24

def get(uuid = nil)
  if uuid
    path = %(/api/#{AruxApp::API.uri_escape(uuid)})
  else
    path = %(/api/#{self.generate_cart_path})
  end

  request = HTTPI::Request.new
  request.url = "#{self.class.server_uri}#{path}"
  request.headers = self.generate_headers

  response = HTTPI.get(request)

  if !response.error?
    JSON.parse(response.body)
  else
    raise(API::Error.new(response.code, JSON.parse(response.body)["message"]))
  end
end

#get_item(item_identifier) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/arux_app/api/cart.rb', line 76

def get_item(item_identifier)
  path = %(/api/#{self.generate_cart_path}/items/#{item_identifier})

  request = HTTPI::Request.new
  request.url = "#{self.class.server_uri}#{path}"
  request.headers = self.generate_headers

  response = HTTPI.get(request)

  if !response.error?
    JSON.parse(response.body)
  else
    raise(API::Error.new(response.code, JSON.parse(response.body)["message"]))
  end
end

#get_itemsObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/arux_app/api/cart.rb', line 60

def get_items
  path = %(/api/#{self.generate_cart_path}/items)

  request = HTTPI::Request.new
  request.url = "#{self.class.server_uri}#{path}"
  request.headers = self.generate_headers

  response = HTTPI.get(request)

  if !response.error?
    JSON.parse(response.body)
  else
    raise(API::Error.new(response.code, JSON.parse(response.body)["message"]))
  end
end

#get_statusObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/arux_app/api/cart.rb', line 44

def get_status
  path = %(/api/#{self.generate_cart_path}/status)

  request = HTTPI::Request.new
  request.url = "#{self.class.server_uri}#{path}"
  request.headers = self.generate_headers

  response = HTTPI.get(request)

  if !response.error?
    JSON.parse(response.body)
  else
    raise(API::Error.new(response.code, JSON.parse(response.body)["message"]))
  end
end

#update_item(item_identifier, params) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/arux_app/api/cart.rb', line 112

def update_item(item_identifier, params)
  path = %(/api/#{self.generate_cart_path}/items/#{AruxApp::API.uri_escape(item_identifier)})

  request = HTTPI::Request.new
  request.url = "#{self.class.server_uri}#{path}"
  if params.keys.first.to_s != 'item'
    params = {:item => params}
  end
  request.body = params.to_json
  request.headers = self.generate_headers

  response = HTTPI.put(request)

  if !response.error?
    JSON.parse(response.body)
  else
    raise(API::Error.new(response.code, JSON.parse(response.body)["message"]))
  end
end