module DaphneUtil::Tools
Public Instance Methods
api_domain()
click to toggle source
# File lib/daphne_util/tools.rb, line 214 def api_domain "#{store.url}/api/v#{store.version}" end
app(k, v = nil)
click to toggle source
# File lib/daphne_util/tools.rb, line 163 def app(k, v = nil) k = k.to_s.to_sym v ||= store.apps[k] seed = [k] + v %{ #{k} : #{v.join(',')} - uid : #{DaphneUtil.generate_id(*seed)} - secret : #{DaphneUtil.generate_secret(*seed)} - api_key : #{DaphneUtil.generate_api_key(*seed)} } end
apps()
click to toggle source
# File lib/daphne_util/tools.rb, line 175 def apps return 'None' if store.apps.empty? store.apps.collect {|k,v| app(k,v) }.join("\n") end
create(resource, *args)
click to toggle source
# File lib/daphne_util/tools.rb, line 337 def create(resource, *args) case resource when :resource_owner, :application, :app rtn = create_api_resource(resource, to_payload(args)) output(rtn) when :token rtn = create_token(*args) output(rtn) when :env rtn = create_env(args[0], args[1]) puts env when :creds create_app(args[0], *args[1..-1]) puts env else raise IncorrectUsage end end
create_api_resource(resource_type, payload)
click to toggle source
# File lib/daphne_util/tools.rb, line 238 def create_api_resource(resource_type, payload) raise IncorrectUsage unless resource_type && payload RestClient.post("#{api_domain}/#{resource_mapping(resource_type)}", {resource_type => payload}, authorization: "Bearer #{token}") end
create_app(name, *args)
click to toggle source
# File lib/daphne_util/tools.rb, line 301 def create_app(name, *args) raise IncorrectUsage unless name store.add_app(name, *args) end
create_env(name, url)
click to toggle source
# File lib/daphne_util/tools.rb, line 291 def create_env(name, url) raise IncorrectUsage unless url && name store.add_env(name, url) end
create_token(*args)
click to toggle source
# File lib/daphne_util/tools.rb, line 258 def create_token(*args) raise IncorrectUsage if args.empty? params = {grant_type: "client_credentials"} case args.length when 1 if store.app_keys.include?(args.first.to_sym) seed = [args.first] + store.apps[args.first.to_sym] params[:client_id] = DaphneUtil.generate_id(*seed) params[:client_secret] = DaphneUtil.generate_secret(*seed) else raise IncorrectUsage end when 2 params[:client_id] = args[0] params[:client_secret] = args[1] when 3 raise IncorrectUsage else params[:grant_type] = "password" params[:client_id] = args[0] params[:client_secret] = args[1] params[:email] = args[2] params[:password] = args[3] end RestClient.post("#{oauth_domain}/token", params) end
delete(resource, *args)
click to toggle source
# File lib/daphne_util/tools.rb, line 370 def delete(resource, *args) case resource when :resource_owner, :application, :app rtn = delete_api_resource(resource, args.first) output(rtn) when :env store.rm_env(args.first) puts env when :creds store.rm_app(args.first) puts env else raise IncorrectUsage end end
delete_api_resource(resource_type, identifier)
click to toggle source
# File lib/daphne_util/tools.rb, line 248 def delete_api_resource(resource_type, identifier) raise IncorrectUsage unless identifier RestClient.delete("#{api_domain}/#{resource_mapping(resource_type)}/#{identifier}", authorization: "Bearer #{token}") end
delete_app(name)
click to toggle source
# File lib/daphne_util/tools.rb, line 306 def delete_app(name) raise IncorrectUsage unless name store.add_app(name) end
delete_env(name)
click to toggle source
# File lib/daphne_util/tools.rb, line 296 def delete_env(name) raise IncorrectUsage unless name store.rm_env(name) end
env()
click to toggle source
# File lib/daphne_util/tools.rb, line 155 def env %{ Environment #{store.env} : - url : #{store.url} - version : #{store.version} - token : #{store.token || 'No token set'} Credentials : #{apps}} end
execute(*argv)
click to toggle source
# File lib/daphne_util/tools.rb, line 311 def execute(*argv) raise IncorrectUsage if argv.empty? action, resource, args = argv[0], argv[1].to_s.to_sym, argv[2..-1].to_a case action when "create" then create(resource, *args) when "update" then update(resource, *args) when "delete" then delete(resource, *args) when "get" then retrieve(resource, *args) when "verify" then verify_token(args[1]) when "env" then retrieve(:env) when "help" then puts usage when "token" argv[1] ? update(:token, argv[1]) : retrieve(:token) when "url" argv[1] ? update(:url, argv[1]) : retrieve(:url) else raise IncorrectUsage end rescue IncorrectUsage puts usage rescue RestClient::Exception => e puts "Error :" e.is_a?(RestClient::InternalServerError) ? (raise e) : output(e.response) end
oauth_domain()
click to toggle source
# File lib/daphne_util/tools.rb, line 218 def oauth_domain "#{store.url}/oauth2/v#{store.version}" end
output(str)
click to toggle source
# File lib/daphne_util/tools.rb, line 227 def output(str) puts JSON.pretty_generate(JSON.parse(str)) end
resource_mapping(key)
click to toggle source
# File lib/daphne_util/tools.rb, line 231 def resource_mapping(key) { resource_owner: "resource_owners", application: "applications" }[key.to_s.to_sym] end
retrieve(resource, *args)
click to toggle source
# File lib/daphne_util/tools.rb, line 385 def retrieve(resource, *args) case resource when :resource_owner, :application rtn = retrieve_api_resource(resource, args.first) output(rtn) when :token puts(store.token ? store.token : "Token not configured for this environment.") when :env puts env else raise IncorrectUsage end end
retrieve_api_resource(resource_type, identifier)
click to toggle source
# File lib/daphne_util/tools.rb, line 253 def retrieve_api_resource(resource_type, identifier) raise IncorrectUsage unless identifier RestClient.get("#{api_domain}/#{resource_mapping(resource_type)}/#{identifier}", authorization: "Bearer #{token}") end
store()
click to toggle source
# File lib/daphne_util/tools.rb, line 151 def store @store ||= Store.new end
to_payload(args)
click to toggle source
# File lib/daphne_util/tools.rb, line 222 def to_payload(args) r = CGI.parse(args.join('&')) Hash[*r.to_a.flatten] end
token()
click to toggle source
# File lib/daphne_util/tools.rb, line 180 def token store.token end
update(resource, *args)
click to toggle source
# File lib/daphne_util/tools.rb, line 355 def update(resource, *args) case resource when :resource_owner, :application, :app rtn = update_api_resource(resource, args[0], to_payload(args[1..-1])) output(rtn) when :token store.token = args.first puts env when :url store.url = args.first puts env else raise IncorrectUsage end end
update_api_resource(resource_type, identifier, payload)
click to toggle source
# File lib/daphne_util/tools.rb, line 243 def update_api_resource(resource_type, identifier, payload) raise IncorrectUsage unless payload && identifier RestClient.put("#{api_domain}/#{resource_mapping(resource_type)}/#{identifier}", {resource_type => payload}, authorization: "Bearer #{token}") end
usage()
click to toggle source
# File lib/daphne_util/tools.rb, line 184 def usage %{Environments create env <name> <url> delete env <name> get env env Application Credentials create creds <name> [arg1, [arg2, [arg3] ...]] Token token [string] verify <string> create token <creds name> create token <app uid> <app secret> create token <creds name> <email> <password> create token <app uid> <app secret> <email> <password> Url url [string] Application create app <attr1>=<value1> <attr2>=<value2> <attr3>=<value3> update app <uid or composite id> <attr1>=<value1> <attr2>=<value2> <attr3>=<value3> delete app <uid or composite id> get app <uid or composite id> Resource Owner create resource_owmer <attr1>=<value1> <attr2>=<value2> <attr3>=<value3> update resource_owmer <uid or composite id or email> <attr1>=<value1> <attr2>=<value2> <attr3>=<value3> delete resource_owmer <uid or composite id or email> get resource_owmer <uid or composite id or email> } end
verify_token(val)
click to toggle source
# File lib/daphne_util/tools.rb, line 286 def verify_token(val) raise IncorrectUsage if val.to_s.empty? RestClient.post("#{oauth_domain}/verify?access_token=#{val}") end