Skip to content

Commit

Permalink
Merge pull request #8 from ipinfo/uman/fixes
Browse files Browse the repository at this point in the history
v1.0.0; dependency upgrades & other fixes
  • Loading branch information
UmanShahzad authored Jan 28, 2021
2 parents 1f0a023 + 88fd4b9 commit 48a46ea
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 39 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
ipinfo-rails-*.gem
48 changes: 48 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
AllCops:
TargetRubyVersion: 2.5
NewCops: enable

Layout/IndentationWidth:
Width: 4

Layout/LineLength:
Enabled: true
Max: 80

Lint/MissingSuper:
Enabled: false

Metrics/MethodLength:
Enabled: false

Metrics/AbcSize:
Enabled: false

Metrics/ClassLength:
Enabled: false

Lint/DuplicateMethods:
Enabled: false

Style/Documentation:
Enabled: false

Style/ClassAndModuleChildren:
Enabled: false

Style/MethodCallWithArgsParentheses:
EnforcedStyle: require_parentheses
IgnoreMacros: false
IgnoredPatterns: []
AllowParenthesesInMultilineCall: true
AllowParenthesesInChaining: true
AllowParenthesesInCamelCaseMethod: true

Style/MethodCallWithoutArgsParentheses:
Enabled: false

Naming/FileName:
Enabled: false

Naming/PredicateName:
Enabled: false
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.7.2
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec

group :development do
gem 'bundler'
gem 'minitest'
gem 'minitest-reporters'
gem 'rubocop'
end
43 changes: 30 additions & 13 deletions ipinfo-rails.gemspec
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
# frozen_string_literal: true

lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require 'ipinfo-rails/version'

Gem::Specification.new do |s|
s.name = 'ipinfo-rails'
s.version = '0.1.1'
s.date = '2018-12-10'
s.summary = "The official Rails gem for IPinfo. IPinfo prides itself on being the most reliable, accurate, and in-depth source of IP address data available anywhere. We process terabytes of data to produce our custom IP geolocation, company, carrier and IP type data sets. You can visit our developer docs at https://ipinfo.io/developers."
s.description = "The official Rails gem for IPinfo. IPinfo prides itself on being the most reliable, accurate, and in-depth source of IP address data available anywhere. We process terabytes of data to produce our custom IP geolocation, company, carrier and IP type data sets. You can visit our developer docs at https://ipinfo.io/developers."
s.authors = ["James Timmins"]
s.email = '[email protected]'
s.files = ["lib/ipinfo-rails.rb"]
s.homepage = 'https://ipinfo.io'
s.license = 'Apache-2.0'

s.add_runtime_dependency 'IPinfo', '~> 0.1.2'
s.add_runtime_dependency 'rack', '~> 2.0'
s.name = 'ipinfo-rails'
s.version = IPinfoRails::VERSION
s.required_ruby_version = '>= 2.5.0'
s.date = '2018-12-10'
s.summary = 'The official Rails gem for IPinfo. IPinfo prides itself on ' \
'being the most reliable, accurate, and in-depth source of ' \
'IP address data available anywhere. We process terabytes ' \
'of data to produce our custom IP geolocation, company, ' \
'carrier and IP type data sets. You can visit our developer ' \
'docs at https://ipinfo.io/developers.'
s.description = s.summary
s.authors = ['James Timmins', 'Uman Shahzad']
s.email = ['[email protected]', '[email protected]']
s.homepage = 'https://ipinfo.io'
s.license = 'Apache-2.0'

s.add_runtime_dependency 'IPinfo', '~> 1.0.1'
s.add_runtime_dependency 'rack', '~> 2.0'

s.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
s.require_paths = ['lib']
end
54 changes: 28 additions & 26 deletions lib/ipinfo-rails.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
# frozen_string_literal: true

require 'rack'
require 'ipinfo'

class IPinfoMiddleware
def initialize(app, cache_options = {})
@app = app

token = cache_options.fetch(:token, nil)
@ipinfo = IPinfo::create(@token, cache_options)
@filter = cache_options.fetch(:filter, nil)
end

def call(env)
env["called"] = "yes"
request = Rack::Request.new(env)

if !@filter.nil?
filtered = @filter.call(request)
else
filtered = is_bot(request)
def initialize(app, cache_options = {})
@app = app
@token = cache_options.fetch(:token, nil)
@ipinfo = IPinfo.create(@token, cache_options)
@filter = cache_options.fetch(:filter, nil)
end

if filtered
env["ipinfo"] = nil
else
ip = request.ip
env["ipinfo"] = @ipinfo.details(ip)
def call(env)
env['called'] = 'yes'
request = Rack::Request.new(env)

filtered = if @filter.nil?
is_bot(request)
else
@filter.call(request)
end

if filtered
env['ipinfo'] = nil
else
ip = request.ip
env['ipinfo'] = @ipinfo.details(ip)
end

@app.call(env)
end

@app.call(env)
end
private

private
def is_bot(request)
user_agent = request.user_agent.downcase
user_agent.include?("bot") || user_agent.include?("spider")
user_agent = request.user_agent.downcase
user_agent.include?('bot') || user_agent.include?('spider')
end
end
5 changes: 5 additions & 0 deletions lib/ipinfo-rails/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

module IPinfoRails
VERSION = '1.0.0'
end

0 comments on commit 48a46ea

Please sign in to comment.