geolocater 0.0.1.alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Guardfile
4
+ Gemfile.lock
5
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in geolocater.gemspec
4
+ gemspec
@@ -0,0 +1,16 @@
1
+ Geolocater
2
+ ==========
3
+ Yet Another IP Geolocation Gem for Fun and Profit!
4
+ --------------------------------------------------
5
+
6
+ ### Installation
7
+ `gem install geolocater`
8
+
9
+ ### Usage
10
+ Pass any properly formatted IPv4 address string to a new instance of Geolocater like so.
11
+
12
+ `Geolocater.ip_lookup(IP_ADDRESS_STRING)`
13
+
14
+ ### More Info
15
+ * Email me at <dbarrett83@gmail.com>
16
+ * [Twitter](http://www.twitter.com/thoughtpunch)
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "geolocater/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "geolocater"
7
+ s.license = 'MIT'
8
+ s.version = Geolocater::VERSION
9
+ s.authors = ["Dan Barrett"]
10
+ s.email = ["dbarrett83@gmail.com"]
11
+ s.homepage = "http://www.about.me/thoughtpunch"
12
+ s.summary = "Yet Another IP Geolocation Gem"
13
+ s.description = "geolocater is a simple ruby wrapper for the freegeoip.net API"
14
+
15
+ s.rubyforge_project = "geolocater"
16
+
17
+ s.required_ruby_version = '>= 1.9.0'
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+
23
+ s.add_development_dependency "rspec"
24
+ s.add_development_dependency "guard"
25
+ s.add_dependency "faraday"
26
+ s.add_dependency "json"
27
+ end
@@ -0,0 +1,47 @@
1
+ require "geolocater/version"
2
+ require 'faraday'
3
+ require 'json'
4
+
5
+ class Geolocater
6
+
7
+ class << self
8
+
9
+ def ip_lookup(ip_address)
10
+ #IP Regex check
11
+ if /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/.match(ip_address).nil?
12
+ raise "Not a valid IPv4 address"
13
+ #Localhost Check
14
+ elsif ip_address == "127.0.0.1"
15
+ raise "Can't lookup localhost address. Please use an external IP address!"
16
+ #If valid, pass the ip address to the 'geolocate' method
17
+ else
18
+ geolocate_ip ip_address
19
+ end
20
+ end
21
+
22
+ private
23
+ def geolocate_ip ip_address
24
+ #the request URI
25
+ uri = "http://freegeoip.net/json/#{ip_address}"
26
+ #Get the response with Faraday and store to 'http_response' var
27
+ http_response = Faraday.get uri
28
+ #If the HTTP request is successful...
29
+ if http_response.success? == true && http_response.status == 200;
30
+ #JSON PARSE THE BODY
31
+ result = JSON.parse(http_response.body)
32
+ #If the response is missing basic info, like 'city'...
33
+ if result["city"].empty?
34
+ raise "Incomplete record. Please try another address"
35
+ #otherwise return the result
36
+ else
37
+ return result
38
+ end
39
+ #If the HTTP request fails...
40
+ else
41
+ raise "IP address not found"
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,3 @@
1
+ class Geolocater
2
+ VERSION = "0.0.1.alpha"
3
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+ require 'geolocater'
3
+
4
+ describe Geolocater do
5
+
6
+ before :each do
7
+ @geolocater = Geolocater.new
8
+ end
9
+
10
+ describe "#ip_lookup" do
11
+ it "throws an ArgumentError error if no parameters passed" do
12
+ expect {Geolocater.ip_lookup}.to raise_error(ArgumentError)
13
+ end
14
+
15
+ it "throws an ArgumentError error if more than one parameter passed" do
16
+ expect {Geolocater.ip_lookup("sdfsdf","sdfsadf")}.to raise_error(ArgumentError)
17
+ end
18
+
19
+ it "only accepts input that passes a basic IP regex" do
20
+ expect {Geolocater.ip_lookup("sdfsadfsdf")}.to raise_error(RuntimeError,"Not a valid IPv4 address")
21
+ end
22
+
23
+ it "should not accept localhost/loopback address" do
24
+ expect {Geolocater.ip_lookup("127.0.0.1")}.to raise_error(RuntimeError,"Can't lookup localhost address. Please use an external IP address!")
25
+ end
26
+
27
+ it "successfully makes a HTTP request" do
28
+ expect{Geolocater.ip_lookup("231.4.8.6").success?}.to be_true
29
+ end
30
+
31
+ it "throws an error for HTTP statuses other than 200" do
32
+ expect {Geolocater.ip_lookup("0.0.0.0")}.to raise_error(RuntimeError,"IP address not found")
33
+ end
34
+
35
+ it "parses the HTTP body with JSON" do
36
+ Geolocater.ip_lookup("123.45.6.28").should be_an_instance_of Hash
37
+ end
38
+
39
+ it "has a value for city" do
40
+ @result = Geolocater.ip_lookup("123.45.6.28")["city"].should_not be_empty
41
+ expect {Geolocater.ip_lookup("240.0.0.0")}.to raise_error(RuntimeError,"Incomplete record. Please try another address")
42
+ end
43
+ end
44
+ end
45
+
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'geolocater'
3
+
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geolocater
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.alpha
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Dan Barrett
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-11 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &19013520 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *19013520
25
+ - !ruby/object:Gem::Dependency
26
+ name: guard
27
+ requirement: &19013000 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *19013000
36
+ - !ruby/object:Gem::Dependency
37
+ name: faraday
38
+ requirement: &19012420 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *19012420
47
+ - !ruby/object:Gem::Dependency
48
+ name: json
49
+ requirement: &19011860 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *19011860
58
+ description: geolocater is a simple ruby wrapper for the freegeoip.net API
59
+ email:
60
+ - dbarrett83@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - .rspec
67
+ - Gemfile
68
+ - README.md
69
+ - Rakefile
70
+ - geolocater.gemspec
71
+ - lib/geolocater.rb
72
+ - lib/geolocater/version.rb
73
+ - spec/geolocater_spec.rb
74
+ - spec/spec_helper.rb
75
+ homepage: http://www.about.me/thoughtpunch
76
+ licenses:
77
+ - MIT
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: 1.9.0
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>'
92
+ - !ruby/object:Gem::Version
93
+ version: 1.3.1
94
+ requirements: []
95
+ rubyforge_project: geolocater
96
+ rubygems_version: 1.8.10
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Yet Another IP Geolocation Gem
100
+ test_files: []
101
+ has_rdoc: