Module: DaphneUtil::Tools
- Defined in:
- lib/daphne_util/tools.rb
Defined Under Namespace
Classes: Store
Instance Method Summary collapse
- #api_domain ⇒ Object
- #app(k, v = nil) ⇒ Object
- #apps ⇒ Object
- #create(resource, *args) ⇒ Object
- #create_api_resource(resource_type, payload) ⇒ Object
- #create_app(name, *args) ⇒ Object
- #create_env(name, url) ⇒ Object
- #create_token(*args) ⇒ Object
- #delete(resource, *args) ⇒ Object
- #delete_api_resource(resource_type, identifier) ⇒ Object
- #delete_app(name) ⇒ Object
- #delete_env(name) ⇒ Object
- #env ⇒ Object
- #execute(*argv) ⇒ Object
- #oauth_domain ⇒ Object
- #output(str) ⇒ Object
- #resource_mapping(key) ⇒ Object
- #retrieve(resource, *args) ⇒ Object
- #retrieve_api_resource(resource_type, identifier) ⇒ Object
- #store ⇒ Object
- #to_payload(args) ⇒ Object
- #token ⇒ Object
- #update(resource, *args) ⇒ Object
- #update_api_resource(resource_type, identifier, payload) ⇒ Object
- #usage ⇒ Object
- #verify_token(val) ⇒ Object
Instance Method Details
#api_domain ⇒ Object
214 215 216 |
# File 'lib/daphne_util/tools.rb', line 214 def api_domain "#{store.url}/api/v#{store.version}" end |
#app(k, v = nil) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 |
# 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 ⇒ Object
175 176 177 178 |
# 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) ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# 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) ⇒ Object
238 239 240 241 |
# 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) ⇒ Object
301 302 303 304 |
# 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) ⇒ Object
291 292 293 294 |
# 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) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# 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) ⇒ Object
370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# 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) ⇒ Object
248 249 250 251 |
# 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) ⇒ Object
306 307 308 309 |
# File 'lib/daphne_util/tools.rb', line 306 def delete_app(name) raise IncorrectUsage unless name store.add_app(name) end |
#delete_env(name) ⇒ Object
296 297 298 299 |
# File 'lib/daphne_util/tools.rb', line 296 def delete_env(name) raise IncorrectUsage unless name store.rm_env(name) end |
#env ⇒ Object
155 156 157 158 159 160 161 |
# 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) ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# 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 ⇒ Object
218 219 220 |
# File 'lib/daphne_util/tools.rb', line 218 def oauth_domain "#{store.url}/oauth2/v#{store.version}" end |
#output(str) ⇒ Object
227 228 229 |
# File 'lib/daphne_util/tools.rb', line 227 def output(str) puts JSON.pretty_generate(JSON.parse(str)) end |
#resource_mapping(key) ⇒ Object
231 232 233 234 235 236 |
# 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) ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 396 |
# 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) ⇒ Object
253 254 255 256 |
# 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 ⇒ Object
151 152 153 |
# File 'lib/daphne_util/tools.rb', line 151 def store @store ||= Store.new end |
#to_payload(args) ⇒ Object
222 223 224 225 |
# File 'lib/daphne_util/tools.rb', line 222 def to_payload(args) r = CGI.parse(args.join('&')) Hash[*r.to_a.flatten] end |
#token ⇒ Object
180 181 182 |
# File 'lib/daphne_util/tools.rb', line 180 def token store.token end |
#update(resource, *args) ⇒ Object
355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# 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) ⇒ Object
243 244 245 246 |
# 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 ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# 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) ⇒ Object
286 287 288 289 |
# 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 |