Module: Ip::Lookup

Defined in:
lib/ip/lookup.rb,
lib/ip/lookup/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.cli(ip: server_whatismyipaddress, table: true) ⇒ Object



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
# File 'lib/ip/lookup.rb', line 28

def cli ip: server_whatismyipaddress, table: true
  require "terminal-table"
  require "awesome_print"

  if ip.nil?
    raise "Params is require!"
  else
    api = JSON.parse( Net::HTTP.get( URI "http://ip-api.com/json/#{ip}" ) )
    if table == true
      if api["status"] == "success"
        array = [
          ["Network", api["as"]],
          ["City", api["city"]],
          ["Country", api["country"]],
          ["CountryCode", api["countryCode"]],
          ["Isp", api["isp"]],
          ["Lat", api["lat"]],
          ["Lon", api["lon"]],
          ["Org", api["org"]],
          ["IP Address", api["query"]],
          ["Region", api["region"]],
          ["RegionName", api["regionName"]],
          ["Timezone", api["timezone"]],
          ["Zip", api["zip"]],
        ]
      else
        array = [
          ["Message", api["message"]],
          ["Request", api["query"]],
        ]
      end
      data = Terminal::Table.new :headings => ["String", "Value"], :rows => array
      puts data
    else
      data = ''
      if api["status"] == "success"
        data = api
      else
        raise "Invalid query!"
      end
      data
    end
  end
rescue => exception
  exception
end

.client_whatismyipaddressObject



18
19
20
21
22
23
24
25
26
# File 'lib/ip/lookup.rb', line 18

def client_whatismyipaddress
  if request.remote_ip.nil? || request.remote_ip == '127.0.0.1'
    open('http://bot.whatismyipaddress.com').read
  else
    request.remote_ip
  end
rescue => exception
  exception
end

.get(option = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ip/lookup.rb', line 75

def get option = nil
  api = JSON.parse( Net::HTTP.get( URI "http://ip-api.com/json/#{server_whatismyipaddress}" ) )
  case option
    when :as, 'as'
      api['as']
    when :city, 'city'
      api['city']
    when :country, 'country'
      api['country']
    when :countryCode, 'countryCode'
      api['countryCode']
    when :isp, 'isp'
      api['isp']
    when :lat, 'lat'
      api['lat']
    when :lon, 'lon'
      api['lon']
    when :org, 'org'
      api['org']
    when :query, 'query'
      api['query']
    when :region, 'region'
      api['region']
    when :regionName, 'regionName'
      api['regionName']
    when :timezone, 'timezone'
      api['timezone']
    when :zip, 'zip'
      api['zip']
    else
      raise "Invalid parameter!"
  end
rescue => exception
  exception
end

.server_whatismyipaddressObject



12
13
14
15
16
# File 'lib/ip/lookup.rb', line 12

def server_whatismyipaddress
  Net::HTTP.get( URI 'http://bot.whatismyipaddress.com' )
rescue => exception
  exception
end