geoip_rails 0.0.2 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6e3b4e789c007000b5362101181f33ac700281d4
4
- data.tar.gz: 339c7adfa4ccbfc62ded5acd6a0c2f595a0b3edc
3
+ metadata.gz: cff04182d9e20c4a548563394ca80eee8abf4ff8
4
+ data.tar.gz: 481f7b4d08690ab137d904c75a72921774c48719
5
5
  SHA512:
6
- metadata.gz: 99d9bd5bdc49318964e34d989f135c9c35ea71c2fea6155c7962d2ebdf40a3e157e1603c01dff986e6de08f666511a5247094f4a8ef558799b2f1ae0f3ab4354
7
- data.tar.gz: 109a169dabc1cfa8fffa77d7a24c9b51db229ca7069d53cd2403aeff287ed1889334e83b4e1fce3994365097fe45ab2c585c4eb043c4685f6d656aa93e26cdbf
6
+ metadata.gz: 1eff5a9c55320faf31be6d9e67e45abc1190ab6f988a1de783fd8613debdf41a5558226e194ed313945ec8dfec47a350a9fca10466233299c4a5037e9ae1fa7c
7
+ data.tar.gz: c2738af21562553b4e5dcdb99ac4389d0ad8a4d95b7804d8b0385d796fbe3cd1e93ff7980e5c37fb00cfa074abfd3c890286b590097360929bebed76e664cdd8
data/README.md CHANGED
@@ -1,7 +1,8 @@
1
+ [![Gem Version](https://badge.fury.io/rb/geoip_rails.svg)](http://badge.fury.io/rb/geoip_rails)
1
2
  # GeoipRails
2
3
 
3
4
  Gives geolocation details based on an IP address or a host name
4
- The gem uses freegeoip's API to fetch geolocation data given an IP address. freegeoip.net is a public REST API for searching geolocation of IP addresses and host names. Thanks to http://freegeoip.net/ for providing such a great API
5
+ The gem uses freegeoip's API to fetch geolocation data given an IP address. freegeoip.net is a public REST API for searching geolocation of IP addresses and host names. Supported formats include json (default), xml and csv. Thanks to http://freegeoip.net/ for providing such a great API.
5
6
  ## Installation
6
7
 
7
8
  Add this line to your application's Gemfile:
@@ -20,28 +21,21 @@ Or install it yourself as:
20
21
 
21
22
  You can pass the IP address/hostname for which you want to check geolocation data to:
22
23
 
23
- GeoipRails.find_by_ip("208.95.216.41")
24
- GeoipRails.find_by_ip("google.com")
25
-
26
- These return the hashes below:
27
-
28
- {"ip"=>"208.95.216.41", "country_code"=>"US", "country_name"=>"United States",
24
+ GeoipRails.geolocate("208.95.216.41")
25
+ => {"ip"=>"208.95.216.41", "country_code"=>"US", "country_name"=>"United States",
29
26
  "region_code"=>"VT", "region_name"=>"Vermont", "city"=>"Colchester", "zipcode"=>"05446",
30
27
  "latitude"=>44.55, "longitude"=>-73.1552, "metro_code"=>"523", "area_code"=>"802"}
31
-
32
- And
33
28
 
34
- {"ip"=>"173.194.115.72", "country_code"=>"US", "country_name"=>"United States",
29
+ GeoipRails.geolocate("google.com")
30
+ => {"ip"=>"173.194.115.72", "country_code"=>"US", "country_name"=>"United States",
35
31
  "region_code"=>"CA", "region_name"=>"California", "city"=>"Mountain View", "zipcode"=>"94043",
36
32
  "latitude"=>37.4192, "longitude"=>-122.0574, "metro_code"=>"807", "area_code"=>"650"}
37
33
 
38
- for the second case.
39
-
40
- You can of course select your desired value from this hash i.e.
34
+ You can of course select your desired value from the hash i.e.
41
35
 
42
- GeoipRails.find_by_ip("208.95.216.41")["country_name"]
36
+ GeoipRails.geolocate("208.95.216.41")["country_name"]
43
37
  => "United States"
44
- GeoipRails.find_by_ip("208.95.216.41")["country_code"]
38
+ GeoipRails.geolocate("208.95.216.41")["country_code"]
45
39
  => "US"
46
40
 
47
41
  And so on. But we have shorthand methods for all this as shown below:
@@ -64,6 +58,14 @@ And so on. But we have shorthand methods for all this as shown below:
64
58
  => 44.55
65
59
  GeoipRails.longitude("208.95.216.41")
66
60
  => -73.1552
61
+
62
+ If, for some reason, you want the response in xml or csv, you can use the same method but, pass in an extra argument for the format, as shown below:
63
+
64
+ GeoipRails.geolocate("208.95.216.41", "xml")
65
+
66
+ GeoipRails.geolocate("208.95.216.41", "csv")
67
+
68
+ GeoipRails.geolocate("208.95.216.41", "json") # Json is the default. You don't need to specify it.
67
69
 
68
70
  ## Pros and Cons
69
71
 
@@ -3,9 +3,34 @@ require "httparty"
3
3
 
4
4
  module GeoipRails
5
5
  module_function
6
+
6
7
  def find_by_ip ip
7
- format = "json"
8
- HTTParty.get("http://freegeoip.net/#{format}/#{ip}")
8
+ HTTParty.get("http://freegeoip.net/json/#{ip}")
9
+ end
10
+
11
+ def find_by_ip_and_format ip, format
12
+ ip = ip.downcase
13
+ format = format.downcase
14
+ `wget "http://freegeoip.net/#{format}/#{ip}"`
15
+ response = ""
16
+ File.open("#{ip}").each { |line| response += line}#.gsub("\n", " ")
17
+ `rm -rf #{ip}`
18
+ return response
19
+ end
20
+
21
+ # def print ip, format
22
+ # puts find_by_ip_and_format ip, format
23
+ # end
24
+
25
+ def geolocate(*args)
26
+ supported_formats = ["json", "xml", "csv"]
27
+ if args.length == 1
28
+ find_by_ip(args[0])
29
+ else
30
+ format = args[1].strip.downcase
31
+ format = "json" if !supported_formats.include?(format)
32
+ find_by_ip_and_format(args[0], format)
33
+ end
9
34
  end
10
35
 
11
36
  # def geolocation
@@ -16,30 +41,39 @@ module GeoipRails
16
41
  def country ip
17
42
  find_by_ip(ip)["country_name"]
18
43
  end
44
+
19
45
  def country_code ip
20
46
  find_by_ip(ip)["country_code"]
21
47
  end
48
+
22
49
  def city ip
23
50
  find_by_ip(ip)["city"]
24
51
  end
52
+
25
53
  def region_code ip
26
54
  find_by_ip(ip)["region_code"]
27
55
  end
56
+
28
57
  def region_name ip
29
58
  find_by_ip(ip)["region_name"]
30
59
  end
60
+
31
61
  def zipcode ip
32
62
  find_by_ip(ip)["zipcode"]
33
63
  end
64
+
34
65
  def latitude ip
35
66
  find_by_ip(ip)["latitude"]
36
67
  end
68
+
37
69
  def longitude ip
38
70
  find_by_ip(ip)["longitude"]
39
71
  end
72
+
40
73
  def metro_code ip
41
74
  find_by_ip(ip)["metro_code"]
42
75
  end
76
+
43
77
  def area_code ip
44
78
  find_by_ip(ip)["area_code"]
45
79
  end
@@ -1,3 +1,3 @@
1
1
  module GeoipRails
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geoip_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - muaad
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-19 00:00:00.000000000 Z
11
+ date: 2014-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler