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_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

#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

#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

#root_nameObject



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

def root_name
  'plan'
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

#whitelist_charges(charges) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/lago/api/resources/plan.rb', line 96

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,
      :properties,
      :filters,
      :tax_codes,
      :applied_pricing_unit,
    )

    processed_charges << result unless result.empty?
  end

  processed_charges
end

#whitelist_entitlements_params(params) ⇒ Object



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

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

#whitelist_minimum_commitment(minimum_commitment) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/lago/api/resources/plan.rb', line 88

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

#whitelist_params(params) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/lago/api/resources/plan.rb', line 57

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],
    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

  { root_name => result_hash }
end

#whitelist_usage_thresholds(usage_thresholds) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/lago/api/resources/plan.rb', line 121

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