Class: ProcessOut::Coupon

Inherits:
Object
  • Object
show all
Defined in:
lib/processout/coupon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, data = {}) ⇒ Coupon

Initializes the Coupon object Params:

client

ProcessOut client instance

data

data that can be used to fill the object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/processout/coupon.rb', line 95

def initialize(client, data = {})
  @client = client

  self.id = data.fetch(:id, nil)
  self.project = data.fetch(:project, nil)
  self.project_id = data.fetch(:project_id, nil)
  self.amount_off = data.fetch(:amount_off, nil)
  self.percent_off = data.fetch(:percent_off, nil)
  self.currency = data.fetch(:currency, nil)
  self.iteration_count = data.fetch(:iteration_count, nil)
  self.max_redemptions = data.fetch(:max_redemptions, nil)
  self.expires_at = data.fetch(:expires_at, nil)
  self. = data.fetch(:metadata, nil)
  self.redeemed_number = data.fetch(:redeemed_number, nil)
  self.sandbox = data.fetch(:sandbox, nil)
  self.created_at = data.fetch(:created_at, nil)
  
end

Instance Attribute Details

#amount_offObject

Returns the value of attribute amount_off.



14
15
16
# File 'lib/processout/coupon.rb', line 14

def amount_off
  @amount_off
end

#created_atObject

Returns the value of attribute created_at.



23
24
25
# File 'lib/processout/coupon.rb', line 23

def created_at
  @created_at
end

#currencyObject

Returns the value of attribute currency.



16
17
18
# File 'lib/processout/coupon.rb', line 16

def currency
  @currency
end

#expires_atObject

Returns the value of attribute expires_at.



19
20
21
# File 'lib/processout/coupon.rb', line 19

def expires_at
  @expires_at
end

#idObject

Returns the value of attribute id.



11
12
13
# File 'lib/processout/coupon.rb', line 11

def id
  @id
end

#iteration_countObject

Returns the value of attribute iteration_count.



17
18
19
# File 'lib/processout/coupon.rb', line 17

def iteration_count
  @iteration_count
end

#max_redemptionsObject

Returns the value of attribute max_redemptions.



18
19
20
# File 'lib/processout/coupon.rb', line 18

def max_redemptions
  @max_redemptions
end

#metadataObject

Returns the value of attribute metadata.



20
21
22
# File 'lib/processout/coupon.rb', line 20

def 
  @metadata
end

#percent_offObject

Returns the value of attribute percent_off.



15
16
17
# File 'lib/processout/coupon.rb', line 15

def percent_off
  @percent_off
end

#projectObject

Returns the value of attribute project.



12
13
14
# File 'lib/processout/coupon.rb', line 12

def project
  @project
end

#project_idObject

Returns the value of attribute project_id.



13
14
15
# File 'lib/processout/coupon.rb', line 13

def project_id
  @project_id
end

#redeemed_numberObject

Returns the value of attribute redeemed_number.



21
22
23
# File 'lib/processout/coupon.rb', line 21

def redeemed_number
  @redeemed_number
end

#sandboxObject

Returns the value of attribute sandbox.



22
23
24
# File 'lib/processout/coupon.rb', line 22

def sandbox
  @sandbox
end

Instance Method Details

#all(options = {}) ⇒ Object

Get all the coupons. Params:

options

Hash of options



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/processout/coupon.rb', line 215

def all(options = {})
  self.prefill(options)

  request = Request.new(@client)
  path    = "/coupons"
  data    = {

  }

  response = Response.new(request.get(path, data, options))
  return_values = Array.new
  
  a    = Array.new
  body = response.body
  for v in body['coupons']
    tmp = Coupon.new(@client)
    tmp.fill_with_data(v)
    a.push(tmp)
  end

  return_values.push(a)
  

  
  return_values[0]
end

#create(options = {}) ⇒ Object

Create a new coupon. Params:

options

Hash of options



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/processout/coupon.rb', line 245

def create(options = {})
  self.prefill(options)

  request = Request.new(@client)
  path    = "/coupons"
  data    = {
    "id" => @id, 
    "amount_off" => @amount_off, 
    "percent_off" => @percent_off, 
    "currency" => @currency, 
    "iteration_count" => @iteration_count, 
    "max_redemptions" => @max_redemptions, 
    "expires_at" => @expires_at, 
    "metadata" => @metadata
  }

  response = Response.new(request.post(path, data, options))
  return_values = Array.new
  
  body = response.body
  body = body["coupon"]
  
  
  return_values.push(self.fill_with_data(body))
  

  
  return_values[0]
end

#delete(options = {}) ⇒ Object

Delete the coupon. Params:

options

Hash of options



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/processout/coupon.rb', line 332

