-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.rb
93 lines (78 loc) · 2.31 KB
/
script.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
require 'net/http'
require 'uri'
require 'json'
def read_env_file(file_path)
env_vars = {}
File.foreach(file_path) do |line|
next if line.strip.empty? || line.start_with?('#')
key, value = line.split('=', 2)
env_vars[key.strip] = value.strip.gsub(/["']/, '')
end
env_vars
end
def get_jwt_token(env_vars)
uri = URI("#{env_vars['COMMON_LOGIN_URL']}/login")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'
auth_payload = {
email: env_vars['COMMON_LOGIN_EMAIL'],
password: env_vars['COMMON_LOGIN_PASSWORD']
}
request = Net::HTTP::Post.new(uri.request_uri)
request['Content-Type'] = 'application/json'
request['Accept'] = 'application/json'
request.body = auth_payload.to_json
begin
response = http.request(request)
if response.is_a?(Net::HTTPSuccess)
return JSON.parse(response.body)['token']
else
puts "Authentication failed. Status code: #{response.code}"
puts response.body
return nil
end
rescue StandardError => e
puts "An error occurred during authentication: #{e.message}"
return nil
end
end
def get_company_data(company_id, jwt_token)
uri = URI("https://api-sandbox.acubeapi.com/verify/company/#{company_id}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
request['Authorization'] = "Bearer #{jwt_token}"
request['Accept'] = 'application/json'
begin
response = http.request(request)
if response.is_a?(Net::HTTPSuccess)
return JSON.parse(response.body)
else
puts "Company verification failed. Status code: #{response.code}"
puts response.body
return nil
end
rescue StandardError => e
puts "An error occurred during company verification: #{e.message}"
return nil
end
end
# Main execution
env_vars = read_env_file('.env.local')
unless ARGV.size == 1
puts "Usage: ruby script.rb <company_id>"
exit 1
end
company_id = ARGV[0]
jwt_token = get_jwt_token(env_vars)
if jwt_token
puts "Authentication successful. Received JWT token."
company_data = get_company_data(company_id, jwt_token)
if company_data
puts "Company verification successful."
pretty_json = JSON.pretty_generate(company_data)
puts pretty_json
end
else
puts "Failed to obtain JWT token. Exiting..."
end