Permalink
Browse files

Adding a description to the gemspec

  • Loading branch information...
1 parent f098b16 commit b17dd765f3292013e6cd0ea85feceb364776782a @ezkl committed Jun 29, 2011
View
@@ -6,4 +6,7 @@ group :development do
gem 'rake', '~> 0.8.7'
gem 'ore-tasks', '~> 0.4'
gem 'rspec', '~> 2.4'
+ gem 'guard', '~> 0.4.2'
+ gem 'guard-rspec', '~> 0.4.0'
+ gem 'growl', '~> 1.0.3'
end
View
@@ -0,0 +1,8 @@
+# A sample Guardfile
+# More info at https://github.com/guard/guard#readme
+
+guard 'rspec', :version => 2 do
+ watch(%r{^spec/.+_spec\.rb$})
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
+ watch('spec/spec_helper.rb') { "spec" }
+end
View
@@ -1,9 +1,12 @@
name: ip2location
-summary: "TODO: Summary"
-description: "TODO: Description"
+summary: "Client for the IP2Location API"
+description: "A simple client for the free IP2Location API."
license: MIT
authors: ezkl
homepage: http://rubygems.org/gems/ip2location
+runtime_dependencies:
+ monster_mash: ~> 0.2
+
development_dependencies:
bundler: ~> 1.0.0
View
@@ -24,7 +24,7 @@ Gem::Specification.new do |gemspec|
version = {
:file => 'lib/ip2location/version.rb',
- :constant => 'Ip2location::VERSION'
+ :constant => 'Ip2Location::VERSION'
}
defaults = {
View
@@ -1 +1,28 @@
+require 'ostruct'
+require 'monster_mash'
+
require 'ip2location/version'
+require 'ip2location/base'
+require 'ip2location/request'
+
+module Ip2Location
+ BASE_URI = "http://api.ip2locationapi.com/"
+ @api_key = nil
+ @user = nil
+
+ class << self
+ attr_accessor :api_key, :user
+
+ def setup
+ yield self if block_given?
+ end
+
+ def request(ip)
+ ::Request.ip2l(ip)
+ end
+
+ end
+
+ class InvalidApiKeyError < StandardError; end
+ class NoLocationDataError < StandardError; end
+end
View
@@ -0,0 +1,10 @@
+module Ip2Location
+ class Base < MonsterMash::Base
+ defaults do
+ cache_timeout 999999
+ user_agent "Ip2LocationAPI Ruby [v#{VERSION}]"
+ params :key => Ip2Location.api_key,
+ :user => Ip2Location.user
+ end
+ end
+end
View
@@ -0,0 +1,35 @@
+module Ip2Location
+ Location = Struct.new(:ip, :country_code, :country, :region, :city, :latitude, :longitude)
+ class Request < Base
+ get(:ip2l) do |ip|
+ uri "#{BASE_URI}"
+ params :user => Ip2Location.user,
+ :key => Ip2Location.api_key,
+ :ip => ip
+ handler do |response|
+ puts response.effective_url
+ if (parsed = Ip2Location::Request.parse_response(response.body, ip))
+ parsed
+ end
+ end
+ end
+
+ def self.parse_response(body, ip)
+ check_response_for_errors(body)
+ location_parts = body.split(/,/)
+ Location.new(ip, location_parts[0], location_parts[1], location_parts[2], location_parts[3], location_parts[4], location_parts[5])
+ end
+
+ def self.check_response_for_errors(body)
+ case body
+ when /Invalid user name or API key/
+ raise InvalidApiKeyError, "The API key [#{Ip2Location.api_key}] or Username [#{Ip2Location.user}] is invalid!"
+ when /NO DATA/
+ raise NoLocationDataError, "The IP you requested has no associated location data."
+ else
+ body
+ end
+ end
+
+ end
+end
@@ -1,4 +1,3 @@
-module Ip2location
- # ip2location version
+module Ip2Location
VERSION = "0.1.0"
-end
+end
@@ -0,0 +1,5 @@
+require 'spec_helper'
+require 'ip2location'
+
+describe Ip2Location::Base do
+end
@@ -0,0 +1,34 @@
+require 'spec_helper'
+require 'ip2location'
+
+describe Ip2Location::Request do
+ it "should return a properly parsed Location structure" do
+ @api_key = "1fe480f5bac2c5261cf686f6265eed138953b98c"
+ @user = "ip2location_ruby"
+
+ Ip2Location.setup do |s|
+ s.api_key = @api_key
+ s.user = @user
+ end
+
+ Ip2Location.request("8.8.8.8").should eq Location.new("8.8.8.8", "US","United States","California","Mountain View","34.305","-86.2981")
+ end
+
+ describe "Parsing" do
+ before(:all) do
+ @no_data = "NO DATA"
+ @bad_api = "Sorry, Invalid user name or API key!"
+ @good_response = "US,United States,California,Mountain View,34.305,-86.2981"
+ end
+
+ it "should process responses properly" do
+ ->{ Ip2Location::Request.check_response_for_errors(@no_data) }.should raise_error(NoLocationDataError, "The IP you requested has no associated location data.")
+ ->{ Ip2Location::Request.check_response_for_errors(@bad_api) }.should raise_error(InvalidApiKeyError)
+ ->{ Ip2Location::Request.check_response_for_errors(@good_response) }.should_not raise_error
+ end
+
+ it "should return body unless response matches an error" do
+ Ip2Location::Request.check_response_for_errors(@good_response).should eq @good_response
+ end
+ end
+end
View
@@ -1,8 +1,21 @@
require 'spec_helper'
require 'ip2location'
-describe Ip2location do
+describe Ip2Location do
it "should have a VERSION constant" do
subject.const_get('VERSION').should_not be_empty
end
+
+ describe "#setup" do
+ it "should take @api_key and @user" do
+ Ip2Location.setup do |s|
+ s.api_key = "1234"
+ s.user = "username"
+ end
+
+ Ip2Location.api_key.should eq "1234"
+ Ip2Location.user.should eq "username"
+ end
+ end
+
end
View
@@ -1,4 +1,4 @@
require 'rspec'
-require 'ip2location/version'
+require 'ip2location'
-include Ip2location
+include Ip2Location

0 comments on commit b17dd76

Please sign in to comment.