Class: ProcessOut::Discount

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Discount

Initializes the Discount object Params:

client

ProcessOut client instance



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/processout/discount.rb', line 82

def initialize(client)
  @client = client

  @id = ""
  @project = nil
  @subscription = nil
  @coupon = nil
  @amount = ""
  @expires_at = ""
   = Hash.new
  @sandbox = false
  @created_at = ""
  
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



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

def amount
  @amount
end

#couponObject

Returns the value of attribute coupon.



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

def coupon
  @coupon
end

#created_atObject

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#expires_atObject

Returns the value of attribute expires_at.



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

def expires_at
  @expires_at
end

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/processout/discount.rb', line 10

def id
  @id
end

#metadataObject

Returns the value of attribute metadata.



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

def 
  
end

#projectObject

Returns the value of attribute project.



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

def project
  @project
end

#sandboxObject

Returns the value of attribute sandbox.



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

def sandbox
  @sandbox
end

#subscriptionObject

Returns the value of attribute subscription.



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

def subscription
  @subscription
end

Instance Method Details

#apply(subscription_id, options = nil) ⇒ Object

Apply a new discount to the given subscription ID. Params:

subscription_id

ID of the subscription

options

Hash of options



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/processout/discount.rb', line 136

def apply(subscription_id, options = nil)
  request = Request.new(@client)
  path    = "/subscriptions/" + CGI.escape(subscription_id) + "/discounts"
  data    = {
    "amount": @amount, 
    "expires_at": @expires_at, 
    "metadata": 
  }

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

  
  return_values[0]
end

#apply_coupon(subscription_id, coupon_id, options = nil) ⇒ Object

Apply a new discount to the given subscription ID from a coupon ID. Params:

subscription_id

ID of the subscription

coupon_id

ID of the coupon

options

Hash of options



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/discount.rb', line 164

def apply_coupon(subscription_id, coupon_id, options = nil)
  request = Request.new(@client)
  path    = "/subscriptions/" + CGI.escape(subscription_id) + "/discounts"
  data    = {
    "amount": @amount, 
    "expires_at": @expires_at, 
    "metadata": , 
    'coupon_id': coupon_id
  }

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

  
  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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/processout/discount.rb', line 100

def fill_with_data(data)
  if data.include? "id"
    @id = data["id"]
  end
  if data.include? "project"
    @project = data["project"]
  end
  if data.include? "subscription"
    @subscription = data["subscription"]
  end
  if data.include? "coupon"
    @coupon = data["coupon"]
  end
  if data.include? "amount"
    @amount = data["amount"]
  end
  if data.include? "expires_at"
    @expires_at = data["expires_at"]
  end
  if data.include? "metadata"
     = data["metadata"]
  end
  if data.include? "sandbox"
    @sandbox = data["sandbox"]
  end
  if data.include? "created_at"
    @created_at = data["created_at"]
  end
  
  self
end

#find(subscription_id, discount_id, options = nil) ⇒ Object

Find a subscription’s discount by its ID. Params:

subscription_id

ID of the subscription

discount_id

ID of the discount

options

Hash of options



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

def find(subscription_id, discount_id, options = nil)
  request = Request.new(@client)
  path    = "/subscriptions/" + CGI.escape(subscription_id) + "/discounts/" + CGI.escape(discount_id) + ""
  data    = {

  }

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

  
  return_values[0]
end