From 0c56a8b575072cf3ead32d311bb6ed26a6c3f8ff Mon Sep 17 00:00:00 2001 From: kirill Date: Sun, 7 Oct 2012 00:16:59 +0400 Subject: [PATCH] add tests --- Gemfile | 5 +++++ README.md | 34 ++++++++++++++++++++++++++++++---- Rakefile | 10 ++++++++++ lib/ipgeobase.rb | 6 +++--- lib/ipgeobase/ip_meta_data.rb | 17 +++++++++++++---- test/fixtures/response.xml | 11 +++++++++++ test/lib/ipgeobase_test.rb | 22 ++++++++++++++++++++++ test/test_helper.rb | 10 ++++++++++ 8 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 test/fixtures/response.xml create mode 100644 test/lib/ipgeobase_test.rb create mode 100644 test/test_helper.rb diff --git a/Gemfile b/Gemfile index 9eb8dc6..e315e7e 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,8 @@ source "http://rubygems.org" # Specify your gem's dependencies in ipgeobase.gemspec gemspec + +gem 'rake' +gem 'minitest' +gem 'turn' +gem 'webmock', :require => 'webmock/minitest' diff --git a/README.md b/README.md index 5716231..6505470 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,39 @@ # Ipgeobase -## Install +[![Build Status](https://travis-ci.org/mokevnin/ipgeobase.png)](https://travis-ci.org/mokevnin/ipgeobase) -Add this to your `Gemfile`: +Simple format validators for Rails 3 - gem "ipgeobase" +## Installation -## Examples +Add this line to your application's Gemfile: + + gem 'ipgeobase' + +And then execute: + + $ bundle + +Or install it yourself as: + + $ gem install 'ipgeobase' + +## Usage ip_meta = Ipgeobase.lookup('10.11.12.134') ip_meta.city # => Москва ip_meta.country # => Россия + + +## Contributing + +1. Fork it +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Added some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create new Pull Request + +run tests: + + turn -Itest test/lib + diff --git a/Rakefile b/Rakefile index 2995527..57fd452 100644 --- a/Rakefile +++ b/Rakefile @@ -1 +1,11 @@ require "bundler/gem_tasks" + +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.libs << "test" + t.test_files = FileList['test/lib/*_test.rb'] + t.verbose = true +end + +task :default => :test diff --git a/lib/ipgeobase.rb b/lib/ipgeobase.rb index 3875f66..84d0c8d 100644 --- a/lib/ipgeobase.rb +++ b/lib/ipgeobase.rb @@ -1,14 +1,14 @@ require 'uri' require 'net/http' -require 'happymapper' module Ipgeobase - autoload 'Version', 'ipgeobase/version' + URL = 'http://ipgeobase.ru:7020/geo' autoload 'IpMetaData', 'ipgeobase/ip_meta_data' def self.lookup(ip) - uri = URI.parse('http://ipgeobase.ru:7020/geo') + uri = URI.parse(URL) uri.query = URI.encode_www_form :ip => ip + IpMetaData.parse(Net::HTTP.get(uri)) end end diff --git a/lib/ipgeobase/ip_meta_data.rb b/lib/ipgeobase/ip_meta_data.rb index b02d56d..3245ff4 100644 --- a/lib/ipgeobase/ip_meta_data.rb +++ b/lib/ipgeobase/ip_meta_data.rb @@ -1,4 +1,5 @@ -require 'iconv' unless String.new.respond_to?(:encode) +require 'happymapper' +require 'iconv' unless String.instance_methods.include?(:encode) module Ipgeobase class IpMetaData @@ -12,9 +13,17 @@ class IpMetaData element :lat, Float, :deep => true element :lng, Float, :deep => true - def city;encode(@city);end - def country;encode(@country);end - def region;encode(@region);end + def city + encode(@city) + end + + def country + encode(@country) + end + + def region + encode(@region) + end private diff --git a/test/fixtures/response.xml b/test/fixtures/response.xml new file mode 100644 index 0000000..7377ef0 --- /dev/null +++ b/test/fixtures/response.xml @@ -0,0 +1,11 @@ + + + 46.8.112.0 - 46.8.127.255 + RU + Ульяновск + Ульяновская область + Приволжский федеральный округ + 54.321480 + 48.385651 + + diff --git a/test/lib/ipgeobase_test.rb b/test/lib/ipgeobase_test.rb new file mode 100644 index 0000000..548e4fb --- /dev/null +++ b/test/lib/ipgeobase_test.rb @@ -0,0 +1,22 @@ +require 'test_helper' + +class IpgeobaseTest < TestCase + def setup + @ip = '46.8.114.116' + @stub = stub_request(:get, "#{Ipgeobase::URL}?ip=#{@ip}"). + with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}). + to_return(:status => 200, :body => load_fixture('response.xml'), :headers => {}) + end + + def test_lookup_http_query + Ipgeobase.lookup @ip + + assert_requested @stub + end + + def test_lookup_response_object + meta = Ipgeobase.lookup @ip + + assert_equal 54.321480, meta.lat + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..1537a84 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,10 @@ +require 'bundler/setup' +Bundler.require + +MiniTest::Unit.autorun + +class TestCase < MiniTest::Unit::TestCase + def load_fixture(filename) + File.read(File.dirname(__FILE__) + "/fixtures/#{filename}") + end +end