class ProcessOut::Coupon

Attributes

amount_off[R]
created_at[R]
currency[R]
expires_at[R]
id[R]
iteration_count[R]
max_redemptions[R]
metadata[R]
percent_off[R]
project[R]
project_id[R]
redeemed_number[R]
sandbox[R]

Public Class Methods

new(client, data = {}) click to toggle source

Initializes the Coupon object Params:

client

ProcessOut client instance

data

data that can be used to fill the object

# 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.metadata = 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

Public Instance Methods

all(options = {}) click to toggle source

Get all the coupons. Params:

options

Hash of options

# 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
amount_off=(val) click to toggle source
# File lib/processout/coupon.rb, line 50
def amount_off=(val)
  @amount_off = val
end
create(options = {}) click to toggle source

Create a new coupon. Params:

options

Hash of options

# 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
created_at=(val) click to toggle source
# File lib/processout/coupon.rb, line 86
def created_at=(val)
  @created_at = val
end
currency=(val) click to toggle source
# File lib/processout/coupon.rb, line 58
def currency=(val)
  @currency = val
end
delete(options = {}) click to toggle source

Delete the coupon. Params:

options

Hash of options

# 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
expires_at=(val) click to toggle source
# File lib/processout/coupon.rb, line 70
def expires_at=(val)
  @expires_at = val
end
fill_with_data(data) click to toggle source

Fills the object with data coming from the API Params:

data

Hash of data coming from the API

# 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.metadata = 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 = {}) click to toggle source

Find a coupon by its ID. Params:

coupon_id

ID of the coupon

options

Hash of options

# 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
id=(val) click to toggle source
# File lib/processout/coupon.rb, line 26
def id=(val)
  @id = val
end
iteration_count=(val) click to toggle source
# File lib/processout/coupon.rb, line 62
def iteration_count=(val)
  @iteration_count = val
end
max_redemptions=(val) click to toggle source
# File lib/processout/coupon.rb, line 66
def max_redemptions=(val)
  @max_redemptions = val
end
metadata=(val) click to toggle source
# File lib/processout/coupon.rb, line 74
def metadata=(val)
  @metadata = val
end
new(data = {}) click to toggle source

Create a new Coupon using the current client

# File lib/processout/coupon.rb, line 115
def new(data = {})
  Coupon.new(@client, data)
end
percent_off=(val) click to toggle source
# File lib/processout/coupon.rb, line 54
def percent_off=(val)
  @percent_off = val
end
prefill(data) click to toggle source

Prefills the object with the data passed as parameters Params:

data

Hash of data

# 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.metadata = data.fetch(:metadata, self.metadata)
  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
project=(val) click to toggle source
# File lib/processout/coupon.rb, line 30
def project=(val)
  if val.nil?
    @project = val
    return
  end

  if val.instance_of? Project
    @project = val
  else
    obj = Project.new(@client)
    obj.fill_with_data(val)
    @project = obj
  end
  
end
project_id=(val) click to toggle source
# File lib/processout/coupon.rb, line 46
def project_id=(val)
  @project_id = val
end
redeemed_number=(val) click to toggle source
# File lib/processout/coupon.rb, line 78
def redeemed_number=(val)
  @redeemed_number = val
end
sandbox=(val) click to toggle source
# File lib/processout/coupon.rb, line 82
def sandbox=(val)
  @sandbox = val
end
save(options = {}) click to toggle source

Save the updated coupon attributes. Params:

options

Hash of options

# 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) click to toggle source

Overrides the JSON marshaller to only send the fields we want

# 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.metadata,
      "redeemed_number": self.redeemed_number,
      "sandbox": self.sandbox,
      "created_at": self.created_at,
  }.to_json
end