peasy 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3a9b69951d0814fb7608a77fee8bae0cd9949355459cb1b0eeabffc1eeba58bb
4
- data.tar.gz: 2bf6ce4951bc1e2664a4c608104fa3c1c4c37b9e69ab78a1aa49f5433508245f
3
+ metadata.gz: 50497c8baa8e234e8b5720ccdffd583cf7a9947efdb84d55358dc0a2bfeeedd2
4
+ data.tar.gz: 931ecbb25fcb358d0e7784dabf75a752b0501a5839ecf70fce5256841519cdda
5
5
  SHA512:
6
- metadata.gz: 30f1f8a38b633d139367c14ac3484079136d0cffa313f4816b4013ea6560a0158c10f4cc9d6864faa29071acc17cfc8c40af2986a1fd6e605e2205f770342878
7
- data.tar.gz: 8abb797d706b00d5f8668fcd989eb0d4b80e87dee4271b8bf16b479b7377c8999a9d2f898ca2480a02f4ba63db055a9402b53b2743e5c39d8d50d50196d75979
6
+ metadata.gz: a8b95e344c4b1508a4ab4ba99625159896c49622d9394beea6b5c8973ca0798e6c74aab80aebe949ae9bad786e788be5055bd061c7731910a2aa8237161dd00e
7
+ data.tar.gz: dbec525bd69d16b78fcd2624bac6f91749b973e8236db5a34cd28b913d6c52b33e77c8cb6d65e90b1095175d3cfd77ada5872fd5d1076de0aaea163dde799c75
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # Peasy
2
+ [![Gem Version](https://badge.fury.io/rb/peasy.svg)](https://badge.fury.io/rb/peasy)
2
3
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/peasy`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+ An Easy Peasy way to use LemonSqueezy.
4
5
 
5
- TODO: Delete this and the text above, and describe your gem
6
+ This gem lets you use the LemonSqueezy API to manage subscriptions from your application.
6
7
 
7
8
  ## Installation
8
9
 
@@ -22,17 +23,36 @@ Or install it yourself as:
22
23
 
23
24
  ## Usage
24
25
 
25
- TODO: Write usage instructions here
26
+ This is super basic, and there are currently no tests. Probably don't use in production unless you know what the code does.
26
27
 
27
- ## Development
28
+ ### Create Initializer
29
+ Add a `peasy.rb` file to your initializers folder, defining your API key. The LemonSqueezy API keys don't always play nice with Rails credentials, so you might need to split if over several lines in your credentials and then gsub the linebreaks.
28
30
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
31
+ ```
32
+ Peasy.configure do |config|
33
+ config.key = Rails.application.credentials.lemonsqueezy_api_key
34
+ end
35
+ ```
36
+
37
+ ### Get Subscription
30
38
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
39
+ If you know the subscription ID from LemonSqueezy (likely you've saved it already to your application's database), you can query the API to get the response.
40
+
41
+ ```
42
+ Peasy::Subscription.find(API_KEY, SUBSCRIPTION_ID)
43
+ ```
44
+
45
+ ### Cancel Subscription
46
+
47
+ As above, if you know the subscription ID from LemonSqueezy, you can instruct the API to cancel the relevant subscription.
48
+
49
+ ```
50
+ Peasy::Subscription.cancel(API_KEY, SUBSCRIPTION_ID)
51
+ ```
32
52
 
33
- ## Contributing
53
+ ## To Do
34
54
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/peasy.
55
+ There's a lot of endpoints left to map here, and there's also going to be some refactoring to do and tests to write
36
56
 
37
57
  ## License
38
58
 
@@ -0,0 +1,5 @@
1
+ module Peasy
2
+ class Configuration
3
+ attr_accessor :key
4
+ end
5
+ end
@@ -1,6 +1,11 @@
1
1
  module Peasy
2
+
3
+ class Configuration
4
+ attr_accessor :key
5
+ end
6
+
2
7
  class Subscription
3
- def self.find(key, id)
8
+ def self.find(id)
4
9
  require 'net/http'
5
10
 
6
11
  uri = URI.parse("https://api.lemonsqueezy.com/v1/subscriptions/#{id}")
@@ -11,10 +16,34 @@ module Peasy
11
16
  request = Net::HTTP::Get.new(uri.request_uri)
12
17
  request['Content-Type'] = 'application/vnd.api+json'
13
18
  request['Accept'] = 'application/vnd.api+json'
14
- request['Authorization'] = "Bearer #{key}"
19
+ request['Authorization'] = "Bearer #{Peasy.configuration.key}"
15
20
 
16
21
  response = http.request(request)
22
+ response = JSON.parse(response.body)
17
23
 
18
24
  end
25
+
26
+ def self.change_payment_method(id)
27
+ self.find(id)['data']['attributes']['urls']['update_payment_method']
28
+ end
29
+
30
+ def self.cancel(id)
31
+ require 'net/http'
32
+
33
+ uri = URI.parse("https://api.lemonsqueezy.com/v1/subscriptions/#{id}")
34
+
35
+ http = Net::HTTP.new(uri.host, uri.port)
36
+ http.use_ssl = true if uri.instance_of? URI::HTTPS
37
+
38
+ request = Net::HTTP::Patch.new(uri.request_uri)
39
+ request['Content-Type'] = 'application/vnd.api+json'
40
+ request['Accept'] = 'application/vnd.api+json'
41
+ request['Authorization'] = "Bearer #{Peasy.configuration.key}"
42
+ request.body = {data: {type: "subscriptions", id: "#{id}", attributes: {cancelled: true}}}.to_json
43
+
44
+ response = http.request(request)
45
+ response = JSON.parse(response.body)
46
+
47
+ end
19
48
  end
20
49
  end
data/lib/peasy/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Peasy
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/peasy.rb CHANGED
@@ -1,10 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "peasy/version"
4
-
4
+ require_relative "peasy/configuration"
5
5
  require_relative 'peasy/subscription'
6
6
 
7
7
  module Peasy
8
8
  class Error < StandardError; end
9
- # Your code goes here...
9
+ def self.configuration
10
+ @configuration ||= Configuration.new
11
+ end
12
+ def self.configure(&block)
13
+ yield(configuration)
14
+ end
10
15
  end
data/peasy-0.2.0.gem ADDED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: peasy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Farnworth
@@ -28,8 +28,10 @@ files:
28
28
  - bin/console
29
29
  - bin/setup
30
30
  - lib/peasy.rb
31
+ - lib/peasy/configuration.rb
31
32
  - lib/peasy/subscription.rb
32
33
  - lib/peasy/version.rb
34
+ - peasy-0.2.0.gem
33
35
  - peasy.gemspec
34
36
  homepage: https://github.com/stevefarnworth/peasy
35
37
  licenses: