locus 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +22 -0
- data/README.md +52 -0
- data/Rakefile +3 -0
- data/lib/data/zipcodes.yml +13992 -0
- data/lib/locus.rb +17 -0
- data/lib/locus/place.rb +43 -0
- data/lib/locus/railtie.rb +10 -0
- data/lib/locus/tasks/geonames.rake +26 -0
- data/lib/locus/version.rb +3 -0
- data/locus.gemspec +27 -0
- data/spec/place_spec.rb +36 -0
- data/spec/spec_helper.rb +29 -0
- metadata +136 -0
data/lib/locus.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'yaml'
|
3
|
+
require 'active_support/core_ext/module/attribute_accessors.rb'
|
4
|
+
|
5
|
+
require 'locus/version'
|
6
|
+
require 'locus/railtie' if defined?(Rails)
|
7
|
+
|
8
|
+
require 'locus/place'
|
9
|
+
|
10
|
+
# Locus.
|
11
|
+
module Locus
|
12
|
+
mattr_accessor :zip_path, :default_country
|
13
|
+
|
14
|
+
self.zip_path =
|
15
|
+
File.expand_path(File.join('..', 'data', 'zipcodes.yml'), __FILE__)
|
16
|
+
self.default_country = :de
|
17
|
+
end
|
data/lib/locus/place.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module Locus
|
4
|
+
class Place
|
5
|
+
attr_accessor :country, :postal_code, :state_code
|
6
|
+
|
7
|
+
def initialize(attributes = {})
|
8
|
+
attributes.each do |key, value|
|
9
|
+
send("#{key}=", value)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# Find a place by postal code and country.
|
14
|
+
#
|
15
|
+
# @param postal_code [String] The postal code.
|
16
|
+
# @param country [Symbol] Country symbol.
|
17
|
+
#
|
18
|
+
# @example
|
19
|
+
# Locus::Place.find_by_postal_code '12053', :de # => 'BE'
|
20
|
+
#
|
21
|
+
# @return [Place] The {Place} or nil.
|
22
|
+
def self.find_by_postal_code(postal_code, country = Locus.default_country)
|
23
|
+
return nil unless state_code = state_code(postal_code, country)
|
24
|
+
self.new(country: country,
|
25
|
+
postal_code: postal_code,
|
26
|
+
state_code: state_code)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# Get the state code for a given postal code and country.
|
32
|
+
#
|
33
|
+
# @return [String] The state code or nil.
|
34
|
+
def self.state_code(postal_code, country)
|
35
|
+
return nil unless country = places[country]
|
36
|
+
country[postal_code]
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.places
|
40
|
+
@places ||= YAML.load_file Locus.zip_path
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
namespace :locus do
|
5
|
+
namespace :import do
|
6
|
+
|
7
|
+
unless Rake::Task.task_defined?('locus:import:zip')
|
8
|
+
desc 'import geonames zip code db and convert it to yaml'
|
9
|
+
task :zip, :output, :input do |task, args|
|
10
|
+
input_files = args.input.split(' ')
|
11
|
+
|
12
|
+
result = input_files.map do |file|
|
13
|
+
country = nil
|
14
|
+
codes = IO.readlines(file).map do |line|
|
15
|
+
line = line.split("\t")
|
16
|
+
country ||= line[0].downcase.to_sym
|
17
|
+
{ line[1] => line[4] }
|
18
|
+
end.reduce(:merge)
|
19
|
+
{ country => codes }
|
20
|
+
end.reduce(:merge)
|
21
|
+
|
22
|
+
IO.write(args.output, result.to_yaml)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/locus.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'locus/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'locus'
|
8
|
+
gem.version = Locus::VERSION
|
9
|
+
gem.authors = ['Gebhard Wöstemeyer']
|
10
|
+
gem.email = ['g.woestemeyer@gmail.com']
|
11
|
+
gem.description = 'The gem uses the downloadable geonames.org database for retrieving additional information about postal codes'
|
12
|
+
gem.summary = 'Information about postal codes'
|
13
|
+
gem.homepage = 'https://github.com/gewo/locus/'
|
14
|
+
gem.license = 'MIT'
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^spec/})
|
19
|
+
gem.require_paths = ['lib']
|
20
|
+
|
21
|
+
gem.add_runtime_dependency 'activesupport'
|
22
|
+
|
23
|
+
gem.add_development_dependency 'rake'
|
24
|
+
gem.add_development_dependency 'rspec'
|
25
|
+
gem.add_development_dependency 'simplecov'
|
26
|
+
gem.add_development_dependency 'coveralls'
|
27
|
+
end
|
data/spec/place_spec.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
module Locus
|
5
|
+
describe Place do
|
6
|
+
describe '#find_by_postal_code' do
|
7
|
+
it 'returns nil when postal_code does not exist' do
|
8
|
+
Place.find_by_postal_code('12345', :de).should be_nil
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'returns nil when given an invalid country' do
|
12
|
+
Place.find_by_postal_code('12345', :wunderland).should be_nil
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'given valid input' do
|
16
|
+
subject { Place.find_by_postal_code('12053', :de) }
|
17
|
+
it { should be_a Place }
|
18
|
+
its(:country) { should == :de }
|
19
|
+
its(:postal_code) { should == '12053' }
|
20
|
+
its(:state_code) { should == 'BE' }
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'default_country' do
|
24
|
+
subject { Place.find_by_postal_code('12053') }
|
25
|
+
its(:country) { should == :de }
|
26
|
+
|
27
|
+
context 'when changed' do
|
28
|
+
before { Locus.default_country = :ch }
|
29
|
+
after { Locus.default_country = :de }
|
30
|
+
it { should be_nil }
|
31
|
+
it { Place.find_by_postal_code('4243').state_code.should == 'BL' }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'simplecov'
|
4
|
+
require 'coveralls'
|
5
|
+
|
6
|
+
# puts SimpleCov.formatter.inspect
|
7
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
8
|
+
SimpleCov::Formatter::HTMLFormatter,
|
9
|
+
Coveralls::SimpleCov::Formatter
|
10
|
+
]
|
11
|
+
|
12
|
+
SimpleCov.at_exit do
|
13
|
+
File.open(File.join(SimpleCov.coverage_path, 'percent.txt'), 'w') do |f|
|
14
|
+
f.write SimpleCov.result.covered_percent
|
15
|
+
end
|
16
|
+
SimpleCov.result.format!
|
17
|
+
end
|
18
|
+
SimpleCov.start
|
19
|
+
|
20
|
+
# Coveralls.wear!
|
21
|
+
|
22
|
+
require 'locus'
|
23
|
+
|
24
|
+
RSpec.configure do |config|
|
25
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
26
|
+
config.run_all_when_everything_filtered = true
|
27
|
+
config.filter_run :focus
|
28
|
+
config.order = 'random'
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: locus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gebhard Wöstemeyer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: coveralls
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: The gem uses the downloadable geonames.org database for retrieving additional
|
84
|
+
information about postal codes
|
85
|
+
email:
|
86
|
+
- g.woestemeyer@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- .coveralls.yml
|
92
|
+
- .gitignore
|
93
|
+
- .rspec
|
94
|
+
- .ruby-gemset
|
95
|
+
- .ruby-version
|
96
|
+
- .travis.yml
|
97
|
+
- Gemfile
|
98
|
+
- LICENSE.txt
|
99
|
+
- README.md
|
100
|
+
- Rakefile
|
101
|
+
- lib/data/zipcodes.yml
|
102
|
+
- lib/locus.rb
|
103
|
+
- lib/locus/place.rb
|
104
|
+
- lib/locus/railtie.rb
|
105
|
+
- lib/locus/tasks/geonames.rake
|
106
|
+
- lib/locus/version.rb
|
107
|
+
- locus.gemspec
|
108
|
+
- spec/place_spec.rb
|
109
|
+
- spec/spec_helper.rb
|
110
|
+
homepage: https://github.com/gewo/locus/
|
111
|
+
licenses:
|
112
|
+
- MIT
|
113
|
+
metadata: {}
|
114
|
+
post_install_message:
|
115
|
+
rdoc_options: []
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - '>='
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - '>='
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
requirements: []
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 2.0.3
|
131
|
+
signing_key:
|
132
|
+
specification_version: 4
|
133
|
+
summary: Information about postal codes
|
134
|
+
test_files:
|
135
|
+
- spec/place_spec.rb
|
136
|
+
- spec/spec_helper.rb
|