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
|
# File 'lib/whois_rb/http.rb', line 17
def get(domain, timeout, url_suffix='check')
options = {
url: "#{PROMPTAPI_ENDPOINT}/#{url_suffix}",
params: {domain: domain},
request: {timeout: timeout},
headers: {
'Accept' => 'application/json',
'apikey' => ENV['PROMPTAPI_TOKEN'] || nil,
},
}
unless options[:headers]['apikey']
return {error: "You need to set PROMPTAPI_TOKEN environment variable"}
end
conn = Faraday.new(options) do |c|
c.use Faraday::Response::RaiseError
end
begin
response = conn.get
return self.parse(response.body)
rescue Faraday::ConnectionFailed
return {error: "Connection error"}
rescue Faraday::TimeoutError => e
return {error: e.message.capitalize}
rescue Faraday::ClientError => e
return {error: parse(e.response[:body])[:message].capitalize}
rescue Faraday::ServerError => e
return {error: e.message.capitalize}
end
end
|