geo_locator 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dfd06ba8440f46c6dbbd48128b012121bdd1e517
4
- data.tar.gz: 980b5ed4d134c8cf3fc4582346a49c217e3d362c
3
+ metadata.gz: 0a7d11b260f776e20225709d37d0d9084ea1fe75
4
+ data.tar.gz: f07c259078e44c84cb8e9c624ffd858ba8110bad
5
5
  SHA512:
6
- metadata.gz: 7193cd9ed44f91478093be647a05bae95a7b0f9a7492cbd15d07a519803e371055295a900728afc3e8cb643bc3f772aa923a17d5105308105f6b37b97b6f4bb8
7
- data.tar.gz: 9147ce9a35b32d3ebe1c5ee80e6dbfe34ea6678ef3891c4ccc63c58461ad9343371b430078460fb37e5c26cd61623cfb492c87d73c40704a13e2c5f5f5604b27
6
+ metadata.gz: 93e61b7523ca92577c4410e60d73b3dabe830c8e981248e2f95b20d09f17b45c4bc25a347aa6ada2b4cddff9c89c31d0dfb7df17feacd876a90b269f34d23784
7
+ data.tar.gz: 6edc764a8dadc54bad9968a80c31dc83b5bf328ac8f7696f8cfda8ca685cb3b6f4f4e5d4c50662a777a8baf7fd782dfe36e1bb4879657003d2b34ffa8610beb5
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Rakefile CHANGED
@@ -1 +1,38 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+ task :release => :check_database # ensure that there is a database before releasing
8
+
9
+ task :check_database do
10
+ raise "Database does not exist!" unless File.exist?(File.expand_path("geo_locator.sqlite3", File.dirname(__FILE__)))
11
+ end
12
+
13
+ task :create_database do
14
+ require "sqlite3"
15
+
16
+ database_name = "geo_locator.sqlite3"
17
+ plz_tab_url = "http://fa-technik.adfc.de/code/opengeodb/PLZ.tab"
18
+ raw_plz_tab = Net::HTTP.get(URI(plz_tab_url))
19
+ plz_tab = raw_plz_tab
20
+ .split("\n")
21
+ .delete_if { |line| line[0] == "#" }
22
+ .map! { |line| line.split("\t") }
23
+
24
+ unless File.exist?(File.expand_path(database_name, File.dirname(__FILE__)))
25
+ db = SQLite3::Database.new(database_name)
26
+ db.execute("CREATE TABLE IF NOT EXISTS locations(loc_id INTEGER PRIMARY KEY, zip_code INTEGER, lon REAL, lat REAL, location TEXT)")
27
+ db.execute("CREATE UNIQUE INDEX 'loc_id_UNIQUE' ON 'locations' ('loc_id')")
28
+ db.execute("CREATE INDEX 'location' ON 'locations' ('location')")
29
+ db.execute("CREATE INDEX 'zip_code' ON 'locations' ('zip_code')")
30
+
31
+ plz_tab.each do |line|
32
+ db.execute("INSERT INTO locations VALUES(#{line[0]}, #{line[1]}, #{line[2]}, #{line[3]}, '#{line[4]}')")
33
+ end
34
+ else
35
+ puts "Database already exists!"
36
+ end
37
+ end
38
+ task :create_db => :create_database # alias for the command line
data/geo_locator.gemspec CHANGED
@@ -17,6 +17,8 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
+ spec.add_dependency "sqlite3", "~> 1.3.9"
21
+
20
22
  spec.add_development_dependency "bundler", "~> 1.5"
21
23
  spec.add_development_dependency "pry", "~> 0.9.12.4"
22
24
  spec.add_development_dependency "pry-nav", "~> 0.2.3"
Binary file
@@ -1,3 +1,3 @@
1
- module GeoLocator
2
- VERSION = "0.0.1"
1
+ class GeoLocator
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/geo_locator.rb CHANGED
@@ -1,5 +1,52 @@
1
- require "geo_locator/version"
1
+ require "sqlite3"
2
+
3
+ class GeoLocator
4
+ require "geo_locator/version"
5
+
6
+ class << self
7
+ private
8
+ def _locate(options = {})
9
+ unless db = (options[:db] || options["db"])
10
+ raise ArgumentError, "Options do not contain a db connection!"
11
+ end
12
+
13
+ location = options[:location] || options["location"]
14
+ zip_code = options[:zip_code] || options["zip_code"]
15
+
16
+ unless location || zip_code
17
+ raise ArgumentError, "Options do not contains neither location nor zip_code!"
18
+ end
19
+
20
+ options[:format] = :hash unless options[:format] || options["format"]
21
+
22
+ db_result = db.execute("SELECT lat, lon FROM locations WHERE #{location ? 'location LIKE \'' + location + '\'' : ''} #{location && zip_code ? 'AND' : ''} #{zip_code ? 'zip_code = \'' + zip_code.to_s + '\'' : ''}")
23
+
24
+ db_result.map! do |row|
25
+ lat = row[0]
26
+ lon = row[1]
27
+
28
+ case options[:format]
29
+ when :array then [lon, lat] # order conforms to GeoJSON and is supported as such be elasticsearch
30
+ when :hash then { lat: lat, lon: lon }
31
+ when :string then "#{lat},#{lon}"
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ def self.locate(options = {})
38
+ end
39
+
40
+ def initialize(options = {})
41
+ @db = SQLite3::Database.open(File.expand_path("../geo_locator.sqlite3", File.dirname(__FILE__)))
42
+ @format = options[:format] || options["format"] || :hash
43
+ end
44
+
45
+ def locate(options = {})
46
+ options[:db] = @db
47
+ options[:format] = options[:format] || options["format"] || @format
48
+
49
+ self.class.send(:_locate, options)
50
+ end
2
51
 
