patreon 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bee2755fa7bbe4c0dcb717fb13f8d5cf1b06cee5
4
- data.tar.gz: d529a387ad7c3ed3db7827bdd22ac1a3a9bd8b15
3
+ metadata.gz: ad676d15579d0bcd7c803e890eb2036d2100ec8b
4
+ data.tar.gz: 322b087b65fd2ebbd21b6dd48a162abf173395e2
5
5
  SHA512:
6
- metadata.gz: 0c6b2ea5492326a1488fe824053dd338b832abb052f0c1f61d3c57571a684c66348ce1d73c449bdd110bd00c40d5f98b4c5d19262c221efb05d165f65c5c3887
7
- data.tar.gz: 777e1d8b36da247e966769486495f9d1b61590bc50a72450b91c879beb53652362c4c52ff6919b67c17bb0e90f48d0dbb986b36c3751d0f782c4e134e694272c
6
+ metadata.gz: 180b08e913282461e376b555dac658b3434acfef272c6ca86d5e5c2ea05e6d48bb7de3a6dd2f805b7598b9457a14e22f35caba00ff41fb90dc2a0cce0cfdb265
7
+ data.tar.gz: f167cf347b909ae43c20e8f3304807fff873225bc72e3c921e7792209bb1afa8309582e23bb7d730ffb260ddf28153058b3d62d0ed1955028c787fc96bbebe4d
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,13 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ json-api-vanilla (1.0.1)
5
+
6
+ PLATFORMS
7
+ ruby
8
+
9
+ DEPENDENCIES
10
+ json-api-vanilla (~> 1.0.1)
11
+
12
+ BUNDLED WITH
13
+ 1.15.1
data/README.md CHANGED
@@ -24,15 +24,12 @@ class OAuthController < ApplicationController
24
24
 
25
25
  api_client = Patreon::API.new(access_token)
26
26
  user_response = api_client.fetch_user()
27
- @user = user_response['data']
28
- included = user_response['included']
29
- if included
30
- @pledge = included.find {|obj| obj['type'] == 'pledge' && obj['relationships']['creator']['data']['id'] == creator_id}
31
- @campaign = included.find {|obj| obj['type'] == 'campaign' && obj['relationships']['creator']['data']['id'] == creator_id}
32
- else
33
- @pledge = nil
34
- @campaign = nil
35
- end
27
+ # user_response uses [json-api-vanilla](https://github.com/trainline/json-api-vanilla) for easy usage
28
+ @user = user_response.data
29
+ # you can list all attributes and relationships with (@user.methods - Object.methods)
30
+ @pledge = @user.pledges[0]
31
+ # just like with @user, you can list all pledge attributes and relationships with (@pledge.methods - Object.methods)
32
+ @pledge_amount = @pledge.amount_cents
36
33
  end
37
34
  end
