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

Commit 1f7d19d

Browse files
author
Andrej Jančič
committedJul 28, 2017
auth utils
1 parent f09764a commit 1f7d19d

File tree

6 files changed

+62
-54
lines changed

6 files changed

+62
-54
lines changed
 

‎bin/ctb_list_creations

+3-18
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ require 'optparse'
66
require 'creatubbles'
77
require 'pp'
88

9-
auth_details = {
10-
client_id: ENV['CREATUBBLES_CLIENT_ID'],
11-
client_secret: ENV['CREATUBBLES_CLIENT_SECRET'],
12-
api_url: ENV['CREATUBBLES_API_URL'] || Creatubbles::DEFAULT_API_URL,
13-
username: ENV['CREATUBBLES_USERNAME'],
14-
password: ENV['CREATUBBLES_PASSWORD']
15-
}
9+
auth_details = Creatubbles::Utils::AuthDetails.from_env
1610

1711
list_for = nil
1812
list_for_id = nil
@@ -21,20 +15,11 @@ opt_parser = OptionParser.new do |opt|
2115
opt.banner = "Usage: ctb_list_creators [OPTIONS]"
2216
opt.separator ""
2317
opt.separator "Environment"
24-
opt.separator " CREATUBBLES_CLIENT_ID: application client ID"
25-
opt.separator " CREATUBBLES_CLIENT_SECRET: application client secret"
26-
opt.separator " CREATUBBLES_USERNAME: user's email"
27-
opt.separator " CREATUBBLES_PASSWORD: user's password"
28-
opt.separator " CREATUBBLES_API_URL: API URL (default: #{Creatubbles::DEFAULT_API_URL})"
18+
Creatubbles::Utils::AuthDetails.option_parser_env_explain(opt)
2919
opt.separator ""
3020
opt.separator "Options"
3121

32-
opt.on("-u","--username EMAIL","user's email") do |email|
33-
auth_details[:username] = email
34-
end
35-
opt.on("-p","--password password","user's password") do |password|
36-
auth_details[:password] = password
37-
end
22+
Creatubbles::Utils::AuthDetails.option_parser_handle(opt,auth_details)
3823

3924
opt.on("--user id","list creations for user id (use 'me' for logged in user)") do |user_id|
4025
list_for = :users

‎bin/ctb_list_creators

+3-18
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,17 @@ require 'optparse'
66
require 'creatubbles'
77
require 'pp'
88

9-
auth_details = {
10-
client_id: ENV['CREATUBBLES_CLIENT_ID'],
11-
client_secret: ENV['CREATUBBLES_CLIENT_SECRET'],
12-
api_url: ENV['CREATUBBLES_API_URL'] || Creatubbles::DEFAULT_API_URL,
13-
username: ENV['CREATUBBLES_USERNAME'],
14-
password: ENV['CREATUBBLES_PASSWORD']
15-
}
9+
auth_details = Creatubbles::Utils::AuthDetails.from_env
1610

1711
opt_parser = OptionParser.new do |opt|
1812
opt.banner = "Usage: ctb_list_creators [OPTIONS]"
1913
opt.separator ""
2014
opt.separator "Environment"
21-
opt.separator " CREATUBBLES_CLIENT_ID: application client ID"
22-
opt.separator " CREATUBBLES_CLIENT_SECRET: application client secret"
23-
opt.separator " CREATUBBLES_USERNAME: user's email"
24-
opt.separator " CREATUBBLES_PASSWORD: user's password"
25-
opt.separator " CREATUBBLES_API_URL: API URL (default: #{Creatubbles::DEFAULT_API_URL})"
15+
Creatubbles::Utils::AuthDetails.option_parser_env_explain(opt)
2616
opt.separator ""
2717
opt.separator "Options"
2818

29-
opt.on("-u","--username EMAIL","user's email") do |email|
30-
auth_details[:username] = email
31-
end
32-
opt.on("-p","--password password","user's password") do |password|
33-
auth_details[:password] = password
34-
end
19+
Creatubbles::Utils::AuthDetails.option_parser_handle(opt,auth_details)
3520

3621
opt.on("-h","--help","help") do
3722
puts opt_parser

‎bin/ctb_uploader

+3-18
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
55
require 'optparse'
66
require 'creatubbles'
77

8-
auth_details = {
9-
client_id: ENV['CREATUBBLES_CLIENT_ID'],
10-
client_secret: ENV['CREATUBBLES_CLIENT_SECRET'],
11-
api_url: ENV['CREATUBBLES_API_URL'] || Creatubbles::DEFAULT_API_URL,
12-
username: ENV['CREATUBBLES_USERNAME'],
13-
password: ENV['CREATUBBLES_PASSWORD']
14-
}
8+
auth_details = Creatubbles::Utils::AuthDetails.from_env
159

