lemonsqueezy 0.2.2 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.env.example +2 -1
  3. data/Gemfile +2 -1
  4. data/Gemfile.lock +13 -9
  5. data/README.md +194 -68
  6. data/bin/console +3 -1
  7. data/lib/lemon_squeezy/client.rb +57 -63
  8. data/lib/lemon_squeezy/configuration.rb +12 -0
  9. data/lib/lemon_squeezy/models/checkout.rb +46 -0
  10. data/lib/lemon_squeezy/models/customer.rb +52 -0
  11. data/lib/lemon_squeezy/models/discount.rb +47 -0
  12. data/lib/lemon_squeezy/models/discount_redemption.rb +19 -0
  13. data/lib/lemon_squeezy/models/file.rb +19 -0
  14. data/lib/lemon_squeezy/models/license.rb +24 -0
  15. data/lib/lemon_squeezy/models/license_key.rb +32 -0
  16. data/lib/lemon_squeezy/models/license_key_instance.rb +19 -0
  17. data/lib/lemon_squeezy/models/order.rb +24 -0
  18. data/lib/lemon_squeezy/models/price.rb +19 -0
  19. data/lib/lemon_squeezy/models/product.rb +19 -0
  20. data/lib/lemon_squeezy/models/store.rb +19 -0
  21. data/lib/lemon_squeezy/models/subscription.rb +101 -0
  22. data/lib/lemon_squeezy/models/subscription_invoice.rb +19 -0
  23. data/lib/lemon_squeezy/models/subscription_item.rb +38 -0
  24. data/lib/lemon_squeezy/models/subscription_usage.rb +4 -0
  25. data/lib/lemon_squeezy/models/user.rb +14 -0
  26. data/lib/lemon_squeezy/models/variant.rb +19 -0
  27. data/lib/lemon_squeezy/models/webhook.rb +58 -0
  28. data/lib/lemon_squeezy/object.rb +15 -1
  29. data/lib/lemon_squeezy/version.rb +1 -1
  30. data/lib/lemon_squeezy.rb +34 -29
  31. metadata +23 -29
  32. data/lib/lemon_squeezy/objects/checkout.rb +0 -12
  33. data/lib/lemon_squeezy/objects/customer.rb +0 -12
  34. data/lib/lemon_squeezy/objects/discount.rb +0 -12
  35. data/lib/lemon_squeezy/objects/file.rb +0 -12
  36. data/lib/lemon_squeezy/objects/license_key.rb +0 -12
  37. data/lib/lemon_squeezy/objects/license_key_instance.rb +0 -12
  38. data/lib/lemon_squeezy/objects/order.rb +0 -12
  39. data/lib/lemon_squeezy/objects/order_item.rb +0 -12
  40. data/lib/lemon_squeezy/objects/product.rb +0 -12
  41. data/lib/lemon_squeezy/objects/store.rb +0 -12
  42. data/lib/lemon_squeezy/objects/subscription.rb +0 -12
  43. data/lib/lemon_squeezy/objects/subscription_invoice.rb +0 -12
  44. data/lib/lemon_squeezy/objects/variant.rb +0 -12
  45. data/lib/lemon_squeezy/resource.rb +0 -60
  46. data/lib/lemon_squeezy/resources/checkouts.rb +0 -42
  47. data/lib/lemon_squeezy/resources/customers.rb +0 -15
  48. data/lib/lemon_squeezy/resources/discounts.rb +0 -15
  49. data/lib/lemon_squeezy/resources/files.rb +0 -15
  50. data/lib/lemon_squeezy/resources/license_key_instances.rb +0 -15
  51. data/lib/lemon_squeezy/resources/license_keys.rb +0 -15
  52. data/lib/lemon_squeezy/resources/orders.rb +0 -20
  53. data/lib/lemon_squeezy/resources/products.rb +0 -15
  54. data/lib/lemon_squeezy/resources/stores.rb +0 -15
  55. data/lib/lemon_squeezy/resources/subscription_invoices.rb +0 -15
  56. data/lib/lemon_squeezy/resources/subscriptions.rb +0 -92
  57. data/lib/lemon_squeezy/resources/variants.rb +0 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d3696e1ba538e89d4ea65a77f90a20c024f79790700981191437e09c22bf183
