Skip to content

Commit e8e773a

Browse files
committed
chore: Refactor Caido::Instance class initialization
1 parent a8347f5 commit e8e773a

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

lib/caido/instance.rb

+16-26
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,44 @@
44
require 'json'
55

66
# 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 }
88

99
module Caido
1010
# Instance class
1111
class Instance
1212
attr_reader :graphql_url, :authorization
1313

1414
def initialize(*args)
15+
set_defaults
16+
process_arguments(args)
17+
auth_from_env
18+
end
19+
20+
private
21+
22+
def set_defaults
1523
@graphql_url = 'http://localhost:8080/graphql'
1624
@authorization = nil
25+
end
1726

27+
def process_arguments(args)
1828
case args.size
19-
when 0
20-
# Initialize with default values
2129
when 1
22-
# Initialize with graphql_url
2330
@graphql_url = args[0]
2431
when 2
25-
# Initialize with graphql_url and authorization
2632
@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
3335
raise ArgumentError, 'Too many arguments provided'
3436
end
37+
end
3538

36-
auth_from_env if @authorization.nil?
39+
def format_authorization(auth)
40+
auth.include?('Bearer ') ? auth : "Bearer #{auth}"
3741
end
3842

3943
def auth_from_env
4044
@auth_from_env ||= ENV.fetch('CAIDO_AUTH_TOKEN', 'Bearer ')
4145
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
5646
end
5747
end

0 commit comments

Comments
 (0)