Class: Lago::Api::Resources::Plan

Inherits:
Base
  • Object
show all
Defined in:
lib/lago/api/resources/plan.rb

Instance Attribute Summary

Attributes inherited from Base

#client

Instance Method Summary collapse

Methods inherited from Base

#create, #destroy, #get, #get_all, #initialize, #update

Constructor Details

This class inherits a constructor from Lago::Api::Resources::Base

Instance Method Details

#api_resourceObject



9
10
11
# File 'lib/lago/api/resources/plan.rb', line 9

def api_resource
  'plans'
end

#create_charge(plan_code, params) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/lago/api/resources/plan.rb', line 68

def create_charge(plan_code, params)
  response = connection.post(
    whitelist_charge_params(params),
    charges_uri(plan_code),
  )
  JSON.parse(response.to_json, object_class: OpenStruct).charge
end

#create_charge_filter(plan_code, charge_code, params) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/lago/api/resources/plan.rb', line 142

def create_charge_filter(plan_code, charge_code, params)
  response = connection.post(
    whitelist_charge_filter_params(params),
    charge_filters_uri(plan_code, charge_code),
  )
  JSON.parse(response.to_json, object_class: OpenStruct).filter
end

#create_entitlements(plan_code, params) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/lago/api/resources/plan.rb', line 22

def create_entitlements(plan_code, params)
  response = connection.post(
    whitelist_entitlements_params(params),
    entitlements_uri(plan_code),
  )
  JSON.parse(response.to_json, object_class: OpenStruct).entitlements
end

#create_fixed_charge(plan_code, params) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/lago/api/resources/plan.rb', line 105

def create_fixed_charge(plan_code, params)
  response = connection.post(
    whitelist_fixed_charge_params(params),
    fixed_charges_uri(plan_code),
  )
  JSON.parse(response.to_json, object_class: OpenStruct).fixed_charge
end

#delete_all_metadata(plan_code) ⇒ Object



309
310
311
312
313
314
# File 'lib/lago/api/resources/plan.rb', line 309

def (plan_code)
  path = "/api/v1/plans/#{plan_code}/metadata"
  response = connection.destroy(path, identifier: nil)

  response['metadata']
end

#delete_entitlement(plan_code, feature_code) ⇒ Object



44
45
46
47
# File 'lib/lago/api/resources/plan.rb', line 44

def delete_entitlement(plan_code, feature_code)
  response = connection.destroy(entitlements_uri(plan_code, feature_code), identifier: nil)
  JSON.parse(response.to_json, object_class: OpenStruct).entitlement
end

#delete_entitlement_privilege(plan_code, feature_code, privilege_code) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/lago/api/resources/plan.rb', line 49

def delete_entitlement_privilege(plan_code, feature_code, privilege_code)
  response = connection.destroy(
    entitlements_uri(plan_code, feature_code, "privileges/#{privilege_code}"),
    identifier: nil,
  )
  JSON.parse(response.to_json, object_class: OpenStruct).entitlement
end

#delete_metadata_key(plan_code, key) ⇒ Object



316
317
318
319
320
321
# File 'lib/lago/api/resources/plan.rb', line 316

def (plan_code, key)
  path = "/api/v1/plans/#{plan_code}/metadata/#{key}"
  response = connection.destroy(path, identifier: nil)

  response['metadata']
end

#destroy_charge(plan_code, charge_code, params = {}) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/lago/api/resources/plan.rb', line 85

def destroy_charge(plan_code, charge_code, params = {})
  response = connection.destroy(
    charges_uri(plan_code, charge_code),
    identifier: nil,
    options: params.empty? ? nil : params,
  )
  JSON.parse(response.to_json, object_class: OpenStruct).charge
end

#destroy_charge_filter(plan_code, charge_code, filter_id, params = {}) ⇒ Object



159
160
161
162
163
164
165
166
# File 'lib/lago/api/resources/plan.rb', line 159

def destroy_charge_filter(plan_code, charge_code, filter_id, params = {})
  response = connection.destroy(
    charge_filters_uri(plan_code, charge_code, filter_id),
    identifier: nil,
    options: params.empty? ? nil : params,
  )
  JSON.parse(response.to_json, object_class: OpenStruct).filter
end

#destroy_fixed_charge(plan_code, fixed_charge_code, params = {}) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/lago/api/resources/plan.rb', line 122

def destroy_fixed_charge(plan_code, fixed_charge_code, params = {})
  response = connection.destroy(
    fixed_charges_uri(plan_code, fixed_charge_code),
    identifier: nil,
    options: params.empty? ? nil : params,
  )
  JSON.parse(response.to_json, object_class: OpenStruct).fixed_charge
