freegeoip-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: df91150c1279ff29613c0169914cd73c8163bd09068c200382a5833d0efe3f22
4
+ data.tar.gz: c89707641b2ce7cc3c27ea4ab0a0b4d93c52c315c289e162d2ae8aabec218872
5
+ SHA512:
6
+ metadata.gz: 77c7ef528044946767eab99403e395a1955b3173c83158e1a87836f803329e5d5cec4b27b9c61df344b6ba9e812a205fe365b4b866162b884bc0206bc4a11b4c
7
+ data.tar.gz: 9f7cf21e4f5bc808bde053b810babeb86c74f6bcb3681e1cd8b3cc9afaf6d686b8053b193f7729ebdcee6fb1c25231c22a0137177b55b5c8bed57fc5bace8710
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Varvet AB
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ [![Build status](https://travis-ci.org/varvet/freegeoip-rails.svg?branch=master)](https://travis-ci.org/varvet/freegeoip-rails)
2
+
3
+ # Freegeoip
4
+
5
+ This gem provides a simple Rails engine with an IP geolocation API that ducks with [Freegeoip](https://github.com/fiorix/freegeoip).
6
+
7
+ It reads MaxMindDB's open source GeoLite2 City database, with the help of [maxminddb](https://github.com/yhirose/maxminddb) gem, a pure Ruby implementation.
8
+
9
+ You can find MaxMind's GeoLite2 City database [here](https://dev.maxmind.com/geoip/geoip2/geolite2/).
10
+
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem "freegeoip-rails", require: "freegeoip"
18
+ ```
19
+
20
+ Mount the engine in your routes file:
21
+
22
+ ```ruby
23
+ mount Freegeoip::Engine, at: "/json"
24
+ ```
25
+
26
+ Add an initializer like `config/initializers/freegeoip.rb` and tell the engine where it can find your GeoLite2 City database:
27
+
28
+ ```ruby
29
+ Freegeoip.configure do |config|
30
+ # Anything that can be opened by OpenURI
31
+ config.db_location = Rails.root.join "vendor/maxminddb/GeoLite2-City.mmdb"
32
+ end
33
+ ```
34
+
35
+ Start your web server and point your browser to `http://localhost:3000/json/www.ruby-lang.org` or `http://localhost:3000/json/151.101.1.178` and you should get a response with JSON data.
36
+
37
+ ## Contributing
38
+
39
+ Contributions are always welcome.
40
+
41
+
42
+ ## Varvet
43
+
44
+ Crafted with love at [Varvet](https://www.varvet.com/).
45
+ Skapad med omsorg av [Varvet digital byrå](https://www.varvet.se/).
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'bundler/gem_tasks'
8
+
9
+ Bundler::GemHelper.install_tasks
10
+
11
+ require 'rspec/core'
12
+ require 'rspec/core/rake_task'
13
+
14
+
15
+ desc "Run all specs in spec directory (excluding plugin specs)"
16
+ RSpec::Core::RakeTask.new(:spec)
17
+
18
+ task :default => :spec
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Freegeoip
4
+ class ApplicationController < ActionController::Base
5
+ end
6
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Freegeoip
4
+ class LookupsController < ApplicationController
5
+ def show
6
+ if result = Freegeoip::Lookup.hostname_or_ip(params[:hostname_or_ip])
7
+ render json: result
8
+ else
9
+ render body: "404 page not found", status: 404
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "resolv"
4
+ require "maxminddb"
5
+ require "open-uri"
6
+
7
+ module Freegeoip
8
+ class Lookup
9
+ def self.hostname_or_ip(string, locale = "en")
10
+ ip = resolve string.to_s
11
+
12
+ return nil unless ip
13
+
14
+ result = lookup(ip)
15
+
16
+ return nil if result.empty?
17
+
18
+ {
19
+ ip: ip,
20
+ country_code: result.dig("country", "iso_code"),
21
+ country_name: result.dig("country", "names", locale),
22
+ region_code: result.dig("subdivisions", 0, "iso_code"),
23
+ region_name: result.dig("subdivisions", 0, "names", locale),
24
+ city: result.dig("city", "names", locale),
25
+ zip_code: result.dig("postal", "code"),
26
+ time_zone: result.dig("location", "time_zone"),
27
+ latitude: result.dig("location", "latitude"),
28
+ longitude: result.dig("location", "longitude"),
29
+ metro_code: (result.dig("location", "metro_code") || 0),
30
+ }
31
+ end
32
+
33
+ private
34
+ def self.resolve(string)
35
+ return string if string =~ Resolv::IPv4::Regex
36
+ Resolv.getaddress string
37
+ rescue Resolv::ResolvError
38
+ nil
39
+ end
40
+
41
+ def self.lookup(ip)
42
+ db.lookup(ip).to_hash
43
+ end
44
+
45
+ def self.db
46
+ return @db if instance_variable_defined?("@db")
47
+ @db = MaxMindDB.new read_db
48
+ end
49
+
50
+
51
+ def self.read_db
52
+ if Freegeoip.config.db_location
53
+ open Freegeoip.config.db_location
54
+ else
55
+ raise Freegeoip::ConfigError, "GeoLite2-City database location not configured"
56
+ end
57
+ end
58
+ end
59
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ Freegeoip::Engine.routes.draw do
4
+ resources :lookups, only: :show, param: :hostname_or_ip, format: false
5
+ get "/*hostname_or_ip", to: "lookups#show", format: false
6
+ end
data/lib/freegeoip.rb ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "freegeoip/engine"
4
+
5
+ module Freegeoip
6
+ autoload :Config, 'freegeoip/config'
7
+
8
+ class ConfigError < StandardError; end
9
+
10
+ def self.configure(&block)
11
+ Config.configure(&block)
12
+ end
13
+
14
+ def self.config
15
+ Config
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Freegeoip
4
+ module Config
5
+ extend self
6
+
7
+ attr_accessor :db_location, :param_name
8
+
9
+ def configure
10
+ yield self
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module Freegeoip
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Freegeoip
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module Freegeoip
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: freegeoip-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ingemar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: maxminddb
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.7.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.7.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Rails engine that provides an IP geolocation API that ducks with Freegeoip
70
+ email:
71
+ - ingemar@xox.se
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - MIT-LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - app/controllers/freegeoip/application_controller.rb
80
+ - app/controllers/freegeoip/lookups_controller.rb
81
+ - app/models/freegeoip/lookup.rb
82
+ - config/routes.rb
83
+ - lib/freegeoip.rb
84
+ - lib/freegeoip/config.rb
85
+ - lib/freegeoip/engine.rb
86
+ - lib/freegeoip/version.rb
87
+ homepage: https://varvet.com
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.7.4
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Rails engine with IP geolocation API like Freegeoip
111
+ test_files: []