geo2tz 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in geo2tz.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Christophe Verbinnen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # Geo2tz
2
+
3
+ This gem enables you to find a timezone given a set of latitude and longitude.
4
+
5
+ In order to do that It parses a list of all the cities from geonames.org and setup a Kdtree to find to closest city to your given coordinates and returns the associated timezone.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'geo2tz'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install geo2tz
20
+
21
+ ## Usage
22
+
23
+ In order to work this gem needs to download, parse and store a file from geonames.org. It therefore needs to store that file somewhere. you need to specify a writable directory where It can write to.
24
+
25
+ # config/initializers/geo2tz.rb
26
+ Geo2tz.configure(
27
+ :writable_directory => '/myproject/files,
28
+ :filename => 'geo2tz.db' # Optional,
29
+ :geoname_url => 'http://download.geonames.org/export/dump/cities1000.zip',
30
+ :geoname_filename => 'cities1000.txt'
31
+ )
32
+
33
+ To setup your cities database file.
34
+
35
+ Geo2tz::Updater.new
36
+
37
+ To search use the lat_long method
38
+
39
+ Geo2tz::Search.lat_long(-18,31)
40
+ => (GMT+02:00) Africa/Harare
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/geo2tz.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 'geo2tz/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "geo2tz"
8
+ spec.version = Geo2tz::VERSION
9
+ spec.authors = ["Christophe Verbinnen"]
10
+ spec.email = ["djpate@gmail.com"]
11
+ spec.description = "Get a ActiveSupport/Timezone from a given lat long"
12
+ spec.summary = "In order to do that It parses a list of all the cities from geonames.org and setup a Kdtree to find to closest city to your given coordinates and returns the associated timezone."
13
+ spec.homepage = "https://github.com/djpate/geo2tz"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_runtime_dependency "kdtree"
25
+ spec.add_runtime_dependency "active_support"
26
+ spec.add_runtime_dependency "zip"
27
+ end
@@ -0,0 +1,51 @@
1
+ require 'singleton'
2
+ module Geo2tz
3
+
4
+ def self.configure(options)
5
+ Configuration.instance.configure(options)
6
+ end
7
+
8
+ def self.config
9
+ Configuration.instance.data
10
+ end
11
+
12
+ class Configuration
13
+
14
+ include Singleton
15
+
16
+ attr_accessor :data
17
+
18
+ OPTIONS = [
19
+ :writable_directory,
20
+ :filename,
21
+ :geoname_url,
22
+ :geoname_filename
23
+ ]
24
+
25
+ def initialize
26
+ @data = {}
27
+ set_defaults
28
+ end
29
+
30
+ def configure(options)
31
+ @data.merge!(options)
32
+ end
33
+
34
+ def set_defaults
35
+ @data[:filename] = "geo2tz.db"
36
+ @data[:writable_directory] = "/tmp"
37
+ @data[:geoname_url] = "http://download.geonames.org/export/dump/cities1000.zip"
38
+ @data[:geoname_filename] = "cities1000.txt"
39
+ end
40
+
41
+ OPTIONS.each do |o|
42
+ define_method o do
43
+ @data[o]
44
+ end
45
+ define_method "#{o}=" do |value|
46
+ @data[o] = value
47
+ end
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,11 @@
1
+ require 'geo2tz'
2
+ require 'rails'
3
+ module Geo2tz
4
+ class Railtie < Rails::Railtie
5
+ railtie_name :geo2tz
6
+
7
+ rake_tasks do
8
+ load "tasks/geo2tz.rake"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,33 @@
1
+ require 'kdtree'
2
+ require 'active_support/time_with_zone'
3
+ module Geo2tz
4
+ class Search
5
+
6
+ MAP = {}
7
+
8
+ def self.lat_long(lat, long)
9
+ tz_integer = self.kdtree.nearest(lat, long)
10
+ ActiveSupport::TimeZone.new(MAP.invert[tz_integer])
11
+ end
12
+
13
+ private
14
+
15
+ def self.setup_tree
16
+ points = []
17
+ File.open("#{Geo2tz.config[:writable_directory]}/#{Geo2tz.config[:filename]}").each do |line|
18
+ data = line.split(',')
19
+ tz = data[2].strip
20
+ unless MAP.has_key? tz
21
+ MAP[tz] = MAP.size
22
+ end
23
+ points << [data[0].to_f, data[1].to_f, MAP[tz]]
24
+ end
25
+ Kdtree.new(points)
26
+ end
27
+
28
+ def self.kdtree
29
+ @kd ||= setup_tree
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,45 @@
1
+ require 'open-uri'
2
+ require 'zip/zip'
3
+ module Geo2tz
4
+ class Updater
5
+
6
+ def initialize
7
+ unzip_db
8
+ parse_cities
9
+ write_file
10
+ end
11
+
12
+ def unzip_db
13
+ puts "Downloading #{Geo2tz.config[:geoname_url]}."
14
+ open(Geo2tz.config[:geoname_url]) do |zf|
15
+ puts "Done."
16
+ zipfile = Zip::ZipFile.open(zf.path)
17
+ if zipfile.find_entry(Geo2tz.config[:geoname_filename])
18
+ @unparsed_cities = zipfile.read(Geo2tz.config[:geoname_filename])
19
+ else
20
+ raise "Zip file does not contain expected file"
21
+ end
22
+ end
23
+ true
24
+ end
25
+
26
+ def parse_cities
27
+ @parsed_cities = []
28
+ @unparsed_cities.split("\n").each do |line|
29
+ line_data = line.split("\t")
30
+ @parsed_cities << {:lat => line_data[4], :long => line_data[5], :tz => line_data[17]}
31
+ end
32
+ true
33
+ end
34
+
35
+ def write_file
36
+ File.open("#{Geo2tz.config[:writable_directory]}/#{Geo2tz.config[:filename]}", 'w') do |file|
37
+ @parsed_cities.each do |city|
38
+ file.write("#{city[:lat]},#{city[:long]},#{city[:tz]}\n")
39
+ end
40
+ end
41
+ true
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module Geo2tz
2
+ VERSION = "0.0.1"
3
+ end
data/lib/geo2tz.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "geo2tz/version"
2
+ require "geo2tz/configuration"
3
+ require 'geo2tz/data_fetcher'
4
+ require 'geo2tz/search'
5
+ require 'geo2tz/railtie' if defined?(Rails)
6
+ module Geo2tz
7
+
8
+ end
data/tasks/geo2tz.rake ADDED
@@ -0,0 +1,4 @@
1
+ desc 'Geo2tz data fetching'
2
+ task :fetch_geodata do
3
+ Geo2tz::Updater.new
4
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geo2tz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Christophe Verbinnen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: kdtree
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: active_support
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: zip
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Get a ActiveSupport/Timezone from a given lat long
95
+ email:
96
+ - djpate@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - geo2tz.gemspec
107
+ - lib/geo2tz.rb
108
+ - lib/geo2tz/configuration.rb
109
+ - lib/geo2tz/railtie.rb
110
+ - lib/geo2tz/search.rb
111
+ - lib/geo2tz/updater.rb
112
+ - lib/geo2tz/version.rb
113
+ - tasks/geo2tz.rake
114
+ homepage: https://github.com/djpate/geo2tz
115
+ licenses:
116
+ - MIT
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ! '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 1.8.23
136
+ signing_key:
137
+ specification_version: 3
138
+ summary: In order to do that It parses a list of all the cities from geonames.org
139
+ and setup a Kdtree to find to closest city to your given coordinates and returns
140
+ the associated timezone.
141
+ test_files: []