|
4 | 4 | require 'json'
|
5 | 5 |
|
6 | 6 | # Assuming this code is at the top level of instance.rb and helplers directory is at the same level
|
7 |
| -Dir[File.expand_path('helpers/*.rb', __dir__)].sort.each { |file| require file } |
| 7 | +Dir[File.expand_path('helpers/*.rb', __dir__)].each { |file| require file } |
8 | 8 |
|
9 | 9 | module Caido
|
10 | 10 | # Instance class
|
11 | 11 | class Instance
|
12 | 12 | attr_reader :graphql_url, :authorization
|
13 | 13 |
|
14 | 14 | def initialize(*args)
|
| 15 | + set_defaults |
| 16 | + process_arguments(args) |
| 17 | + auth_from_env |
| 18 | + end |
| 19 | + |
| 20 | + private |
| 21 | + |
| 22 | + def set_defaults |
15 | 23 | @graphql_url = 'http://localhost:8080/graphql'
|
16 | 24 | @authorization = nil
|
| 25 | + end |
17 | 26 |
|
| 27 | + def process_arguments(args) |
18 | 28 | case args.size
|
19 |
| - when 0 |
20 |
| - # Initialize with default values |
21 | 29 | when 1
|
22 |
| - # Initialize with graphql_url |
23 | 30 | @graphql_url = args[0]
|
24 | 31 | when 2
|
25 |
| - # Initialize with graphql_url and authorization |
26 | 32 | @graphql_url, authorization = args
|
27 |
| - @authorization = if authorization.include?('Bearer ') |
28 |
| - authorization |
29 |
| - else |
30 |
| - "Bearer #{authorization}" |
31 |
| - end |
32 |
| - else |
| 33 | + @authorization = format_authorization(authorization) |
| 34 | + when args.size > 2 |
33 | 35 | raise ArgumentError, 'Too many arguments provided'
|
34 | 36 | end
|
| 37 | + end |
35 | 38 |
|
36 |
| - auth_from_env if @authorization.nil? |
| 39 | + def format_authorization(auth) |
| 40 | + auth.include?('Bearer ') ? auth : "Bearer #{auth}" |
37 | 41 | end
|
38 | 42 |
|
39 | 43 | def auth_from_env
|
40 | 44 | @auth_from_env ||= ENV.fetch('CAIDO_AUTH_TOKEN', 'Bearer ')
|
41 | 45 | end
|
42 |
| - |
43 |
| - def query(query) |
44 |
| - res = HTTParty.post( |
45 |
| - graphql_url, |
46 |
| - body: { query: query }.to_json, |
47 |
| - headers: { |
48 |
| - 'Content-Type' => 'application/json', |
49 |
| - 'Authorization' => authorization |
50 |
| - } |
51 |
| - ) |
52 |
| - |
53 |
| - obj = JSON.parse(res.body) |
54 |
| - obj['data'] |
55 |
| - end |
56 | 46 | end
|
57 | 47 | end
|
0 commit comments