Skip to content
This repository was archived by the owner on Oct 7, 2022. It is now read-only.

Commit 0ce1cd1

Browse files
committed
Merge branch 'master' of github.com:creatubbles/ctb-api-ruby
2 parents 63e4d9f + 57ff14f commit 0ce1cd1

File tree

9 files changed

+183
-4
lines changed

9 files changed

+183
-4
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ DEPENDENCIES
4545
rspec
4646

4747
BUNDLED WITH
48-
1.11.2
48+
1.13.6

example/Gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'sinatra'
4+
gem 'puma'
5+
gem 'creatubbles', path: '../'

example/Gemfile.lock

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
PATH
2+
remote: ../
3+
specs:
4+
creatubbles (0.0.1)
5+
oauth2 (>= 1.2.0)
6+
7+
GEM
8+
remote: https://rubygems.org/
9+
specs:
10+
faraday (0.9.2)
11+
multipart-post (>= 1.2, < 3)
12+
jwt (1.5.6)
13+
multi_json (1.12.1)
14+
multi_xml (0.6.0)
15+
multipart-post (2.0.0)
16+
oauth2 (1.2.0)
17+
faraday (>= 0.8, < 0.10)
18+
jwt (~> 1.0)
19+
multi_json (~> 1.3)
20+
multi_xml (~> 0.5)
21+
rack (>= 1.2, < 3)
22+
puma (3.6.0)
23+
rack (1.6.5)
24+
rack-protection (1.5.3)
25+
rack
26+
sinatra (1.4.7)
27+
rack (~> 1.5)
28+
rack-protection (~> 1.4)
29+
tilt (>= 1.3, < 3)
30+
tilt (2.0.5)
31+
32+
PLATFORMS
33+
ruby
34+
35+
DEPENDENCIES
36+
creatubbles!
37+
puma
38+
sinatra
39+
40+
BUNDLED WITH
41+
1.13.6

example/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Example web application
2+
3+
This is a little example web application illustrating how to use the Creatubbles Ruby API.
4+
5+
## Installation on local
6+
7+
1. Get application ID & secret from Creatubbles with `https://ctbex.dev/callback` as callback URL
8+
2. Install puma-dev, for instance using `brew install puma-dev`. We need this so we can use SSL and a local domain
9+
3. Create `.env` file with `export CREATUBBLES_CLIENT_ID=...` and `export CREATUBBLES_CLIENT_SECRET=...`
10+
4. `bundle install`
11+
5. link example directory to `~/.puma-dev/ctbex`
12+
6. open `https://ctbex.dev/`

example/app.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# app.rb
2+
require 'sinatra'
3+
require 'creatubbles'
4+
require 'json'
5+
6+
enable :sessions
7+
8+
def authenticate!
9+
token = session['token']
10+
if token
11+
@current_user =
12+
begin
13+
@creatubbles_client.connect!(JSON.parse(token))
14+
@creatubbles_client.users.me
15+
rescue => err
16+
session['token'] = nil
17+
logger.info("Could not parse session token: #{err}")
18+
redirect to('/')
19+
end
20+
else
21+
redirect to('/sign_in')
22+
end
23+
end
24+
25+
before do
26+
@creatubbles_client = Creatubbles::Client.new(
27+
client_id: ENV['CREATUBBLES_CLIENT_ID'],
28+
client_secret: ENV['CREATUBBLES_CLIENT_SECRET'],
29+
api_url: ENV['CREATUBBLES_API_URL'] || Creatubbles::DEFAULT_API_URL)
30+
end
31+
32+
get '/' do
33+
@creations = @creatubbles_client.creations.recent
34+
@title = "Recent creations"
35+
erb :creations
36+
end
37+
38+
get '/sign_in' do
39+
redirect @creatubbles_client.start_code_flow(url('/callback'))
40+
end
41+
42+
get '/callback' do
43+
@creatubbles_client.complete_code_flow(params[:code], url('/callback'))
44+
logger.info(@creatubbles_client.connection_hash.to_json)
45+
session['token'] = @creatubbles_client.connection_hash.to_json
46+
redirect to('/my/creations')
47+
end
48+
49+
before '/my/*' do
50+
authenticate!
51+
end
52+
53+
get '/my/creations' do
54+
@creations = @current_user.creations
55+
@title = "My creations"
56+
erb :creations
57+
end

example/config.ru

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require './app'
2+
run Sinatra::Application

example/views/creations.erb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<div class="page-header">
2+
<h2><%= @title %></h2>
3+
</div>
4+
<ul class="media-list">
5+
<% @creations.each do |c| %>
6+
<li class="media">
7+
<div class="media-left">
8+
<img class="media-object" src="<%= c.image['links']['list_view'] %>" alt="<%= c.name %>" width="128" height="128">
9+
</div>
10+
<div class="media-body">
11+
<h4 class="media-heading"><%= c.name %></h4>
12+
<p><%= c.reflection_text %></p>
13+
</div>
14+
</li>
15+
<% end %>
16+
</ul>

example/views/layout.erb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>Creatubbles Example Integration</title>
8+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
9+
</head>
10+
<body style="padding-top: 50px;">
11+
<nav class="navbar navbar-default navbar-fixed-top">
12+
<div class="container">
13+
<div class="navbar-header">
14+
<a class="navbar-brand" href="<%= url('/') %>">Creatubbles Example</a>
15+
</div>
16+
<div id="navbar">
17+
<ul class="nav navbar-nav">
18+
<li><a href="<%= url('/sign_in') %>">Sign In</a></li>
19+
</ul>
20+
</div>
21+
</div>
22+
</nav>
23+
<div class="container">
24+
<%= yield %>
25+
</div>
26+
</body>
27+
</html>

lib/creatubbles/client.rb

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ def connection
1616
@connection ||= get_oauth_token
1717
end
1818

19+
def connect!(hash)
20+
@connection = OAuth2::AccessToken.from_hash(oauth_client, hash)
21+
end
22+
23+
def connection_hash
24+
@connection&.to_hash
25+
end
26+
1927
def disconnect!
2028
@connection = nil
2129
end
@@ -28,14 +36,25 @@ def #{type_name}
2836
EOM
2937
end
3038

39+
def start_code_flow(redirect_uri='urn:ietf:wg:oauth:2.0:oob')
40+
oauth_client.auth_code.authorize_url(redirect_uri: redirect_uri)
41+
end
42+
43+
def complete_code_flow(code_value, redirect_uri='urn:ietf:wg:oauth:2.0:oob')
44+
@connection = oauth_client.auth_code.get_token(code_value, redirect_uri: redirect_uri)
45+
end
46+
3147
private
3248

3349
def get_oauth_token
34-
oauth = OAuth2::Client.new(@client_id, @client_secret, site: @api_url)
3550
if @username && @password
36-
oauth.password.get_token(@username, @password, scope: @scope)
51+
oauth_client.password.get_token(@username, @password, scope: @scope)
3752
else
38-
oauth.client_credentials.get_token(scope: @scope)
53+
oauth_client.client_credentials.get_token(scope: @scope)
3954
end
4055
end
56+
57+
def oauth_client
58+
@oauth_client ||= OAuth2::Client.new(@client_id, @client_secret, site: @api_url)
59+
end
4160
end

0 commit comments

Comments
 (0)