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/.travis.yml b/.travis.yml index 54b8699..08f56de 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,13 @@ sudo: false language: ruby +cache: bundler + rvm: - - 2.3.3 -before_install: gem install bundler -v 1.13.7 + - 2.0.0 + - 2.1 + - 2.2 + - 2.3.0 + - 2.4.0 + +before_install: +- gem install bundler diff --git a/Gemfile b/Gemfile index ff73c03..4211de9 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,33 @@ source 'https://rubygems.org' # Specify your gem's dependencies in ipinfo_io.gemspec 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" + end + + 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 + # 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/README.md b/README.md index e67278c..6e5e837 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,10 @@ Or install it yourself as: $ gem install ipinfo_io ## Usage +Not yeat published on rubygems.org -... +### Requirements +- Ruby 2.0+ ## Development @@ -32,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/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 diff --git a/lib/ipinfo_io.rb b/lib/ipinfo_io.rb index 0cdce22..05b164a 100644 --- a/lib/ipinfo_io.rb +++ b/lib/ipinfo_io.rb @@ -1,5 +1,13 @@ require "ipinfo_io/version" +require 'faraday' +require 'json' module IpinfoIo - # Your code goes here... + 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 end 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 e9b4f80..1d71aaa 100644 --- a/test/ipinfo_io_test.rb +++ b/test/ipinfo_io_test.rb @@ -4,4 +4,19 @@ class IpinfoIoTest < Minitest::Test def test_that_it_has_a_version_number refute_nil ::IpinfoIo::VERSION end + + def test_machine_location + response = { + "ip" => "49.229.162.85", + "city" => "", + "region" => "", + "country" => "TH", + "loc" => "13.7500,100.4667", + "org" => "AS131445 ADVANCED WIRELESS NETWORK COMPANY LIMITED" + } + + 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!