zipcode 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ init.rb
2
+ lib/zipcode.rb
3
+ Manifest
4
+ Rakefile
5
+ README.rdoc
6
+ test/test_helper.rb
7
+ test/zipcode_test.rb
@@ -0,0 +1,5 @@
1
+ ## Simple Ruby Class for handling zipcodes when shipping
2
+ # Example
3
+ # zipcode = Zipcode.new("92688-1111")
4
+ # zipcode.code => 92688
5
+ # zipcode.plus_four => 1111
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('zipcode', '0.1.0') do |p|
6
+ p.description = "Creates a Zipcode object class used for validating zipcodes."
7
+ p.url = ""
8
+ p.author = "Tim Matheson"
9
+ p.email = "me@timmatheson.com"
10
+ p.ignore_pattern = ["tmp/*","script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'zipcode'
@@ -0,0 +1,32 @@
1
+ # Simple Ruby Class for handling zipcodes when shipping
2
+ # Example
3
+ # zipcode = Zipcode.new("92688-1111")
4
+ # zipcode.code => 92688
5
+ # zipcode.plus_four => 1111
6
+ require 'rubygems'
7
+ require 'active_support'
8
+
9
+ class Zipcode < Object
10
+ attr_accessor :code, :plus_four
11
+
12
+ def initialize(zipcode)
13
+ format_zipcode(zipcode)
14
+ end
15
+
16
+ private
17
+
18
+ def format_zipcode(zipcode)
19
+ @code = sanitize_zipcode(zipcode)
20
+ if zipcode =~ /^\d{5}(\-?)(\d{4})?$/
21
+ @plus_four = @code.slice(5,4).blank? ? nil : @code.slice(5,4)
22
+ @code = @code.first(5)
23
+ else
24
+ @plus_four = nil
25
+ end
26
+ end
27
+
28
+ # Removes anything other than Alpha-Numerics
29
+ def sanitize_zipcode(zipcode)
30
+ zipcode.gsub(/([^0-9A-Za-z]| \-)/,'')
31
+ end
32
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require '../lib/zipcode'
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ class ZipcodeTest < Test::Unit::TestCase
4
+ def test_zipcode_code_with_plus_four
5
+ zipcode = Zipcode.new("92688-1001")
6
+ assert_equal("92688",zipcode.code)
7
+ end
8
+
9
+ def test_zipcode_plus_four_with_plus_four
10
+ zipcode = Zipcode.new("10011-1001")
11
+ assert_equal("1001",zipcode.plus_four)
12
+ end
13
+
14
+ def test_zipcode_code_without_plus_four
15
+ zipcode = Zipcode.new("XCR MNT")
16
+ assert_equal("XCRMNT",zipcode.code)
17
+ end
18
+
19
+ def test_zipcode_plus_four_without_plus_four
20
+ zipcode = Zipcode.new("KGV RDE")
21
+ assert_nil zipcode.plus_four
22
+ end
23
+
24
+ end
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{zipcode}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Tim Matheson"]
9
+ s.date = %q{2009-11-11}
10
+ s.description = %q{Creates a Zipcode object class used for validating zipcodes.}
11
+ s.email = %q{me@timmatheson.com}
12
+ s.extra_rdoc_files = ["lib/zipcode.rb", "README.rdoc"]
13
+ s.files = ["init.rb", "lib/zipcode.rb", "Manifest", "Rakefile", "README.rdoc", "test/test_helper.rb", "test/zipcode_test.rb", "zipcode.gemspec"]
14
+ s.homepage = %q{}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Zipcode", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{zipcode}
18
+ s.rubygems_version = %q{1.3.5}
19
+ s.summary = %q{Creates a Zipcode object class used for validating zipcodes.}
20
+ s.test_files = ["test/test_helper.rb", "test/zipcode_test.rb"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zipcode
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tim Matheson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-11 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Creates a Zipcode object class used for validating zipcodes.
17
+ email: me@timmatheson.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - lib/zipcode.rb
24
+ - README.rdoc
25
+ files:
26
+ - init.rb
27
+ - lib/zipcode.rb
28
+ - Manifest
29
+ - Rakefile
30
+ - README.rdoc
31
+ - test/test_helper.rb
32
+ - test/zipcode_test.rb
33
+ - zipcode.gemspec
34
+ has_rdoc: true
35
+ homepage: ""
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --line-numbers
41
+ - --inline-source
42
+ - --title
43
+ - Zipcode
44
+ - --main
45
+ - README.rdoc
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "1.2"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project: zipcode
63
+ rubygems_version: 1.3.5
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Creates a Zipcode object class used for validating zipcodes.
67
+ test_files:
68
+ - test/test_helper.rb
69
+ - test/zipcode_test.rb