4
- data.tar.gz: 11186062f312505a6dbe326c9ff27a8c940573475f788b5d44cabbb3e27c95ac
3
+ metadata.gz: 53f2c6ca5274eba19d0bff2b7d05b439f4ffaad8fe2061537d35eafea9930337
4
+ data.tar.gz: 0d41faa2caed4e7f142a64df9196ff3c28f66d17f773108329d5876d2d299b85
5
5
  SHA512:
6
- metadata.gz: 47b685c7e6d41eaf056789dbdfbe58b2e080626cc20fe2fc997d78cc16d95545547d25efd1b54fbc51fc7c296cc6bd532b95a6193b94a43d97e2c4749bf44057
7
- data.tar.gz: 1894f19062e99ea38437dd0fc9c2691e90148c2974d8ca7390f1daf8a96de1291d455551a97f29b64cbf6f7dc955b6bc444bd4863b77a422f8e74281b826b9c3
6
+ metadata.gz: a8c0b303b09b54fb089eb5bc6d4dbe62fc162dc9bc6052860fc4f0b5ec61eb82cbb9fc9ba8c3e566e7251583a60ebeb52b6579fd366facf938745c74c3981555
7
+ data.tar.gz: '016958edb001b1e7b42c67280184a14cbfd29e455fd15fb0586766f3b8cfd8d52330c940f2d0e4551dd88ea23a32b6bdb96ad87a86b12b39e6ef00d5ec724e22'
data/.env.example CHANGED
@@ -1 +1,2 @@
1
- ACCESS_TOKEN=
1
+ LEMON_SQUEEZY_API_KEY=""
2
+ LEMON_SQUEEZY_API_KEY_TEST=""
data/Gemfile CHANGED
@@ -8,4 +8,5 @@ gemspec
8
8
  gem "rake", "~> 13.0"
9
9
 
10
10
  gem "minitest", "~> 5.0"
