Class: Robtex::API

Inherits:
Object
  • Object
show all
Defined in:
lib/robtex/api.rb

Constant Summary collapse

HOST =
"freeapi.robtex.com".freeze
URL =
"https://#{HOST}".freeze

Instance Method Summary collapse

Instance Method Details

#as(q) ⇒ Object



9
10
11
# File 'lib/robtex/api.rb', line 9

def as(q)
  get("/asquery/#{q}") { |body| JSON.parse body }
end

#fpdns(q) ⇒ Object



23
24
25
26
27
# File 'lib/robtex/api.rb', line 23

def fpdns(q)
  get("/pdns/forward/#{q}") do |body|
    body.lines.map { |line| JSON.parse line }
  end
end

#get(path, &block) ⇒ Object



64
65
66
67
# File 'lib/robtex/api.rb', line 64

def get(path, &block)
  get = Net::HTTP::Get.new(url_for(path))
  request(get, &block)
end

#https_optionsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/robtex/api.rb', line 33

def https_options
  if proxy = ENV["HTTPS_PROXY"] || ENV["https_proxy"]
    uri = URI(proxy)
    {
      proxy_address:  uri.hostname,
      proxy_port:     uri.port,
      proxy_from_env: false,
      use_ssl: true
    }
  else
    { use_ssl: true }
  end
end

#ip(q) ⇒ Object



13
14
15
# File 'lib/robtex/api.rb', line 13

def ip(q)
  get("/ipquery/#{q}") { |body| JSON.parse body }
end

#request(req) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/robtex/api.rb', line 47

def request(req)
  Net::HTTP.start(HOST, 443, https_options) do |http|
    response = http.request(req)

    case response.code
    when '200' then yield response.body
    when '400' then raise ProcessingError, response.body
    when '401' then raise AuthenticationError, response.body
    when '404' then raise NotFound, response.body
    when '429' then raise RateLimited, response.body
    when '500' then raise InternalServerError, response.body
    else
      raise ResponseError, response.body
    end
  end
end

#rpdns(q) ⇒ Object



17
18
19
20
21
# File 'lib/robtex/api.rb', line 17

def rpdns(q)
  get("/pdns/reverse/#{q}") do |body|
    body.lines.map { |line| JSON.parse line }
  end
end

#url_for(path) ⇒ Object



29
30
31
# File 'lib/robtex/api.rb', line 29

def url_for(path)
  URI(URL + path)
end