Browse files

Basic port complete

  • Loading branch information...
1 parent 5f553e5 commit 9f8c620d582f525d4691a735e39b68f402c65e2e @ezkl committed Apr 11, 2012
Showing with 68 additions and 15 deletions.
  1. +5 −0 .travis.yml
  2. +4 −4 Gemfile
  3. +2 −1 Rakefile
  4. +18 −10 lib/ziptastic.rb
  5. 0 { → lib/ziptastic/data}/zipcodes.db
  6. +3 −0 spec/spec_helper.rb
  7. +33 −0 spec/ziptastic_spec.rb
  8. +3 −0 ziptastic.gemspec
View
5 .travis.yml
@@ -0,0 +1,5 @@
+language: ruby
+rvm:
+ - 1.8.7
+ - 1.9.2
+ - 1.9.3
View
8 Gemfile
@@ -1,8 +1,8 @@
source :rubygems
-gem 'sqlite3'
-gem 'multi_json'
+gemspec
-group :development, :test do
- gem 'rspec'
+group :test do
+ gem 'rake'
end
+
View
3 Rakefile
@@ -2,4 +2,5 @@
require "bundler/gem_tasks"
require 'rspec/core/rake_task'
-RSpec::Core::RakeTask.new(:spec)
+RSpec::Core::RakeTask.new(:spec)
+task :default => :spec
View
28 lib/ziptastic.rb
@@ -1,22 +1,30 @@
require "sqlite3"
require "ostruct"
-require "multi_json"
module Ziptastic
- ZIP_DATABASE = SQLite3::Database.new("zipcodes.db")
+ class NotZipCode < StandardError; end
+ class Region < OpenStruct; end
+
+ DATABASE_PATH = File.expand_path(File.dirname(__FILE__) + "/ziptastic/data/zipcodes.db")
+ ZIP_DATABASE = SQLite3::Database.open(DATABASE_PATH, :readonly => true, :results_as_hash => true)
+
def self.search(zip_code)
- valid_zip_code?(zip_code)
- result = ZIP_DATABASE.execute('select Country, State, City from zipcodes where ZipCode=?', zip_code)
+ search_results = search_for_zip_code(zip_code).inject([]) do |search_results, row|
+ search_results << Region.new(:city => row['city'], :state => row['state'], :country => row['country'])
+ end
+
+ search_results.any? ? search_results : false
end
private
- def self.valid_zip_code?(zip_code)
- unless zip_code =~ /[^a-zA-Z]/
- raise NotZipCode, "#{zip_code} is not a zip code!"
- end
+ def self.search_for_zip_code(zip_code)
+ validate_zip_code(zip_code)
+ ZIP_DATABASE.execute('select City, State, Country from zipcodes where ZipCode=?', zip_code)
end
- class NotZipCode < StandardError; end
+ def self.validate_zip_code(zip_code)
+ raise NotZipCode, "#{zip_code} is not a zip code!" if zip_code =~ /\D/
+ end
end
-puts Ziptastic.search("16335")
+require "ziptastic/version"
View
0 zipcodes.db → lib/ziptastic/data/zipcodes.db
File renamed without changes.
View
3 spec/spec_helper.rb
@@ -1,5 +1,8 @@
+require File.expand_path(File.dirname(__FILE__) + '/../lib/ziptastic')
+
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus
end
+
View
33 spec/ziptastic_spec.rb
@@ -0,0 +1,33 @@
+require "spec_helper"
+
+describe Ziptastic do
+ describe "#query" do
+ context "valid zipcode" do
+ context "w/ results" do
+ it "should return an array of results" do
+ Ziptastic.search("16335").count.should eq 3
+ end
+
+ it "each result should have city, state, and country" do
+ meadville = Ziptastic.search("16335")[0]
+
+ meadville.city.should eq "MEADVILLE"
+ meadville.state.should eq "PA"
+ meadville.country.should eq "US"
+ end
+ end
+
+ context "no result" do
+ it "should return false" do
+ Ziptastic.search("8675309").should eq false
+ end
+ end
+ end
+
+ context "invalid zipcode" do
+ it "should return an exception" do
+ expect { Ziptastic.search("16335a") }.to raise_error(Ziptastic::NotZipCode, "16335a is not a zip code!")
+ end
+ end
+ end
+end
View
3 ziptastic.gemspec
@@ -14,4 +14,7 @@ Gem::Specification.new do |gem|
gem.name = "ziptastic"
gem.require_paths = ["lib"]
gem.version = Ziptastic::VERSION
+
+ gem.add_dependency("sqlite3", "~> 1.3.5")
+ gem.add_development_dependency("rspec", "~> 2.9.0")
end

0 comments on commit 9f8c620

Please sign in to comment.