end

#get_all_charge_filters(plan_code, charge_code, options = {}) ⇒ Object

Charge Filters



132
133
134
135
# File 'lib/lago/api/resources/plan.rb', line 132

def get_all_charge_filters(plan_code, charge_code, options = {})
  response = connection.get_all(options, charge_filters_uri(plan_code, charge_code))
  JSON.parse(response.to_json, object_class: OpenStruct)
end

#get_all_charges(plan_code, options = {}) ⇒ Object

Charges



58
59
60
61
# File 'lib/lago/api/resources/plan.rb', line 58

def get_all_charges(plan_code, options = {})
  response = connection.get_all(options, charges_uri(plan_code))
  JSON.parse(response.to_json, object_class: OpenStruct)
end

#get_all_fixed_charges(plan_code, options = {}) ⇒ Object

Fixed Charges



95
96
97
98
# File 'lib/lago/api/resources/plan.rb', line 95

def get_all_fixed_charges(plan_code, options = {})
  response = connection.get_all(options, fixed_charges_uri(plan_code))
  JSON.parse(response.to_json, object_class: OpenStruct)
end

#get_charge(plan_code, charge_code) ⇒ Object



63
64
65
66
# File 'lib/lago/api/resources/plan.rb', line 63

def get_charge(plan_code, charge_code)
  response = connection.get(charges_uri(plan_code, charge_code), identifier: nil)
  JSON.parse(response.to_json, object_class: OpenStruct).charge
end

#get_charge_filter(plan_code, charge_code, filter_id) ⇒ Object



137
138
139
140
# File 'lib/lago/api/resources/plan.rb', line 137

def get_charge_filter(plan_code, charge_code, filter_id)
  response = connection.get(charge_filters_uri(plan_code, charge_code, filter_id), identifier: nil)
  JSON.parse(response.to_json, object_class: OpenStruct).filter
end

#get_entitlement(plan_code, feature_code) ⇒ Object



39
40
41
42
# File 'lib/lago/api/resources/plan.rb', line 39

def get_entitlement(plan_code, feature_code)
  response = connection.get(entitlements_uri(plan_code, feature_code), identifier: nil)
  JSON.parse(response.to_json, object_class: OpenStruct).entitlement
end

#get_entitlements(plan_code) ⇒ Object



17
18
19
20
# File 'lib/lago/api/resources/plan.rb', line 17

def get_entitlements(plan_code)
  response = connection.get(entitlements_uri(plan_code), identifier: nil)
  JSON.parse(response.to_json, object_class: OpenStruct).entitlements
end

#get_fixed_charge(plan_code, fixed_charge_code) ⇒ Object



100
101
102
103
# File 'lib/lago/api/resources/plan.rb', line 100

def get_fixed_charge(plan_code, fixed_charge_code)
  response = connection.get(fixed_charges_uri(plan_code, fixed_charge_code), identifier: nil)
  JSON.parse(response.to_json, object_class: OpenStruct).fixed_charge
end

#merge_metadata(plan_code, metadata) ⇒ Object



301
302
303
304
305
306
307
# File 'lib/lago/api/resources/plan.rb', line 301

def (plan_code, )
  path = "/api/v1/plans/#{plan_code}/metadata"
  payload = { metadata: () }
  response = connection.patch(path, identifier: nil, body: payload)

  response['metadata']
end

#replace_metadata(plan_code, metadata) ⇒ Object



293
294
295
296
297
298
299
# File 'lib/lago/api/resources/plan.rb', line 293

def (plan_code, )
  path = "/api/v1/plans/#{plan_code}/metadata"
  payload = { metadata: () }
  response = connection.post(payload, path)

  response['metadata']
end

#root_nameObject



13
14
15
# File 'lib/lago/api/resources/plan.rb', line 13

def root_name
  'plan'
end

#update_charge(plan_code, charge_code, params) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/lago/api/resources/plan.rb', line 76

def update_charge(plan_code, charge_code, params)
  response = connection.put(
    charges_uri(plan_code, charge_code),
    identifier: nil,
    body: whitelist_charge_params(params),
  )
  JSON.parse(response.to_json, object_class: OpenStruct).charge
end

#update_charge_filter(plan_code, charge_code, filter_id, params) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/lago/api/resources/plan.rb', line 150

def update_charge_filter(plan_code, charge_code, filter_id, params)
  response = connection.put(
    charge_filters_uri(plan_code, charge_code, filter_id),
    identifier: nil,
    body: whitelist_charge_filter_params(params),
  )
  JSON.parse(response.to_json, object_class: OpenStruct).filter
end

#update_entitlements(plan_code, params) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/lago/api/resources/plan.rb', line 30

