From 20404b09e519d4a72e51162df948927810fe058e Mon Sep 17 00:00:00 2001 From: Stanislav K Date: Mon, 30 Oct 2017 16:26:47 +0700 Subject: [PATCH 01/10] first spec --- lib/ipinfo_io.rb | 4 ++++ test/ipinfo_io_test.rb | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/lib/ipinfo_io.rb b/lib/ipinfo_io.rb index 0cdce22..17b8cc2 100644 --- a/lib/ipinfo_io.rb +++ b/lib/ipinfo_io.rb @@ -1,5 +1,9 @@ require "ipinfo_io/version" module IpinfoIo + def self.call + + end + # Your code goes here... end diff --git a/test/ipinfo_io_test.rb b/test/ipinfo_io_test.rb index e9b4f80..0ace822 100644 --- a/test/ipinfo_io_test.rb +++ b/test/ipinfo_io_test.rb @@ -4,4 +4,8 @@ class IpinfoIoTest < Minitest::Test def test_that_it_has_a_version_number refute_nil ::IpinfoIo::VERSION end + + def test_machine_location + assert_equal ({country: "Thailand"}), IpinfoIo.call + end end From 685fc113dc5202c66eeeed6cf85e17cd1b06d504 Mon Sep 17 00:00:00 2001 From: Stanislav K Date: Mon, 30 Oct 2017 16:38:41 +0700 Subject: [PATCH 02/10] limit gem usage to > Ruby 2.0 --- Gemfile | 13 +++++++++++++ README.md | 3 ++- ipinfo_io.gemspec | 7 +++---- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index ff73c03..40e7605 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,16 @@ source 'https://rubygems.org' # Specify your gem's dependencies in ipinfo_io.gemspec gemspec + + +group :development do + if RUBY_VERSION >= "2.2.2" + gem "rack", ">= 1.5" + else + gem "rack", ">= 1.5", "< 2.0" # rubocop:disable Bundler/DuplicatedGem + end + + gem "bundler" + gem "rake" + gem "minitest" +end \ No newline at end of file diff --git a/README.md b/README.md index e67278c..330ef7f 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,8 @@ Or install it yourself as: ## Usage -... +### Requirements +- Ruby 2.0+ ## Development diff --git a/ipinfo_io.gemspec b/ipinfo_io.gemspec index 112990d..a69f2cc 100644 --- a/ipinfo_io.gemspec +++ b/ipinfo_io.gemspec @@ -6,6 +6,7 @@ require 'ipinfo_io/version' Gem::Specification.new do |spec| spec.name = "ipinfo_io" spec.version = IpinfoIo::VERSION + spec.required_ruby_version = ">= 2.0.0" spec.authors = ["Stanislav K"] spec.email = ["sk@skylup.com"] @@ -22,14 +23,12 @@ Gem::Specification.new do |spec| "public gem pushes." end + spec.add_dependency 'faraday' + spec.files = `git ls-files -z`.split("\x0").reject do |f| f.match(%r{^(test|spec|features)/}) end spec.bindir = "exe" spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] - - spec.add_development_dependency "bundler", "~> 1.13" - spec.add_development_dependency "rake", "~> 10.0" - spec.add_development_dependency "minitest", "~> 5.0" end From 430b0adf18d4010d5f046c706e12d14433feb968 Mon Sep 17 00:00:00 2001 From: Stanislav K Date: Mon, 30 Oct 2017 16:41:18 +0700 Subject: [PATCH 03/10] setting proper code-style with .editorconfig --- .editorconfig | 13 +++++++++++++ README.md | 12 +++++++++++- lib/ipinfo_io.rb | 2 +- 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..1a00e54 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +# Two spaces for indenting +[*] +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 2 +charset = utf-8 +trim_trailing_whitespace = true +max_line_length = 120 diff --git a/README.md b/README.md index 330ef7f..6e5e837 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Or install it yourself as: $ gem install ipinfo_io ## Usage +Not yeat published on rubygems.org ### Requirements - Ruby 2.0+ @@ -33,5 +34,14 @@ To install this gem onto your local machine, run `bundle exec rake install`. To ## Contributing -Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ipinfo_io. +Setting up project: +- Be sure to have **rvm** or **rbenv** +- Be sure to have **bundler gem** installed. +- `bundle install` +- Check that your IDE/editor can handle [.editorconfig](http://editorconfig.org) file + +Running tests is easy: +- `bundle exec rake` + +Bug reports and pull requests are welcome on GitHub at https://github.com/ipinfoio/ruby/. diff --git a/lib/ipinfo_io.rb b/lib/ipinfo_io.rb index 17b8cc2..d6cbf7d 100644 --- a/lib/ipinfo_io.rb +++ b/lib/ipinfo_io.rb @@ -2,7 +2,7 @@ module IpinfoIo def self.call - + end # Your code goes here... From 72da583bdbb1e24161c679216db39c3f0fd1d6b9 Mon Sep 17 00:00:00 2001 From: Stanislav K Date: Mon, 30 Oct 2017 16:48:35 +0700 Subject: [PATCH 04/10] make sure to test on all major Ruby versions --- .travis.yml | 8 +++++++- Gemfile | 8 ++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 54b8699..2017537 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,11 @@ sudo: false language: ruby rvm: - - 2.3.3 + - 2.0.0 + - 2.1 + - 2.2 + - 2.3.0 + - 2.4.0 + - jruby-9.0.5.0 + before_install: gem install bundler -v 1.13.7 diff --git a/Gemfile b/Gemfile index 40e7605..01b19ee 100644 --- a/Gemfile +++ b/Gemfile @@ -5,13 +5,17 @@ gemspec group :development do + # Rack 2.0+ requires Ruby >= 2.2.2 which is problematic for the test suite on + # older Ruby versions. Check Ruby the version here and put a maximum + # constraint on Rack if necessary. + if RUBY_VERSION >= "2.2.2" gem "rack", ">= 1.5" else - gem "rack", ">= 1.5", "< 2.0" # rubocop:disable Bundler/DuplicatedGem + gem "rack", ">= 1.5", "< 2.0" end gem "bundler" gem "rake" gem "minitest" -end \ No newline at end of file +end From db6aee1a29977d14f43ceeb8b15812658b5e24a2 Mon Sep 17 00:00:00 2001 From: Stanislav K Date: Mon, 30 Oct 2017 17:08:42 +0700 Subject: [PATCH 05/10] first spec working --- Gemfile | 10 ++++++++++ lib/ipinfo_io.rb | 9 +++++++-- test/ipinfo_io_test.rb | 11 ++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 01b19ee..7b73633 100644 --- a/Gemfile +++ b/Gemfile @@ -18,4 +18,14 @@ group :development do gem "bundler" gem "rake" gem "minitest" + + platforms :mri do + # to avoid problems, bring Byebug in on just versions of Ruby under which + # it's known to work well + if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new("2.0.0") + gem "byebug" + gem "pry" + gem "pry-byebug" + end + end end diff --git a/lib/ipinfo_io.rb b/lib/ipinfo_io.rb index d6cbf7d..9b5ef03 100644 --- a/lib/ipinfo_io.rb +++ b/lib/ipinfo_io.rb @@ -1,9 +1,14 @@ require "ipinfo_io/version" +require 'faraday' +require 'json' +require 'pry' module IpinfoIo def self.call + response = Faraday.get('https://ipinfo.io') do |req| + req.headers['User-Agent'] = 'curl/7.30.0' + end + JSON.parse(response.body) end - - # Your code goes here... end diff --git a/test/ipinfo_io_test.rb b/test/ipinfo_io_test.rb index 0ace822..fb4f427 100644 --- a/test/ipinfo_io_test.rb +++ b/test/ipinfo_io_test.rb @@ -6,6 +6,15 @@ def test_that_it_has_a_version_number end def test_machine_location - assert_equal ({country: "Thailand"}), IpinfoIo.call + response = { + "ip" => "49.229.162.85", + "city" => "", + "region" => "", + "country" => "TH", + "loc" => "13.7500,100.4667", + "org" => "AS131445 ADVANCED WIRELESS NETWORK COMPANY LIMITED" + } + + assert_equal response, IpinfoIo.call end end From 0191c7266be9d7ec8a1fc92650860a2413ef568c Mon Sep 17 00:00:00 2001 From: Stanislav K Date: Mon, 30 Oct 2017 17:19:18 +0700 Subject: [PATCH 06/10] Record all HTTP interactions with VCR gem --- Gemfile | 3 ++ test/cassettes/current_machine_search.yml | 52 +++++++++++++++++++++++ test/ipinfo_io_test.rb | 4 +- test/test_helper.rb | 16 +++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 test/cassettes/current_machine_search.yml diff --git a/Gemfile b/Gemfile index 7b73633..4211de9 100644 --- a/Gemfile +++ b/Gemfile @@ -18,6 +18,9 @@ group :development do gem "bundler" gem "rake" gem "minitest" + gem 'minitest-vcr' + gem 'minitest-reporters' + gem 'webmock' platforms :mri do # to avoid problems, bring Byebug in on just versions of Ruby under which diff --git a/test/cassettes/current_machine_search.yml b/test/cassettes/current_machine_search.yml new file mode 100644 index 0000000..647dba9 --- /dev/null +++ b/test/cassettes/current_machine_search.yml @@ -0,0 +1,52 @@ +--- +http_interactions: +- request: + method: get + uri: https://ipinfo.io/ + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - curl/7.30.0 + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Server: + - nginx + Date: + - Mon, 30 Oct 2017 10:18:28 GMT + Content-Type: + - application/json; charset=utf-8 + Vary: + - Accept-Encoding + X-Cloud-Trace-Context: + - bee8a4483075fabf2bced6a407f9256d/8252074120977605115;o=0 + Access-Control-Allow-Origin: + - "*" + X-Content-Type-Options: + - nosniff + Via: + - 1.1 google + Transfer-Encoding: + - chunked + body: + encoding: ASCII-8BIT + string: |- + { + "ip": "49.229.162.85", + "city": "", + "region": "", + "country": "TH", + "loc": "13.7500,100.4667", + "org": "AS131445 ADVANCED WIRELESS NETWORK COMPANY LIMITED" + } + http_version: + recorded_at: Mon, 30 Oct 2017 10:18:27 GMT +recorded_with: VCR 3.0.3 diff --git a/test/ipinfo_io_test.rb b/test/ipinfo_io_test.rb index fb4f427..1d71aaa 100644 --- a/test/ipinfo_io_test.rb +++ b/test/ipinfo_io_test.rb @@ -15,6 +15,8 @@ def test_machine_location "org" => "AS131445 ADVANCED WIRELESS NETWORK COMPANY LIMITED" } - assert_equal response, IpinfoIo.call + VCR.use_cassette('current machine search') do + assert_equal response, IpinfoIo.call + end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index d35ff80..3b152e0 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,3 +2,19 @@ require 'ipinfo_io' require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest-vcr' +require 'webmock' + +Minitest::Reporters.use!( + Minitest::Reporters::SpecReporter.new +) + + +VCR.configure do |c| + c.cassette_library_dir = 'test/cassettes' + c.hook_into :webmock + c.allow_http_connections_when_no_cassette = true +end + +MinitestVcr::Spec.configure! From 8a46dbafc69999092c3ef83721062a19f95a83b7 Mon Sep 17 00:00:00 2001 From: Stanislav K Date: Mon, 30 Oct 2017 17:24:21 +0700 Subject: [PATCH 07/10] change jruby version to 9.1 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2017537..486e976 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,6 @@ rvm: - 2.2 - 2.3.0 - 2.4.0 - - jruby-9.0.5.0 + - jruby-9.1 before_install: gem install bundler -v 1.13.7 From 4ff63cbe0d1cc3bc8efeb6007f55f9e1457d79c9 Mon Sep 17 00:00:00 2001 From: Stanislav K Date: Mon, 30 Oct 2017 17:38:29 +0700 Subject: [PATCH 08/10] cache dependencies, remove pry code --- .travis.yml | 2 ++ lib/ipinfo_io.rb | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 486e976..36aad43 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ sudo: false language: ruby +cache: bundler + rvm: - 2.0.0 - 2.1 diff --git a/lib/ipinfo_io.rb b/lib/ipinfo_io.rb index 9b5ef03..05b164a 100644 --- a/lib/ipinfo_io.rb +++ b/lib/ipinfo_io.rb @@ -1,7 +1,6 @@ require "ipinfo_io/version" require 'faraday' require 'json' -require 'pry' module IpinfoIo def self.call From 023fd3830fd6dedea40636f552b5d7ec3f96abb0 Mon Sep 17 00:00:00 2001 From: Stanislav K Date: Mon, 30 Oct 2017 17:50:14 +0700 Subject: [PATCH 09/10] force travis to update rvm or latest jruby is missing --- .travis.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 36aad43..d9532ad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,8 @@ rvm: - 2.2 - 2.3.0 - 2.4.0 - - jruby-9.1 + - jruby-9.1.13.0 -before_install: gem install bundler -v 1.13.7 +before_install: +- gem install bundler +- if [[ "$TRAVIS_RUBY_VERSION" =~ "jruby" ]]; then rvm get head && rvm reload From 87b80a3786964d110491654a99cd15dcda8f26d1 Mon Sep 17 00:00:00 2001 From: Stanislav K Date: Mon, 30 Oct 2017 17:58:38 +0700 Subject: [PATCH 10/10] figure out Jruby later --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index d9532ad..08f56de 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,8 +8,6 @@ rvm: - 2.2 - 2.3.0 - 2.4.0 - - jruby-9.1.13.0 before_install: - gem install bundler -- if [[ "$TRAVIS_RUBY_VERSION" =~ "jruby" ]]; then rvm get head && rvm reload