freegeoip-api 1.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8f71b838799971f42f7256a31e6cba90cdabc8d8f4520d344481e1d29ce380e0
4
+ data.tar.gz: c63f6cbe06ca800419d1a939d410b38fcc0fab693beec9d2be0401d302b2e179
5
+ SHA512:
6
+ metadata.gz: 5b716d662429c91d4fca558076c1efa46664a1ed58204a273237c14da0a0e5a48db167aae85a018e1cd9f3bf8b8c8b0407bdbd7eafe32dce7c974b28e096d6a0
7
+ data.tar.gz: '0339cd0bf0fa693e172932ef239640426e2e470f8d0e875e7cc2ebcf835b7b928c4be8f820d29c37ea8a5928c7fff224e20bcfc7df1e6cf3545c7ed815519995'
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ AllCops:
2
+ NewCops: enable
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in freegeoip.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Muhammad Khan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Freegeoip Ruby
2
+
3
+ A Ruby library for the [Freegeoip API](https://freegeoip.app).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'freegeoip-api'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ gem install freegeoip-api
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ Anywhere in your code
28
+
29
+ ```ruby
30
+ require 'freegeoip'
31
+
32
+ Freegeoip.get('google.com')
33
+ => #<Freegeoip::Response:0x007f9552bb9ba0
34
+ @remote="google.com",
35
+ @ip="172.217.7.14",
36
+ @country_code="US",
37
+ @country_name="United States",
38
+ @region_code="CA",
39
+ @region_name="California",
40
+ @city="Mountain View",
41
+ @zip_code="94043",
42
+ @time_zone="America/Los_Angeles",
43
+ @latitude=37.4192,
44
+ @longitude=-122.0574,
45
+ @metro_code=807>
46
+ ```
47
+
48
+ In the command line
49
+
50
+ ```bash
51
+ $ freegeoip get google.com
52
+ Searching for: google.com
53
+ ------------------------
54
+ IP: 172.217.5.14
55
+ Country code: US
56
+ Country name: United States
57
+ Region code: CA
58
+ Region name: California
59
+ City: Mountain View
60
+ ZIP Code: 94043
61
+ Time zone: America/Los_Angeles
62
+ Latitude: 37.4192
63
+ Longitude: -122.0574
64
+ Metro code: 807
65
+ ```
66
+
67
+ ## Development
68
+
69
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
70
+
71
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub.
76
+ This project is intended to be a safe, welcoming
77
+ space for collaboration, and contributors are
78
+ expected to adhere to basic human decency.
79
+
80
+ ## License
81
+
82
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
83
+
84
+ ## Credits
85
+
86
+ > This codebase is very much a modified version of <https://github.com/TheNaoX/freegeoip>
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require 'bundler/setup'
6
+ require 'freegeoip'
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ # (If you use this, don't forget to add pry to your Gemfile!)
12
+ # require "pry"
13
+ # Pry.start
14
+
15
+ require 'irb'
16
+ IRB.start
data/bin/freegeoip ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require 'freegeoip'
6
+
7
+ case ARGV[0]
8
+ when 'get'
9
+ remote = ARGV[1]
10
+
11
+ puts "Searching for: #{remote}"
12
+ puts '------------------------'
13
+
14
+ response = Freegeoip.get(remote)
15
+
16
+ puts "IP: #{response.ip}"
17
+ puts "Country code: #{response.country_code}"
18
+ puts "Country name: #{response.country_name}"
19
+ puts "Region code: #{response.region_code}"
20
+ puts "Region name: #{response.region_name}"
21
+ puts "City: #{response.city}"
22
+ puts "ZIP Code: #{response.zip_code}"
23
+ puts "Time zone: #{response.time_zone}"
24
+ puts "Latitude: #{response.latitude}"
25
+ puts "Longitude: #{response.longitude}"
26
+ puts "Metro code: #{response.metro_code}"
27
+ else
28
+ puts 'Unknown command, usage: freegeoip get [IP|DOMAIN]'
29
+ end
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/freegeoip.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'freegeoip/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'freegeoip-api'
9
+ spec.version = Freegeoip::VERSION
10
+ spec.authors = ['MBM_1607']
11
+ spec.email = ['muhammadkhan1607@gmail.com']
12
+
13
+ spec.summary = 'Ruby library for interacting with the freegeoip.app API'
14
+ spec.description = 'This gem will help you access the freegeoip.app API from your ruby code'
15
+ spec.homepage = 'https://github.com/MBM1607/freegeoip-api'
16
+ spec.license = 'MIT'
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
22
+ spec.metadata['rubygems_mfa_required'] = 'true'
23
+ else
24
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
25
+ 'public gem pushes.'
26
+ end
27
+
28
+ spec.required_ruby_version = '>= 2.7.4'
29
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
30
+ f.match(%r{^(test|spec|features)/})
31
+ end
32
+ spec.bindir = 'bin'
33
+ spec.executables = ['freegeoip']
34
+ spec.require_paths = ['lib']
35
+
36
+ spec.add_development_dependency 'bundler', '~> 2.3.11'
37
+ spec.add_development_dependency 'rake', '~> 13.0'
38
+ spec.add_development_dependency 'rspec', '~> 3.11'
39
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'uri'
5
+ require 'net/http'
6
+
7
+ module Freegeoip
8
+ # Class to contain main request making logic to api.freegeoip.app
9
+ class Request
10
+ ENDPOINT = 'https://api.freegeoip.app/json'
11
+
12
+ attr_reader :remote
13
+
14
+ def initialize(remote, api_key)
15
+ @remote = remote
16
+ @api_key = api_key
17
+ end
18
+
19
+ def get
20
+ uri = URI("#{ENDPOINT}/#{remote}?apikey=#{@api_key}")
21
+ res = Net::HTTP.get(uri)
22
+
23
+ JSON.parse(res)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Freegeoip
4
+ # Class to contain main api response parsing
5
+ class Response
6
+ attr_reader :remote, :ip, :country_code, :country_name,
7
+ :region_code, :region_name, :city, :zip_code,
8
+ :time_zone, :latitude, :longitude, :metro_code
9
+
10
+ def initialize(remote, data)
11
+ @remote = remote
12
+ data.each do |key, value|
13
+ instance_variable_set "@#{key}", value
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Freegeoip
4
+ VERSION = '1.0.0'
5
+ end
data/lib/freegeoip.rb ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'freegeoip/version'
4
+ require 'freegeoip/request'
5
+ require 'freegeoip/response'
6
+ require 'helpers/configuration'
7
+
8
+ # Main gem module
9
+ module Freegeoip
10
+ extend Configuration
11
+
12
+ define_setting :api_key, 'XXXXXXXXX'
13
+
14
+ def self.get(remote)
15
+ request = Request.new(remote, Freegeoip.api_key)
16
+ Response.new(remote, request.get)
17
+ end
18
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Style/ClassVars
4
+
5
+ # Module for configuration to allow fancy modification for gem users
6
+ # https://www.viget.com/articles/easy-gem-configuration-variables-with-defaults/
7
+ module Configuration
8
+ def configuration
9
+ yield self
10
+ end
11
+
12
+ def define_setting(name, default = nil)
13
+ class_variable_set("@@#{name}", default)
14
+
15
+ define_class_method "#{name}=" do |value|
16
+ class_variable_set("@@#{name}", value)
17
+ end
18
+
19
+ define_class_method name do
20
+ class_variable_get("@@#{name}")
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def define_class_method(name, &block)
27
+ (class << self; self; end).instance_eval do
28
+ define_method name, &block
29
+ end
30
+ end
31
+ end
32
+
33
+ # rubocop:enable Style/ClassVars
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: freegeoip-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - MBM_1607
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-04-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.3.11
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.3.11
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.11'
55
+ description: This gem will help you access the freegeoip.app API from your ruby code
56
+ email:
57
+ - muhammadkhan1607@gmail.com
58
+ executables:
59
+ - freegeoip
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".rubocop.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/freegeoip
72
+ - bin/setup
73
+ - freegeoip.gemspec
74
+ - lib/freegeoip.rb
75
+ - lib/freegeoip/request.rb
76
+ - lib/freegeoip/response.rb
77
+ - lib/freegeoip/version.rb
78
+ - lib/helpers/configuration.rb
79
+ homepage: https://github.com/MBM1607/freegeoip-api
80
+ licenses:
81
+ - MIT
82
+ metadata:
83
+ allowed_push_host: https://rubygems.org
84
+ rubygems_mfa_required: 'true'
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 2.7.4
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.1.6
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Ruby library for interacting with the freegeoip.app API
104
+ test_files: []