Module: WhoisRb::Http

Defined in:
lib/whois_rb/http.rb

Constant Summary collapse

PROMPTAPI_ENDPOINT =
'https://api.promptapi.com/whois'

Class Method Summary collapse

Class Method Details

.get(domain, timeout, url_suffix = 'check') ⇒ Object



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

.parse(body) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/whois_rb/http.rb', line 9

def parse(body)
  begin
    JSON.parse(body, symbolize_names: true)
  rescue JSON::ParserError
    {error: "JSON decoding error"}
  end
end