1610
creation_details = {}
1711
galleries = []
@@ -23,20 +17,11 @@ opt_parser = OptionParser.new do |opt|
2317
opt.separator " path to file you'd like to upload; has to be a supported file type"
2418
opt.separator ""
2519
opt.separator "Environment"
26-
opt.separator " CREATUBBLES_CLIENT_ID: application client ID"
27-
opt.separator " CREATUBBLES_CLIENT_SECRET: application client secret"
28-
opt.separator " CREATUBBLES_USERNAME: user's email"
29-
opt.separator " CREATUBBLES_PASSWORD: user's password"
30-
opt.separator " CREATUBBLES_API_URL: API URL (default: #{Creatubbles::DEFAULT_API_URL})"
20+
Creatubbles::Utils::AuthDetails.option_parser_env_explain(opt)
3121
opt.separator ""
3222
opt.separator "Options"
3323

34-
opt.on("-u","--username EMAIL","user's email") do |email|
35-
auth_details[:username] = email
36-
end
37-
opt.on("-p","--password PASSWORD","user's password") do |password|
38-
auth_details[:password] = password
39-
end
24+
Creatubbles::Utils::AuthDetails.option_parser_handle(opt,auth_details)
4025

4126
opt.on("-n","--name NAME","creation name") do |name|
4227
creation_details[:name] = name

‎lib/creatubbles.rb

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def self.instantiate_objects_from_response(response, connection)
4242

4343
end
4444

45+
require "creatubbles/utils/auth_details"
4546
require "creatubbles/content"
4647
require "creatubbles/contents"
4748
require "creatubbles/creation"

‎lib/creatubbles/client.rb

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
class Creatubbles::Client
44
attr_writer :api_url, :username, :password
55

6+
def self.new_from_env
7+
auth_details = Creatubbles::Utils::AuthDetails.from_env
8+
missing = auth_details.select{|_k,v| ['',nil].include?(v) }
9+
if missing.empty?
10+
new(auth_details)
11+
else
12+
raise "Can't init Creatubbles::Client ! Missing: #{missing.keys.join(',')}"
13+
end
14+
end
15+
616
def initialize(client_id: nil, client_secret: nil, username: nil, password: nil, api_url: Creatubbles::DEFAULT_API_URL, scope: nil)
717
@client_id = client_id
818
@client_secret = client_secret

‎lib/creatubbles/utils/auth_details.rb

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Creatubbles::Utils::AuthDetails
2+
module Creatubbles
3+
module Utils
4+
class AuthDetails
5+
def self.from_env
6+
{
7+
client_id: ENV['CREATUBBLES_CLIENT_ID'],
8+
client_secret: ENV['CREATUBBLES_CLIENT_SECRET'],
9+
api_url: ENV['CREATUBBLES_API_URL'] || Creatubbles::DEFAULT_API_URL,
10+
username: ENV['CREATUBBLES_USERNAME'],
11+
password: ENV['CREATUBBLES_PASSWORD']
12+
}
13+
end
14+
15+
def self.option_parser_env_explain opt
16+
opt.separator " CREATUBBLES_CLIENT_ID: application client ID"
17+
opt.separator " CREATUBBLES_CLIENT_SECRET: application client secret"
18+
opt.separator " CREATUBBLES_USERNAME: user's email"
19+
opt.separator " CREATUBBLES_PASSWORD: user's password"
20+
opt.separator " CREATUBBLES_API_URL: API URL (default: #{Creatubbles::DEFAULT_API_URL})"
21+
end
22+
23+
def self.option_parser_handle(opt,auth_details)
24+
opt.on("-c","--cid CLIENT_ID","application client ID") do |cid|
25+
auth_details[:client_id] = cid
26+
end
27+
opt.on("-s","--cis CLIENT_SECRET","application secret") do |cis|
28+
auth_details[:client_secret] = cis
29+
end
30+
opt.on("-u","--username EMAIL","User's email") do |email|
31+
auth_details[:username] = email
32+
end
33+
opt.on("-p","--password PASSWORD","user's password") do |password|
34+
auth_details[:password] = password
35+
end
36+
opt.on("-a","--api API_URL","API URL (default: #{Creatubbles::DEFAULT_API_URL})") do |api_url|
37+
auth_details[:api_url] = api_url
38+
end
39+
end
40+
end
41+
end
42+
end

0 commit comments

Comments
 (0)
This repository has been archived.