Class: Lago::Api::Resources::Subscription
- Inherits:
-
Base
- Object
- Base
- Lago::Api::Resources::Subscription
show all
- Defined in:
- lib/lago/api/resources/subscription.rb
Instance Attribute Summary
Attributes inherited from Base
#client
Instance Method Summary
collapse
-
#api_resource ⇒ Object
-
#create_alert(external_subscription_id, params) ⇒ Object
-
#delete_alert(external_subscription_id, code) ⇒ Object
-
#delete_entitlement(external_subscription_id, feature_code) ⇒ Object
-
#delete_entitlement_privilege(external_subscription_id, entitlement_code, privilege_code) ⇒ Object
-
#get_alert(external_subscription_id, code) ⇒ Object
-
#get_alerts(external_subscription_id) ⇒ Object
-
#get_entitlements(external_subscription_id) ⇒ Object
-
#lifetime_usage(external_subscription_id) ⇒ Object
-
#root_name ⇒ Object
-
#update_alert(external_subscription_id, code, params) ⇒ Object
-
#update_entitlements(external_subscription_id, params) ⇒ Object
-
#update_lifetime_usage(external_subscription_id, params) ⇒ Object
-
#whitelist_alert_create_params(params) ⇒ Object
-
#whitelist_alert_update_params(params) ⇒ Object
-
#whitelist_entitlements_update_params(params) ⇒ Object
-
#whitelist_lifetime_usage_params(params) ⇒ Object
-
#whitelist_params(params) ⇒ Object
-
#whitelist_thresholds(params) ⇒ Object
Methods inherited from Base
#create, #destroy, #get, #get_all, #initialize, #update
Instance Method Details
#api_resource ⇒ Object
9
10
11
|
# File 'lib/lago/api/resources/subscription.rb', line 9
def api_resource
'subscriptions'
end
|
#create_alert(external_subscription_id, params) ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/lago/api/resources/subscription.rb', line 89
def create_alert(external_subscription_id, params)
response = connection.post(
whitelist_alert_create_params(params),
alert_uri(external_subscription_id),
)
JSON.parse(response.to_json, object_class: OpenStruct).alert
end
|
#delete_alert(external_subscription_id, code) ⇒ Object
79
80
81
82
|
# File 'lib/lago/api/resources/subscription.rb', line 79
def delete_alert(external_subscription_id, code)
response = connection.destroy(alert_uri(external_subscription_id, code), identifier: nil)
JSON.parse(response.to_json, object_class: OpenStruct).alert
end
|
#delete_entitlement(external_subscription_id, feature_code) ⇒ Object
52
53
54
55
|
# File 'lib/lago/api/resources/subscription.rb', line 52
def delete_entitlement(external_subscription_id, feature_code)
response = connection.destroy(entitlements_uri(external_subscription_id, feature_code), identifier: nil)
JSON.parse(response.to_json, object_class: OpenStruct)
end
|
#delete_entitlement_privilege(external_subscription_id, entitlement_code, privilege_code) ⇒ Object
57
58
59
60
61
62
63
|
# File 'lib/lago/api/resources/subscription.rb', line 57
def delete_entitlement_privilege(external_subscription_id, entitlement_code, privilege_code)
response = connection.destroy(
entitlements_uri(external_subscription_id, entitlement_code, "privileges/#{privilege_code}"),
identifier: nil,
)
JSON.parse(response.to_json, object_class: OpenStruct)
end
|
#get_alert(external_subscription_id, code) ⇒ Object
65
66
67
68
|
# File 'lib/lago/api/resources/subscription.rb', line 65
def get_alert(external_subscription_id, code)
response = connection.get(alert_uri(external_subscription_id, code), identifier: nil)
JSON.parse(response.to_json, object_class: OpenStruct).alert
end
|
#get_alerts(external_subscription_id) ⇒ Object
84
85
86
87
|
# File 'lib/lago/api/resources/subscription.rb', line 84
def get_alerts(external_subscription_id)
response = connection.get(alert_uri(external_subscription_id), identifier: nil)
JSON.parse(response.to_json, object_class: OpenStruct).alerts
end
|
#get_entitlements(external_subscription_id) ⇒ Object
38
39
40
41
|
# File 'lib/lago/api/resources/subscription.rb', line 38
def get_entitlements(external_subscription_id)
response = connection.get(entitlements_uri(external_subscription_id), identifier: nil)
JSON.parse(response.to_json, object_class: OpenStruct).entitlements
end
|
#lifetime_usage(external_subscription_id) ⇒ Object
17
18
19
20
21
22
23
|
# File 'lib/lago/api/resources/subscription.rb', line 17
def lifetime_usage(external_subscription_id)
uri = URI(
"#{client.base_api_url}#{api_resource}/#{external_subscription_id}/lifetime_usage",
)
response = connection.get(uri, identifier: nil)
JSON.parse(response.to_json, object_class: OpenStruct).lifetime_usage
end
|
#root_name ⇒ Object
13
14
15
|
# File 'lib/lago/api/resources/subscription.rb', line 13
def root_name
'subscription'
end
|
#update_alert(external_subscription_id, code, params) ⇒ Object
70
71
72
73
74
75
76
77
|
# File 'lib/lago/api/resources/subscription.rb', line 70
def update_alert(external_subscription_id, code, params)
response = connection.put(
alert_uri(external_subscription_id, code),
identifier: nil,
body: whitelist_alert_update_params(params),
)
JSON.parse(response.to_json, object_class: OpenStruct).alert
end
|
#update_entitlements(external_subscription_id, params) ⇒ Object
43
44
45
46
47
48
49
50
|
# File 'lib/lago/api/resources/subscription.rb', line 43
def update_entitlements(external_subscription_id, params)
response = connection.patch(
entitlements_uri(external_subscription_id),
identifier: nil,
body: whitelist_entitlements_update_params(params),
)
JSON.parse(response.to_json, object_class: OpenStruct)
end
|
#update_lifetime_usage(external_subscription_id, params) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/lago/api/resources/subscription.rb', line 25
def update_lifetime_usage(external_subscription_id, params)
uri = URI(
"#{client.base_api_url}#{api_resource}/#{external_subscription_id}/lifetime_usage",
)
response = connection.put(
uri,
identifier: nil,
body: whitelist_lifetime_usage_params(params),
)
JSON.parse(response.to_json, object_class: OpenStruct).lifetime_usage
end
|
#whitelist_alert_create_params(params) ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/lago/api/resources/subscription.rb', line 120
def whitelist_alert_create_params(params)
{
alert: {
alert_type: params[:alert_type],
name: params[:name],
code: params[:code],
billable_metric_code: params[:billable_metric_code],
thresholds: params[:thresholds],
}.compact,
}
end
|
#whitelist_alert_update_params(params) ⇒ Object
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/lago/api/resources/subscription.rb', line 132
def whitelist_alert_update_params(params)
{
alert: {
name: params[:name],
code: params[:code],
billable_metric_code: params[:billable_metric_code],
thresholds: params[:thresholds],
}.compact,
}
end
|
#whitelist_entitlements_update_params(params) ⇒ Object
149
150
151
152
153
|
# File 'lib/lago/api/resources/subscription.rb', line 149
def whitelist_entitlements_update_params(params)
{
entitlements: params,
}
end
|
#whitelist_lifetime_usage_params(params) ⇒ Object
112
113
114
115
116
117
118
|
# File 'lib/lago/api/resources/subscription.rb', line 112
def whitelist_lifetime_usage_params(params)
{
lifetime_usage: {
external_historical_usage_amount_cents: params[:external_historical_usage_amount_cents],
},
}
end
|
#whitelist_params(params) ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/lago/api/resources/subscription.rb', line 97
def whitelist_params(params)
{
root_name => {
external_customer_id: params[:external_customer_id],
plan_code: params[:plan_code],
name: params[:name],
external_id: params[:external_id],
billing_time: params[:billing_time],
subscription_at: params[:subscription_at],
ending_at: params[:ending_at],
plan_overrides: params[:plan_overrides],
}.compact,
}
end
|
#whitelist_thresholds(params) ⇒ Object
143
144
145
146
147
|
# File 'lib/lago/api/resources/subscription.rb', line 143
def whitelist_thresholds(params)
(params || []).map do |p|
(p || {}).slice(:code, :value, :recurring)
end
end
|