3
- module GeoLocator
4
- # Your code goes here...
5
52
  end
@@ -0,0 +1,57 @@
1
+ require_relative "./spec_helper"
2
+
3
+ describe GeoLocator do
4
+ let(:database_path) { File.expand_path("./geo_locator_test.sqlite3", File.dirname(__FILE__)) }
5
+
6
+ it "is a class" do
7
+ expect(GeoLocator.class).to be(Class)
8
+ end
9
+
10
+ describe ".locate" do
11
+ it "is a class level proxy for the class method _locate without the need for instantiation" do
12
+ expect(GeoLocator).to respond_to(:locate)
13
+ end
14
+ end
15
+
16
+ describe ".new" do
17
+ let(:geo_locator) { GeoLocator.new }
18
+
19
+ it "creates an instance of GeoLocator to memoize the internal database connection" do
20
+ expect(geo_locator).to be_a(GeoLocator)
21
+ end
22
+
23
+ describe "#locate" do
24
+ it "is an instance level proxy for the class method _locate" do
25
+ expect(geo_locator).to respond_to(:locate)
26
+ end
27
+ end
28
+ end
29
+
30
+ #
31
+ # private methods used internally
32
+ #
33
+ describe "._locate" do
34
+ let(:db) { SQLite3::Database.open(database_path) }
35
+
36
+ it "is a private class method used internally by the various locate methods" do
37
+ expect(GeoLocator.private_methods.include?(:_locate)).to eq(true)
38
+ expect { GeoLocator._locate }.to raise_error(NoMethodError)
39
+ end
40
+
41
+ it "expects a sqlite3 database named geo_locator.sqlite3 in the gems root dir" do
42
+ #true # expect(File.exist?(File.expand_path("../geo_locator.sqlite3", File.dirname(__FILE__)))).to eq(true)
43
+ end
44
+
45
+ it "needs to be called with a database connection" do
46
+ expect { GeoLocator.send(:_locate) }.to raise_error(ArgumentError)
47
+ end
48
+
49
+ it "returns a array of geo locations if only db and zip_code is given" do
50
+ expect(GeoLocator.send(:_locate, db: db, zip_code: 33100)).to eq([{:lat=>51.7240488542403, :lon=>8.82975311690181}])
51
+ end
52
+
53
+ it "returns a array of geo locations if only db and location is given" do
54
+ expect(GeoLocator.send(:_locate, db: db, location: "Schwerin")).to eq([{:lat=>53.6247393347882, :lon=>11.4081221498717}, {:lat=>53.6577581258256, :lon=>11.4356781728688}, {:lat=>53.6517809563862, :lon=>11.3497453888959}, {:lat=>53.6360865636739, :lon=>11.3920850320353}, {:lat=>53.6021672992816, :lon=>11.4110439798182}, {:lat=>53.5808291697605, :lon=>11.4498483787143}])
55
+ end
56
+ end
57
+ end
Binary file
@@ -0,0 +1,15 @@
1
+ require "geo_locator"
2
+ require "pry"
3
+
4
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+
10
+ # Run specs in random order to surface order dependencies. If you find an
11
+ # order dependency and want to debug it, you can fix the order by providing
12
+ # the seed, which is printed after each run.
13
+ # --seed 1234
14
+ config.order = "random"
15
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geo_locator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Sievers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-15 00:00:00.000000000 Z
11
+ date: 2014-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sqlite3
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.3.9
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.3.9
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -116,13 +130,18 @@ extensions: []
116
130
  extra_rdoc_files: []
117
131
  files:
118
132
  - ".gitignore"
133
+ - ".rspec"
119
134
  - Gemfile
120
135
  - LICENSE.txt
121
136
  - README.md
122
137
  - Rakefile
123
138
  - geo_locator.gemspec
139
+ - geo_locator.sqlite3
124
140
  - lib/geo_locator.rb
125
141
  - lib/geo_locator/version.rb
142
+ - spec/geo_locator_spec.rb
143
+ - spec/geo_locator_test.sqlite3
144
+ - spec/spec_helper.rb
126
145
  homepage: https://github.com/msievers/geo_locator
127
146
  licenses:
128
147
  - MIT
@@ -147,4 +166,7 @@ rubygems_version: 2.2.0
147
166
  signing_key:
148
167
  specification_version: 4
149
168
  summary: A ruby gem for translating geo informations (e.g. zip code) to geo locations.
150
- test_files: []
169
+ test_files:
170
+ - spec/geo_locator_spec.rb
171
+ - spec/geo_locator_test.sqlite3
172
+ - spec/spec_helper.rb