38
35
  ```
@@ -0,0 +1,39 @@
1
+ require_relative 'lib/patreon'
2
+ require 'uri'
3
+ require 'cgi'
4
+
5
+ # API key constants. Register at https://www.patreon.com/platform to get these values
6
+ # client_id = nil
7
+ # client_secret = nil
8
+ # redirect_uri = nil
9
+ # refresh_token = nil # your Creator Refresh Token
10
+
11
+ # Fetching updated tokens
12
+ oauth_client = Patreon::OAuth.new(client_id, client_secret)
13
+ tokens = oauth_client.refresh_token(refresh_token, redirect_uri)
14
+ access_token = tokens['access_token']
15
+
16
+ # Fetching basic data
17
+ api_client = Patreon::API.new(access_token)
18
+
19
+ campaign_response = api_client.fetch_campaign()
20
+ campaign_id = campaign_response.data[0].id
21
+
22
+ # Fetching all pledges
23
+ all_pledges = []
24
+ cursor = nil
25
+ while true do
26
+ page_response = api_client.fetch_page_of_pledges(campaign_id, 25, cursor)
27
+ all_pledges += page_response.data
28
+ next_page_link = page_response.links[page_response.data]['next']
29
+ if next_page_link
30
+ parsed_query = CGI::parse(next_page_link)
31
+ cursor = parsed_query['page[cursor]'][0]
32
+ else
33
+ break
34
+ end
35
+ end
36
+
37
+ # Mapping to all patrons. Feel free to customize as needed.
38
+ # As with all standard Ruby objects, (pledge.methods - Object.methods) will list the available attributes and relationships
39
+ puts all_pledges.map{ |pledge| { full_name: pledge.patron.full_name, amount_cents: pledge.amount_cents } }
@@ -0,0 +1,2 @@
1
+ config.rb
2
+ sqlite.db
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'sinatra', '~>1.4.7'
4
+ gem 'thin', '~>1.1'
5
+ gem 'haml', '~>4.0.7'
6
+ gem 'data_mapper', '~>1.2.0'
7
+ gem 'dm-sqlite-adapter', '~>1.2.0'
8
+ gem 'patreon', :path => './../../'
@@ -0,0 +1,98 @@
1
+ PATH
2
+ remote: ./../../
3
+ specs:
4
+ patreon (0.2.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.5.0)
10
+ public_suffix (~> 2.0, >= 2.0.2)
11
+ bcrypt (3.1.11)
12
+ bcrypt-ruby (3.1.5)
13
+ bcrypt (>= 3.1.3)
14
+ daemons (1.2.4)
15
+ data_mapper (1.2.0)
16
+ dm-aggregates (~> 1.2.0)
17
+ dm-constraints (~> 1.2.0)
18
+ dm-core (~> 1.2.0)
19
+ dm-migrations (~> 1.2.0)
20
+ dm-serializer (~> 1.2.0)
21
+ dm-timestamps (~> 1.2.0)
22
+ dm-transactions (~> 1.2.0)
23
+ dm-types (~> 1.2.0)
24
+ dm-validations (~> 1.2.0)
25
+ data_objects (0.10.17)
26
+ addressable (~> 2.1)
27
+ dm-aggregates (1.2.0)
28
+ dm-core (~> 1.2.0)
29
+ dm-constraints (1.2.0)
30
+ dm-core (~> 1.2.0)
31
+ dm-core (1.2.1)
32
+ addressable (~> 2.3)
33
+ dm-do-adapter (1.2.0)
34
+ data_objects (~> 0.10.6)
35
+ dm-core (~> 1.2.0)
36
+ dm-migrations (1.2.0)
37
+ dm-core (~> 1.2.0)
38
+ dm-serializer (1.2.2)
39
+ dm-core (~> 1.2.0)
40
+ fastercsv (~> 1.5)
41
+ json (~> 1.6)
42
+ json_pure (~> 1.6)
43
+ multi_json (~> 1.0)
44
+ dm-sqlite-adapter (1.2.0)
45
+ dm-do-adapter (~> 1.2.0)
46
+ do_sqlite3 (~> 0.10.6)
47
+ dm-timestamps (1.2.0)
48
+ dm-core (~> 1.2.0)
49
+ dm-transactions (1.2.0)
50
+ dm-core (~> 1.2.0)
51
+ dm-types (1.2.2)
52
+ bcrypt-ruby (~> 3.0)
53
+ dm-core (~> 1.2.0)
54
+ fastercsv (~> 1.5)
55
+ json (~> 1.6)
56
+ multi_json (~> 1.0)
57
+ stringex (~> 1.4)
58
+ uuidtools (~> 2.1)
59
+ dm-validations (1.2.0)
60
+ dm-core (~> 1.2.0)
61
+ do_sqlite3 (0.10.17)
62
+ data_objects (= 0.10.17)
63
+ eventmachine (1.2.1)
64
+ fastercsv (1.5.5)
65
+ haml (4.0.7)
66
+ tilt
67
+ json (1.8.3)
68
+ json_pure (1.8.3)
69
+ multi_json (1.12.1)
70
+ public_suffix (2.0.4)
71
+ rack (1.6.5)
72
+ rack-protection (1.5.3)
73
+ rack
74
+ sinatra (1.4.7)
75
+ rack (~> 1.5)
76
+ rack-protection (~> 1.4)
77
+ tilt (>= 1.3, < 3)
78
+ stringex (1.5.1)
79
+ thin (1.7.0)
80
+ daemons (~> 1.0, >= 1.0.9)
81
+ eventmachine (~> 1.0, >= 1.0.4)
82
+ rack (>= 1, < 3)
83
+ tilt (2.0.5)
84
+ uuidtools (2.1.5)
85
+
86
+ PLATFORMS
87
+ ruby
88
+
89
+ DEPENDENCIES
90
+ data_mapper (~> 1.2.0)
91
+ dm-sqlite-adapter (~> 1.2.0)
92
+ haml (~> 4.0.7)
93
+ patreon!
94
+ sinatra (~> 1.4.7)
95
+ thin (~> 1.1)
96
+
97
+ BUNDLED WITH
98
+ 1.13.6
@@ -0,0 +1,13 @@
1
+ # Patreon OAuth Example: Sinatra Server
2
+
3
+ Simple website made with Sinatra and the Patreon OAuth API
4
+
5
+ ## Install
6
+
7
+ 1. `bundle install`
8
+ 2. Fill out `MyConfig` in `app.rb` with your client details
9
+
10
+ ## Running the Server
11
+
12
+ 1. `bundle exec ruby app.rb`
13
+ 2. Visit [http://localhost:4567](http://localhost:4567)
@@ -0,0 +1,19 @@
1
+ require 'sinatra/base'
2
+
3
+ class MyApp < Sinatra::Application
4
+ enable :sessions
5
+ set :session_secret, 'my app has a secret'
6
+ end
7
+
8
+ class MyConfig
9
+ # Fill all this out with your information
10
+ PATREON_CLIENT_ID = nil
11
+ PATREON_CLIENT_SECRET = nil
12
+ PATREON_CREATOR_ID = nil
13
+ PATREON_REDIRECT_URI = nil
14
+ end
15
+
16
+ require_relative 'models/table/init'
17
+ require_relative 'routes/init'
18
+
19
+ MyApp.run! if MyApp.app_file == $0
@@ -0,0 +1,40 @@
1
+ require 'patreon'
2
+
3
+ class PatreonUserManager
4
+ def self.create_or_update_user_with_tokens(patreon_refresh_token, patreon_access_token)
5
+ # https://www.patreon.com/platform/documentation/oauth -- Step 4
6
+ # Use the tokens to fetch the Patreon profile and pledge data, saving it all to the db.
7
+
8
+ return nil unless patreon_refresh_token and patreon_access_token
9
+
10
+ # Make the API client with the patron's access token
11
+ api_client = Patreon::API.new(patreon_access_token)
12
+
13
+ # Get the patron's profile info and pledge amount
14
+ user_response = api_client.fetch_user()
15
+ patreon_user_data = user_response['data']
16
+ return nil unless patreon_user_data
17
+
18
+ # Find or make the user, and set their information using the API response
19
+ db_user = User.first_or_create({:patreon_user_id => patreon_user_data['id']})
20
+ db_user.update({
21
+ :full_name => patreon_user_data['attributes']['full_name'],
22
+ :email => patreon_user_data['attributes']['email'],
23
+ :patreon_refresh_token => patreon_refresh_token,
24
+ :patreon_access_token => patreon_access_token
25
+ })
26
+
27
+ # Find the user's pledge to us, and if they have one, update their pledge amount in our db
28
+ pledge = (user_response['included'] || []).find {|obj|
29
+ obj['type'] == 'pledge' and obj['relationships']['creator']['data']['id'] == MyConfig::PATREON_CREATOR_ID
30
+ }
31
+ if pledge
32
+ db_user.update({
33
+ :patreon_pledge_amount_cents => pledge['attributes']['amount_cents']
34
+ })
35
+ end
36
+
37
+ # Return the user we've made or updated
38
+ return db_user
39
+ end
40
+ end
@@ -0,0 +1,8 @@
1
+ require "data_mapper"
2
+
3
+ DataMapper::setup(:default, "sqlite://#{Dir.pwd}/sqlite.db")
4
+
5
+ require_relative 'user'
6
+
7
+ DataMapper.finalize
8
+ DataMapper.auto_upgrade!
@@ -0,0 +1,13 @@
1
+ require "data_mapper"
2
+
3
+ class User
4
+ include DataMapper::Resource
5
+
6
+ property :user_id, Serial
7
+ property :full_name, String
8
+ property :email, String
9
+ property :patreon_pledge_amount_cents, Integer
10
+ property :patreon_user_id, String
11
+ property :patreon_refresh_token, String
12
+ property :patreon_access_token, String
13
+ end
@@ -0,0 +1,47 @@
1
+ require_relative '../models/manager/patreon_user_mgr'
2
+ require 'patreon'
3
+
4
+ class MyApp < Sinatra::Application
5
+ get '/' do
6
+ # https://www.patreon.com/platform/documentation/oauth -- Step 1
7
+ # The landing page links to patreon.com/oauth2/authorize so the user can authorize this app to access their Patreon data.
8
+ @log_in_url = (
9
+ "https://www.patreon.com/oauth2/authorize" \
10
+ "?response_type=code" \
11
+ "&client_id=%{client_id}" \
12
+ "&redirect_uri=%{redirect_uri}"
13
+ ) % {
14
+ :client_id => MyConfig::PATREON_CLIENT_ID,
15
+ :redirect_uri => MyConfig::PATREON_REDIRECT_URI
16
+ }
17
+ haml :login
18
+ end
19
+
20
+ get '/oauth/redirect' do
21
+ # https://www.patreon.com/platform/documentation/oauth -- Step 2
22
+ # After authorizing this app to access their Patreon data, the user is redirected back here.
23
+
24
+ # https://www.patreon.com/platform/documentation/oauth -- Step 3
25
+ # Use the code provided as a query parameter to get the user's access token and refresh token
26
+ oauth_client = Patreon::OAuth.new(MyConfig::PATREON_CLIENT_ID, MyConfig::PATREON_CLIENT_SECRET)
27
+ tokens = oauth_client.get_tokens(params['code'], 'http://localhost:4567/oauth/redirect')
28
+
29
+ # https://www.patreon.com/platform/documentation/oauth -- Step 4
30
+ # Save off the user's tokens and fetch their Patreon data.
31
+ user = PatreonUserManager.create_or_update_user_with_tokens(
32
+ patreon_refresh_token=tokens['refresh_token'],
33
+ patreon_access_token=tokens['access_token']
34
+ )
35
+
36
+ # https://www.patreon.com/platform/documentation/oauth -- Step 5
37
+ # If the user signed in successfully, take them to their profile page.
38
+ halt 403 unless user
39
+ redirect to("/users/%{user_id}" % {:user_id => user.user_id})
40
+ end
41
+
42
+ get '/users/:user_id' do
43
+ @user = User.get(params['user_id'])
44
+ halt 404 unless @user
45
+ haml :profile
46
+ end
47
+ end
@@ -0,0 +1 @@
1
+ require_relative 'auth'
@@ -0,0 +1,9 @@
1
+ !!! XML
2
+ !!! 1.1
3
+ %html(xmlns="http://www.w3.org/1999/xhtml")
4
+ %head
5
+ %meta(charset="UTF-8")
6
+ %title= "Patreon OAuth Example: Sinatra Server"
7
+ %body
8
+ %h1= "Patreon OAuth Example: Sinatra Server"
9
+ #content= yield
@@ -0,0 +1 @@
1
+ %a(href=@log_in_url)= "Log In with Patreon"
@@ -0,0 +1,11 @@
1
+ %h2= "Logged In!"
2
+
3
+ %dl
4
+ %dt= "Name"
5
+ %dd= @user.full_name
6
+
7
+ %dt= "Email"
8
+ %dd= @user.email
9
+
10
+ %dt= "Pledge Amount (in cents)"
11
+ %dd= @user.patreon_pledge_amount_cents
@@ -1,6 +1,7 @@
1
1
  require 'net/http'
2
2
  require 'cgi'
3
3
  require 'json'
4
+ require 'json-api-vanilla'
4
5
 
5
6
  module Patreon
6
7
  class API
@@ -31,12 +32,11 @@ module Patreon
31
32
  private
32
33
 
33
34
  def get_json(suffix)
34
- print "getting suffix #{suffix}"
35
- url = URI.parse("https://api.patreon.com/oauth2/api/#{suffix}")
35
+ url = URI.parse("https://www.patreon.com/api/oauth2/api/#{suffix}")
36
36
  req = Net::HTTP::Get.new(url.to_s)
37
37
  req['Authorization'] = "Bearer #{@access_token}"
38
38
  res = Net::HTTP.start(url.host, url.port, :use_ssl => true) {|http| http.request(req)}
39
- JSON.parse(res.body)
39
+ return JSON::Api::Vanilla.parse(res.body)
40
40
  end
41
41
  end
42
42
  end
@@ -30,11 +30,11 @@ module Patreon
30
30
  private
31
31
 
32
32
  def update_token(params)
33
- url = URI.parse('https://api.patreon.com/oauth2/token')
33
+ url = URI.parse('https://www.patreon.com/api/oauth2/token')
34
34
  url.query = URI.encode_www_form(params)
35
35
  req = Net::HTTP::Post.new(url.to_s)
36
36
  res = Net::HTTP.start(url.host, url.port, :use_ssl => true) {|http| http.request(req)}
37
37
  JSON.parse(res.body)
38
38
  end
39
39
  end
40
- end
40
+ end
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = "patreon"
7
- gem.version = "0.2.0"
7
+ gem.version = "0.3.0"
8
8
  gem.authors = ["Patreon"]
9
9
  gem.email = ["david@patreon.com"]
10
10
  gem.description = "Interact with the Patreon API via OAuth"
@@ -16,4 +16,6 @@ Gem::Specification.new do |gem|
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency 'json-api-vanilla', '~> 1.0.1'
19
21
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patreon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patreon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-21 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2017-10-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json-api-vanilla
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.1
13
27
  description: Interact with the Patreon API via OAuth
14
28
  email:
15
29
  - david@patreon.com
@@ -18,9 +32,25 @@ extensions: []
18
32
  extra_rdoc_files: []
19
33
  files:
20
34
  - ".gitignore"
35
+ - Gemfile
36
+ - Gemfile.lock
21
37
  - LICENSE
22
38
  - NOTICE
23
39
  - README.md
40
+ - example/fetch_all_patrons.rb
41
+ - example/sinatra/.gitignore
42
+ - example/sinatra/Gemfile
43
+ - example/sinatra/Gemfile.lock
44
+ - example/sinatra/README.md
45
+ - example/sinatra/app.rb
46
+ - example/sinatra/models/manager/patreon_user_mgr.rb
47
+ - example/sinatra/models/table/init.rb
48
+ - example/sinatra/models/table/user.rb
49
+ - example/sinatra/routes/auth.rb
50
+ - example/sinatra/routes/init.rb
51
+ - example/sinatra/views/layout.haml
52
+ - example/sinatra/views/login.haml
53
+ - example/sinatra/views/profile.haml
24
54
  - lib/patreon.rb
25
55
  - lib/patreon/api.rb
26
56
  - lib/patreon/oauth.rb
@@ -53,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
83
  version: '0'
54
84
  requirements: []
55
85
  rubyforge_project:
56
- rubygems_version: 2.4.8
86
+ rubygems_version: 2.5.1
57
87
  signing_key:
58
88
  specification_version: 4
59
89
  summary: Visit patreon.com/oauth2/documentation for more information.