11
- gem "dotenv"
11
+ gem "dotenv"
12
+ gem "vcr"
data/Gemfile.lock CHANGED
@@ -1,20 +1,23 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lemonsqueezy (0.2.1)
4
+ lemonsqueezy (1.0.1)
5
5
  faraday (~> 2.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- dotenv (2.7.6)
11
- faraday (2.7.4)
12
- faraday-net_http (>= 2.0, < 3.1)
13
- ruby2_keywords (>= 0.0.4)
14
- faraday-net_http (3.0.2)
15
- minitest (5.15.0)
16
- rake (13.0.6)
17
- ruby2_keywords (0.0.5)
10
+ dotenv (2.8.1)
11
+ faraday (2.9.0)
12
+ faraday-net_http (>= 2.0, < 3.2)
13
+ faraday-net_http (3.1.0)
14
+ net-http
15
+ minitest (5.20.0)
16
+ net-http (0.4.1)
17
+ uri
18
+ rake (13.1.0)
19
+ uri (0.13.0)
20
+ vcr (6.2.0)
18
21
 
19
22
  PLATFORMS
20
23
  x86_64-linux
@@ -24,6 +27,7 @@ DEPENDENCIES
24
27
  lemonsqueezy!
25
28
  minitest (~> 5.0)
26
29
  rake (~> 13.0)
30
+ vcr
27
31
 
28
32
  BUNDLED WITH
29
33
  2.3.22
data/README.md CHANGED
@@ -1,220 +1,346 @@
1
- # LemonSqueezy
1
+ # LemonSqueezy Ruby Library
2
2
 
3
- **This Library is a work in progress**
4
-
5
- This is a Ruby library for interacting with the Lemon Squeezy API.
3
+ A Ruby library for the Lemon Squeezy API.
6
4
 
7
5
  ## Installation
8
6
 
9
7
  Add this line to your application's Gemfile:
10
8
 
11
9
  ```ruby
12
- gem "lemonsqueezy"
10
+ gem "lemonsqueezy", "~> 1.0"
13
11
  ```
14
12
 
15
13
  ## Usage
16
14
 
17
- ### Set Access Token
15
+ ### Set API Key
16
+
17
+ Firstly you'll need to create an API Key on your [settings page](https://app.lemonsqueezy.com/settings/api).
18
+
19
+ ```ruby
20
+ LemonSqueezy.configure do |config|
21
+ config.api_key = ENV["LEMON_SQUEEZY_API_KEY"]
22
+ end
23
+ ```
18
24
 
19
- Firstly you'll need to create an API Access Token on your [settings page](https://app.lemonsqueezy.com/settings/api).
25
+ ### User
20
26
 
21
27
  ```ruby
22
- @client = LemonSqueezy::Client.new(access_token: "")
28
+ # Retrieve the authenticated user
29
+ LemonSqueezy::User.me
23
30
  ```
24
31
 
25
32
  ### Stores
26
33
 
27
34
  ```ruby
28
35
  # Retrieves a list of Stores
29
- @client.stores.list
36
+ LemonSqueezy::Store.list
30
37
 
31
38
  # Retrieves a Store
32
- @client.stores.get id: "123"
39
+ LemonSqueezy::Store.retrieve id: 123
40
+ ```
41
+
42
+ ### Customers
43
+
44
+ ```ruby
45
+ # Retrieves a list of Customers
46
+ LemonSqueezy::Customer.list
47
+
48
+ # Retrieves a list of Customers for a store
49
+ LemonSqueezy::Customer.list store_id: 123
50
+
51
+ # Retrieves a list of Customers that contain the supplied email
52
+ LemonSqueezy::Customer.list email: "test@hello.com"
53
+
54
+ # Retrieves a Customer
55
+ LemonSqueezy::Customer.retrieve id: 123
56
+
57
+ # Create a customer
58
+ # https://docs.lemonsqueezy.com/api/customers#create-a-customer
59
+ LemonSqueezy::Customer.create store_id: 123, name: "Customer name", email: "test@hello.com"
60
+
61
+ # Update a customer
62
+ # https://docs.lemonsqueezy.com/api/customers#update-a-customer
63
+ LemonSqueezy::Customer.update id: 123, name: "Customer name", status: "archived"
33
64
  ```
34
65
 
35
66
  ### Products
36
67
 
37
68
  ```ruby
38
69
  # Retrieves a list of Products
39
- @client.products.list
70
+ LemonSqueezy::Product.list
40
71
 
41
72
  # Retrieves a list of Products for a specified Store
42
- @client.products.list store_id: 123
73
+ LemonSqueezy::Product.list store_id: 123
43
74
 
44
75
  # Retrieves a Product
45
- @client.products.get id: "123"
76
+ LemonSqueezy::Product.retrieve id: 123
46
77
  ```
47
78
 
48
79
  ### Product Variants
49
80
 
50
81
  ```ruby
51
82
  # Retrieves a list of Variants
52
- @client.variants.list
83
+ LemonSqueezy::Variant.list
53
84
 
54
85
  # Retrieves a list of Variants for a Product
55
- @client.variants.list product_id: 123
86
+ LemonSqueezy::Variant.list product_id: 123
56
87
 
57
88
  # Retrieves a Variant
58
- @client.variants.get id: "123"
89
+ LemonSqueezy::Variant.retrieve id: 123
90
+ ```
91
+
92
+ ### Prices
93
+
94
+ ```ruby
95
+ # Retrieves a list of Prices
96
+ LemonSqueezy::Price.list
97
+
98
+ # Retrieves a list of Prices for a Variant
99
+ LemonSqueezy::Price.list variant_id: 123
100
+
101
+ # Retrieves a Price
102
+ LemonSqueezy::Price.retrieve id: 123
59
103
  ```
60
104
 
61
105
  ### Orders
62
106
 
63
107
  ```ruby
64
108
  # Retrieves a list of Orders
65
- @client.orders.list
109
+ LemonSqueezy::Order.list
66
110
 
67
111
  # Retrieves a list of Orders for a specified store
68
- @client.orders.list product_id: 123
112
+ LemonSqueezy::Order.list product_id: 123
69
113
 
70
114
  # Retrieves a list of Orders for an email address
71
- @client.orders.list email: "hello@test.com"
115
+ LemonSqueezy::Order.list email: "hello@test.com"
72
116
 
73
117
  # Retrieves an Order
74
- @client.orders.get id: "123"
118
+ LemonSqueezy::Order.retrieve id: 123
75
119
 
76
120
  # Retrieves the items on an Order
77
- @client.orders.get order_items: "123"
121
+ LemonSqueezy::Order.retrieve order_items: 123
78
122
  ```
79
123
 
80
124
  ### Subscriptions
81
125
 
82
126
  ```ruby
83
127
  # Retrieves a list of Subscriptions
84
- @client.subscriptions.list
128
+ LemonSqueezy::Subscription.list
85
129
 
86
130
  # Retrieves a list of Subscriptions for a store
87
- @client.subscriptions.list store_id: 123
131
+ LemonSqueezy::Subscription.list store_id: 123
88
132
 
89
133
  # Retrieves a list of Subscriptions for an Order
90
- @client.subscriptions.list order_id: 123
134
+ LemonSqueezy::Subscription.list order_id: 123
91
135
 
92
136
  # Retrieves a list of Subscriptions for a product
93
- @client.subscriptions.list product_id: 123
137
+ LemonSqueezy::Subscription.list product_id: 123
94
138
 
95
139
  # Retrieves a Subscription
96
- @client.subscriptions.get id: 123
140
+ LemonSqueezy::Subscription.retrieve id: 123
141
+
142
+ # Updates a Subscription
143
+ # https://docs.lemonsqueezy.com/api/subscriptions#update-a-subscription
144
+ LemonSqueezy::Subscription.update id: 123, variant_id: 123
97
145
 
98
146
  # Pauses a Subscription
99
147
  # Kind should be void or free
100
148
  # resumes_at can be nil or an ISO-8601 formatted date-time string indicating
101
149
  # when the subscription will continue collecting payments.
102
- @client.subscriptions.pause id: 123, kind: "void", resumes_at: nil
150
+ LemonSqueezy::Subscription.pause id: 123, kind: "void", resumes_at: nil
103
151
 
104
152
  # Un-Pause a Subscription
105
- @client.subscriptions.unpause id: 123
153
+ LemonSqueezy::Subscription.unpause id: 123
106
154
 
107
155
  # Cancel a Subscription
108
- @client.subscriptions.cancel id: 123
156
+ LemonSqueezy::Subscription.cancel id: 123
109
157
 
110
158
  # Un-Cancel a Subscription
111
- @client.subscriptions.uncancel id: 123
159
+ LemonSqueezy::Subscription.uncancel id: 123
112
160
 
113
161
  # Change the Plan for a Subscription
114
- @client.subscriptions.change_plan id: 123, plan_id: 111, variant_id: 111
162
+ # invoice_immediately and disable_prorations are optional and false by default.
163
+ LemonSqueezy::Subscription.change_plan id: 123, plan_id: 111, variant_id: 111, invoice_immediately: false, disable_prorations: false
115
164
  ```
116
165
 
117
166
  ### Subscription Invoices
118
167
 
119
168
  ```ruby
120
169
  # Retrieves a list of Subscription Invoices
121
- @client.subscription_invoices.list
170
+ LemonSqueezy::SubscriptionInvoice.list
122
171
 
123
172
  # Retrieves a list of Subscription Invoices for a specified Store
124
- @client.subscription_invoices.list store_id: 123
173
+ LemonSqueezy::SubscriptionInvoice.list store_id: 123
174
+
175
+ # Retrieves a list of Subscription Invoices for a specified Subscription
176
+ LemonSqueezy::SubscriptionInvoice.list subscription_id: 123
125
177
 
126
178
  # Retrieves a Subscription Invoice
127
- @client.subscription_invoices.get id: "123"
179
+ LemonSqueezy::SubscriptionInvoice.retrieve id: 123
180
+ ```
181
+
182
+ ### Subscription Items
183
+
184
+ ```ruby
185
+ # Retrieves a list of Subscription Items
186
+ LemonSqueezy::SubscriptionItem.list
187
+
188
+ # Retrieves a list of Subscription Items for a specified Subscription
189
+ LemonSqueezy::SubscriptionItem.list subscription_id: 123
190
+
191
+ # Retrieves a Subscription Item
192
+ LemonSqueezy::SubscriptionItem.retrieve id: 123
193
+
194
+ # Updates a Subscription Item
195
+ # https://docs.lemonsqueezy.com/api/subscription-items#update-a-subscription-item
196
+ LemonSqueezy::SubscriptionItem.update id: 123, quantity: 2
197
+
198
+ # Retrieve subscription item's current usage
199
+ # Returns 404 if the product/variant does not have usage based billing enabled
200
+ # https://docs.lemonsqueezy.com/api/subscription-items#retrieve-a-subscription-item-s-current-usage
201
+ LemonSqueezy::SubscriptionItem.current_usage id: 123
128
202
  ```
129
203
 
130
204
  ### Discounts
131
205
 
132
206
  ```ruby
133
207
  # Retrieves a list of Discounts
134
- @client.discounts.list
208
+ LemonSqueezy::Discount.list
135
209
 
136
210
  # Retrieves a list of Discounts for a store
137
- @client.discounts.list store_id: 123
211
+ LemonSqueezy::Discount.list store_id: 123
138
212
 
139
213
  # Retrieves a Discount
140
- @client.discounts.get id: "123"
214
+ LemonSqueezy::Discount.retrieve id: 123
215
+
216
+ # Create a Discount
217
+ # https://docs.lemonsqueezy.com/api/discounts#create-a-discount
218
+ LemonSqueezy::Discount.create store_id: 123, name: "20 Off", code: "20OFF", amount: 20, amount_type: "percent"
219
+
220
+ # Delete a Discount
221
+ LemonSqueezy::Discount.delete id: 123
141
222
  ```
142
223
 
143
- ### License Keys
224
+ ### Discount Redemptions
144
225
 
145
226
  ```ruby
146
- # Retrieves a list of License Keys
147
- @client.license_keys.list
227
+ # Retrieves a list of Discount Redemptions
228
+ LemonSqueezy::DiscountRedemption.list
148
229
 
149
- # Retrieves a list of License Keys for a store
150
- @client.license_keys.list store_id: 123
230
+ # Retrieves a list of Discount Redemptions for a Discount
231
+ LemonSqueezy::DiscountRedemption.list discount_id: 123
151
232
 
152
- # Retrieves a list of License Keys for an order
153
- @client.license_keys.list order_id: 123
233
+ # Retrieves a list of Discount Redemptions for a Order
234
+ LemonSqueezy::DiscountRedemption.list order_id: 123
154
235
 
155
- # Retrieves a Discount
156
- @client.license_keys.get id: "123"
236
+ # Retrieves a Discount Redemption
237
+ LemonSqueezy::DiscountRedemption.retrieve id: 123
157
238
  ```
158
239
 
159
- ### Files
240
+ ### Licenses
241
+
242
+ This API is used to manage licenses generated by Lemon Squeezy. [Docs](https://docs.lemonsqueezy.com/help/licensing/license-api)
160
243
 
161
244
  ```ruby
162
- # Retrieves a list of Files
163
- @client.files.list
245
+ # Validate a license key
246
+ LemonSqueezy::License.validate key: "abc123-abc123-abc123"
164
247
 
165
- # Retrieves a File
166
- @client.files.get id: "123"
248
+ # Activate a license key
249
+ # Instance is a label to identify this activation
250
+ LemonSqueezy::License.activate key: "abc123-abc123-abc123", instance: "Activation 1"
251
+
252
+ # Deactivate a license key
253
+ # Instance is the License Key Instance ID
254
+ LemonSqueezy::License.deactivate key: "abc123-abc123-abc123", instance: "abc123"
167
255
  ```
168
256
 
169
- ### Customers
257
+ ### License Keys
170
258
 
171
259
  ```ruby
172
- # Retrieves a list of Customers
173
- @client.customers.list
260
+ # Retrieves a list of License Keys
261
+ LemonSqueezy::LicenseKey.list
174
262
 
175
- # Retrieves a list of Customers for a store
176
- @client.customers.list store_id: 123
263
+ # Retrieves a list of License Keys for a store
264
+ LemonSqueezy::LicenseKey.list store_id: 123
177
265
 
178
- # Retrieves a list of Customers that contain the supplied email
179
- @client.customers.list email: "test@hello.com"
266
+ # Retrieves a list of License Keys for an order
267
+ LemonSqueezy::LicenseKey.list order_id: 123
180
268
 
181
- # Retrieves a Customer
182
- @client.customers.get id: "123"
269
+ # Retrieves a License Key
270
+ LemonSqueezy::LicenseKey.retrieve id: 123
271
+
272
+ # Updates a License Key
273
+ # https://docs.lemonsqueezy.com/api/license-keys#update-a-license-key
274
+ LemonSqueezy::LicenseKey.update id: 123, activation_limit: 10, disabled: false
183
275
  ```
184
276
 
185
277
  ### License Key Instances
186
278
 
187
279
  ```ruby
188
280
  # Retrieves a list of License Key Instances
189
- @client.license_key_instances.list
281
+ LemonSqueezy::LicenseKeyInstances.list
190
282
 
191
283
  # Retrieves a list of License Key Instances for a license key
192
- @client.license_key_instances.list license_key_id: 123
284
+ LemonSqueezy::LicenseKeyInstances.list license_key_id: 123
193
285
 
194
286
  # Retrieves a License Key Instance
195
- @client.license_key_instances.get id: "123"
287
+ LemonSqueezy::LicenseKeyInstances.retrieve id: 123
288
+ ```
289
+
290
+ ### Files
291
+
292
+ ```ruby
293
+ # Retrieves a list of Files
294
+ LemonSqueezy::File.list
295
+
296
+ # Retrieves a File
297
+ LemonSqueezy::File.retrieve id: 123
196
298
  ```
197
299
 
198
300
  ### Checkouts
199
301
 
200
302
  ```ruby
201
303
  # Retrieves a list of Checkouts
202
- @client.checkouts.list
304
+ LemonSqeezy::Checkout.list
203
305
 
204
306
  # Retrieves a list of Checkouts for a store
205
- @client.checkouts.list store_id: 123
307
+ LemonSqeezy::Checkout.list store_id: 123
206
308
 
207
309
  # Retrieves a list of Checkouts for a variant
208
- @client.checkouts.list variant_id: 123
310
+ LemonSqeezy::Checkout.list variant_id: 123
209
311
 
210
312
  # Retrieves a Checkout
211
- @client.checkouts.get id: "123"
313
+ LemonSqeezy::Checkout.retrieve id: 123
212
314
 
213
315
  # Creates a Checkout
214
316
  # View docs for more info: https://docs.lemonsqueezy.com/api/checkouts#create-a-checkout
215
317
  # store_id and variant_id are required
216
318
  # Any other parameters are send as "attributes" to the API
217
- @client.checkouts.create store_id: 123, variant_id: 321, custom_price: 500, product_options: {name: "a test name"}
319
+ LemonSqueezy::Checkout.create store_id: 123, variant_id: 321, custom_price: 500, product_options: {name: "a test name"}
320
+ ```
321
+
322
+ ### Webhooks
323
+
324
+ ```ruby
325
+ # List all Webhooks
326
+ LemonSqueezy::Webhook.list
327
+
328
+ # List all Webhooks for a Store
329
+ LemonSqueezy::Webhook.list store_id: 123
330
+
331
+ # Retrieve a Webhook
332
+ LemonSqueezy::Webhook.retrieve id: 123
333
+
334
+ # Create a Webhook
335
+ # https://docs.lemonsqueezy.com/api/webhooks#create-a-webhook
336
+ LemonSqueezy::Webhook.create store_id: 123, url: "", secret: "abc", events: ["order_created"]
337
+
338
+ # Update a Webhook
339
+ # https://docs.lemonsqueezy.com/api/webhooks#update-a-webhook
340
+ LemonSqueezy::Webhook.update id: 123, events: ["order_created", "subscription_created"]
341
+
342
+ # Delete a Webhook
343
+ LemonSqueezy::Webhook.delete id: 123
218
344
  ```
219
345
 
220
346
 
data/bin/console CHANGED
@@ -14,7 +14,9 @@ require 'dotenv/load'
14
14
  # require "pry"
15
15
  # Pry.start
16
16
 
17
- @client = LemonSqueezy::Client.new(access_token: ENV["ACCESS_TOKEN"])
17
+ LemonSqueezy.configure do |config|
18
+ config.api_key = ENV["LEMON_SQUEEZY_API_KEY"]
19
+ end
18
20
 
19
21
  require "irb"
20
22
  IRB.start(__FILE__)
@@ -1,80 +1,74 @@
1
1
  module LemonSqueezy
2
2
  class Client
3
- BASE_URL = "https://api.lemonsqueezy.com/v1"
4
3
 
5
- attr_reader :access_token, :adapter
4
+ class << self
6
5
 
7
- def initialize(access_token:, adapter: Faraday.default_adapter, stubs: nil)
8
- @access_token = access_token
9
- @adapter = adapter
6
+ def connection
7
+ @connection ||= Faraday.new("https://api.lemonsqueezy.com/v1") do |conn|
8
+ conn.request :authorization, :Bearer, LemonSqueezy.config.api_key
10
9
 
11
- # Test stubs for requests
12
- @stubs = stubs
13
- end
14
-
15
- def stores
16
- StoresResource.new(self)
17
- end
18
-
19
- def products
20
- ProductsResource.new(self)
21
- end
22
-
23
- def variants
24
- VariantsResource.new(self)
25
- end
10
+ conn.headers = {
11
+ "User-Agent" => "lemonsqueezy/v#{VERSION} (github.com/deanpcmad/lemonsqueezy)",
12
+ "Accept" => "application/vnd.api+json",
13
+ "Content-Type" => "application/vnd.api+json"
14
+ }
26
15
 
27
- def orders
28
- OrdersResource.new(self)
29
- end
30
-
31
- def subscriptions
32
- SubscriptionsResource.new(self)
33
- end
16
+ conn.request :json
34
17
 
35
- def subscription_invoices
36
- SubscriptionInvoicesResource.new(self)
37
- end
18
+ conn.response :json
19
+ end
20
+ end
38
21
 
39
- def discounts
40
- DiscountsResource.new(self)
41
- end
22
+ def get_request(url, params: {}, headers: {})
23
+ handle_response connection.get(url, params, headers)
24
+ end
42
25
 
43
- def license_keys
44
- LicenseKeysResource.new(self)
45
- end
26
+ def post_request(url, body: {}, headers: {})
27
+ handle_response connection.post(url, body, headers)
28
+ end
46
29
 
47
- def files
48
- FilesResource.new(self)
49
- end
30
+ def patch_request(url, body:, headers: {})
31
+ handle_response connection.patch(url, body, headers)
32
+ end
50
33
 
51
- def customers
52
- CustomersResource.new(self)
53
- end
34
+ def delete_request(url, headers: {})
35
+ handle_response connection.delete(url, headers)
36
+ end
54
37
 
55
- def license_key_instances
56
- LicenseKeyInstancesResource.new(self)
57
- end
38
+ def handle_response(response)
39
+ case response.status
40
+ when 400
41
+ raise Error, "Error 400: Your request was malformed. '#{response.body["errors"]}'"
42
+ when 401
43
+ raise Error, "Error 401: You did not supply valid authentication credentials. '#{response.body["errors"]}'"
44
+ when 403
45
+ raise Error, "Error 403: You are not allowed to perform that action. '#{response.body["errors"]}'"
46
+ when 404
47
+ raise Error, "Error 404: No results were found for your request. '#{response.body["errors"]}'"
48
+ when 409
49
+ raise Error, "Error 409: Your request was a conflict. '#{response.body["errors"]}'"
50
+ when 429
51
+ raise Error, "Error 429: Your request exceeded the API rate limit. '#{response.body["errors"]}'"
52
+ when 422
53
+ raise Error, "Error 422: Unprocessable Entity. '#{response.body["errors"]}'"
54
+ when 500
55
+ raise Error, "Error 500: We were unable to perform the request due to server-side problems. '#{response.body["errors"]}'"
56
+ when 503
57
+ raise Error, "Error 503: You have been rate limited for sending more than 20 requests per second. '#{response.body["errors"]}'"
58
+ when 501
59
+ raise Error, "Error 501: This resource has not been implemented. '#{response.body["errors"]}'"
60
+ when 204
61
+ return true
62
+ end
63
+
64
+ if response.body && response.body["error"]
65
+ raise Error, "Error #{response.body["error"]["code"]} - #{response.body["errors"]["message"]}"
66
+ end
67
+
68
+ response
69
+ end
58
70
 
59
- def checkouts
60
- CheckoutsResource.new(self)
61
71
  end
62
72
 
63
- def connection
64
- @connection ||= Faraday.new(BASE_URL) do |conn|
65
- conn.request :authorization, :Bearer, access_token
66
-
67
- conn.headers = {
68
- "User-Agent" => "lemonsqueezy/v#{VERSION} (github.com/deanpcmad/lemonsqueezy)",
69
- "Accept" => "application/vnd.api+json",
70
- "Content-Type" => "application/vnd.api+json",
71
- }
72
-
73
- conn.request :json
74
- conn.response :json
75
-
76
- conn.adapter adapter, @stubs
77
- end
78
- end
79
73
  end
80
74
  end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LemonSqueezy
4
+ class Configuration
5
+
6
+ attr_accessor :api_key
7
+
8
+ def initialize
9
+ end
10
+
11
+ end
12
+ end