Class: Object

Inherits:
BasicObject
Defined in:
lib/where_are_you.rb

Instance Method Summary collapse

Instance Method Details

#where_are_you(ip, format = 'city', global = false) ⇒ Object



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
# File 'lib/where_are_you.rb', line 3

def where_are_you(ip, format = 'city', global = false)

  require 'net/http'
  require 'json'
  require 'yaml'

  uri = URI.parse("http://ip-api.com/json/#{ip}")
  response = Net::HTTP.get_response(uri)
  result = JSON(response.body)
  data = YAML::load_file(File.dirname(__FILE__) + '/data.yml')

  if result['status'] != 'success'

    return nil

  elsif format == 'city'

    if global || result['country'] == 'United States'
      return result['city'] + ', ' + result['region']
    else
      return nil
    end

  elsif format == 'state'

    if global || result['country'] == 'United States'
      return result['regionName']
    else
      return nil
    end

  elsif format == 'country'

    return result['country']

  elsif format == 'language'

    formatted_country = result['country'].downcase.gsub(' ','_').gsub(',','_')
    return data[formatted_country]

  end
end