def update_entitlements(plan_code, params)
  response = connection.patch(
    entitlements_uri(plan_code),
    identifier: nil,
    body: whitelist_entitlements_params(params),
  )
  JSON.parse(response.to_json, object_class: OpenStruct).entitlements
end

#update_fixed_charge(plan_code, fixed_charge_code, params) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/lago/api/resources/plan.rb', line 113

def update_fixed_charge(plan_code, fixed_charge_code, params)
  response = connection.put(
    fixed_charges_uri(plan_code, fixed_charge_code),
    identifier: nil,
    body: whitelist_fixed_charge_params(params),
  )
  JSON.parse(response.to_json, object_class: OpenStruct).fixed_charge
end

#whitelist_charges(charges) ⇒ Object



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/lago/api/resources/plan.rb', line 215

def whitelist_charges(charges)
  processed_charges = []

  charges&.each do |c|
    result = (c || {}).slice(
      :id,
      :billable_metric_id,
      :charge_model,
      :pay_in_advance,
      :invoiceable,
      :regroup_paid_fees,
      :invoice_display_name,
      :min_amount_cents,
      :prorated,
      :properties,
      :filters,
      :tax_codes,
      :applied_pricing_unit,
      :accepts_target_wallet,
    )

    processed_charges << result unless result.empty?
  end

  processed_charges
end

#whitelist_entitlements_params(params) ⇒ Object



287
288
289
290
291
# File 'lib/lago/api/resources/plan.rb', line 287

def whitelist_entitlements_params(params)
  {
    entitlements: params,
  }
end

#whitelist_fixed_charges(fixed_charges) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/lago/api/resources/plan.rb', line 259

def whitelist_fixed_charges(fixed_charges)
  processed_fixed_charges = []

  fixed_charges&.each do |fc|
    result = (fc || {}).slice(
      :id,
      :add_on_id,
      :charge_model,
      :code,
      :invoice_display_name,
      :units,
      :pay_in_advance,
      :prorated,
      :properties,
      :tax_codes,
      :apply_units_immediately,
    )

    processed_fixed_charges << result unless result.empty?
  end

  processed_fixed_charges
end

#whitelist_metadata(metadata) ⇒ Object



283
284
285
# File 'lib/lago/api/resources/plan.rb', line 283

def ()
  &.to_h&.transform_keys(&:to_s)&.transform_values { |v| v&.to_s }
end

#whitelist_minimum_commitment(minimum_commitment) ⇒ Object



207
208
209
210
211
212
213
# File 'lib/lago/api/resources/plan.rb', line 207

def whitelist_minimum_commitment(minimum_commitment)
  minimum_commitment&.slice(
    :amount_cents,
    :invoice_display_name,
    :tax_codes,
  )
end

#whitelist_params(params) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/lago/api/resources/plan.rb', line 168

def whitelist_params(params)
  result_hash = {
    name: params[:name],
    invoice_display_name: params[:invoice_display_name],
    code: params[:code],
    interval: params[:interval],
    description: params[:description],
    amount_cents: params[:amount_cents],
    amount_currency: params[:amount_currency],
    trial_period: params[:trial_period],
    pay_in_advance: params[:pay_in_advance],
    bill_charges_monthly: params[:bill_charges_monthly],
    bill_fixed_charges_monthly: params[:bill_fixed_charges_monthly],
    tax_codes: params[:tax_codes],
    cascade_updates: params[:cascade_updates],
  }.compact

  whitelist_charges(params[:charges]).tap do |charges|
    result_hash[:charges] = charges unless charges.empty?
  end

  whitelist_minimum_commitment(params[:minimum_commitment]).tap do |minimum_commitment|
    result_hash[:minimum_commitment] = minimum_commitment
  end

  whitelist_usage_thresholds(params[:usage_thresholds]).tap do |usage_thresholds|
    result_hash[:usage_thresholds] = usage_thresholds unless usage_thresholds.empty?
  end

  whitelist_fixed_charges(params[:fixed_charges]).tap do |fixed_charges|
    result_hash[:fixed_charges] = fixed_charges unless fixed_charges.empty?
  end

   = (params[:metadata])
  result_hash[:metadata] =  if 

  { root_name => result_hash }
end

#whitelist_usage_thresholds(usage_thresholds) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/lago/api/resources/plan.rb', line 242

def whitelist_usage_thresholds(usage_thresholds)
  processed_usage_thresholds = []

  usage_thresholds&.each do |c|
    result = (c || {}).slice(
      :id,
      :threshold_display_name,
      :amount_cents,
      :recurring,
    )

    processed_usage_thresholds << result unless result.empty?
  end

  processed_usage_thresholds
end