Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mokevnin committed Oct 6, 2012
1 parent 9769d90 commit 0c56a8b
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 11 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
34 changes: 30 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

10 changes: 10 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions lib/ipgeobase.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 13 additions & 4 deletions lib/ipgeobase/ip_meta_data.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/response.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<ip-answer>
<ip value="46.8.114.116">
<inetnum>46.8.112.0 - 46.8.127.255</inetnum>
<country>RU</country>
<city>Ульяновск</city>
<region>Ульяновская область</region>
<district>Приволжский федеральный округ</district>
<lat>54.321480</lat>
<lng>48.385651</lng>
</ip>
</ip-answer>
22 changes: 22 additions & 0 deletions test/lib/ipgeobase_test.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0c56a8b

Please sign in to comment.