def delete(options = {})
  self.prefill(options)

  request = Request.new(@client)
  path    = "/coupons/" + CGI.escape(@id) + ""
  data    = {

  }

  response = Response.new(request.delete(path, data, options))
  return_values = Array.new
  
  return_values.push(response.success)

  
  return_values[0]
end

#fill_with_data(data) ⇒ Object

Fills the object with data coming from the API Params:

data

Hash of data coming from the API



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/processout/coupon.rb', line 141

def fill_with_data(data)
  if data.nil?
    return self
  end
  if data.include? "id"
    self.id = data["id"]
  end
  if data.include? "project"
    self.project = data["project"]
  end
  if data.include? "project_id"
    self.project_id = data["project_id"]
  end
  if data.include? "amount_off"
    self.amount_off = data["amount_off"]
  end
  if data.include? "percent_off"
    self.percent_off = data["percent_off"]
  end
  if data.include? "currency"
    self.currency = data["currency"]
  end
  if data.include? "iteration_count"
    self.iteration_count = data["iteration_count"]
  end
  if data.include? "max_redemptions"
    self.max_redemptions = data["max_redemptions"]
  end
  if data.include? "expires_at"
    self.expires_at = data["expires_at"]
  end
  if data.include? "metadata"
    self. = data["metadata"]
  end
  if data.include? "redeemed_number"
    self.redeemed_number = data["redeemed_number"]
  end
  if data.include? "sandbox"
    self.sandbox = data["sandbox"]
  end
  if data.include? "created_at"
    self.created_at = data["created_at"]
  end
  
  self
end

#find(coupon_id, options = {}) ⇒ Object

Find a coupon by its ID. Params:

coupon_id

ID of the coupon

options

Hash of options



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/processout/coupon.rb', line 279

def find(coupon_id, options = {})
  self.prefill(options)

  request = Request.new(@client)
  path    = "/coupons/" + CGI.escape(coupon_id) + ""
  data    = {

  }

  response = Response.new(request.get(path, data, options))
  return_values = Array.new
  
  body = response.body
  body = body["coupon"]
  
  
  obj = Coupon.new(@client)
  return_values.push(obj.fill_with_data(body))
  

  
  return_values[0]
end

#new(data = {}) ⇒ Object

Create a new Coupon using the current client



115
116
117
# File 'lib/processout/coupon.rb', line 115

def new(data = {})
  Coupon.new(@client, data)
end

#prefill(data) ⇒ Object

Prefills the object with the data passed as parameters Params:

data

Hash of data



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/processout/coupon.rb', line 191

def prefill(data)
  if data.nil?
    return self
  end
  self.id = data.fetch(:id, self.id)
  self.project = data.fetch(:project, self.project)
  self.project_id = data.fetch(:project_id, self.project_id)
  self.amount_off = data.fetch(:amount_off, self.amount_off)
  self.percent_off = data.fetch(:percent_off, self.percent_off)
  self.currency = data.fetch(:currency, self.currency)
  self.iteration_count = data.fetch(:iteration_count, self.iteration_count)
  self.max_redemptions = data.fetch(:max_redemptions, self.max_redemptions)
  self.expires_at = data.fetch(:expires_at, self.expires_at)
  self. = data.fetch(:metadata, self.)
  self.redeemed_number = data.fetch(:redeemed_number, self.redeemed_number)
  self.sandbox = data.fetch(:sandbox, self.sandbox)
  self.created_at = data.fetch(:created_at, self.created_at)
  
  self
end

#save(options = {}) ⇒ Object

Save the updated coupon attributes. Params:

options

Hash of options



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/processout/coupon.rb', line 306

def save(options = {})
  self.prefill(options)

  request = Request.new(@client)
  path    = "/coupons/" + CGI.escape(@id) + ""
  data    = {
    "metadata" => @metadata
  }

  response = Response.new(request.put(path, data, options))
  return_values = Array.new
  
  body = response.body
  body = body["coupon"]
  
  
  return_values.push(self.fill_with_data(body))
  

  
  return_values[0]
end

#to_json(options) ⇒ Object

Overrides the JSON marshaller to only send the fields we want



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/processout/coupon.rb', line 120

def to_json(options)
  {
      "id": self.id,
      "project": self.project,
      "project_id": self.project_id,
      "amount_off": self.amount_off,
      "percent_off": self.percent_off,
      "currency": self.currency,
      "iteration_count": self.iteration_count,
      "max_redemptions": self.max_redemptions,
      "expires_at": self.expires_at,
      "metadata": self.,
      "redeemed_number": self.redeemed_number,
      "sandbox": self.sandbox,
      "created_at": self.created_at,
  }.to_json
end