diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eee3565 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +/.bundle/ +/.yardoc +/Gemfile.lock +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ +ipinfo-rails-*.gem diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..28dfd38 --- /dev/null +++ b/.rubocop.yml @@ -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 diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..37c2961 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.7.2 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..c6e73e0 --- /dev/null +++ b/Gemfile @@ -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 diff --git a/ipinfo-rails.gemspec b/ipinfo-rails.gemspec index a75211e..28380a2 100644 --- a/ipinfo-rails.gemspec +++ b/ipinfo-rails.gemspec @@ -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 = 'jameshtimmins@gmail.com' - 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 = ['jameshtimmins@gmail.com', 'uman@mslm.io'] + 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 diff --git a/lib/ipinfo-rails.rb b/lib/ipinfo-rails.rb index b9711cf..ca2e429 100644 --- a/lib/ipinfo-rails.rb +++ b/lib/ipinfo-rails.rb @@ -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 diff --git a/lib/ipinfo-rails/version.rb b/lib/ipinfo-rails/version.rb new file mode 100644 index 0000000..91b7196 --- /dev/null +++ b/lib/ipinfo-rails/version.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +module IPinfoRails + VERSION = '1.0.0' +end