ask_geo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'json'
4
+
5
+ group :development do
6
+ gem "rake"
7
+ gem "rspec", "~> 2.3.0"
8
+ gem "bundler", "~> 1.0.0"
9
+ gem "jeweler", "~> 1.6.4"
10
+ gem "rcov", ">= 0"
11
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Scott W. Bradley
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,93 @@
1
+ ask_geo
2
+ =======
3
+
4
+ Ruby client library for the [AskGeo](http://www.askgeo.com/) web service. From the AskGeo website:
5
+
6
+ > AskGeo provides a simple, fast, and accurate web api for finding out the time zone information for a given location (latitude and longitude). It's simple: you give latitude and longitude and the API returns a time zone ID (e.g., "America/Chicago", or "Europe/London"). We built the API because we needed it for a different project and decided to make it available because no other such tools seem to exist that are accurate and capable of returning a large number of results in a short period of time.
7
+ >
8
+ > Unlike GeoNames, look-ups are based on an actual map of the world, rather than "closest point of interest", which ensures accuracy and that you actually get a result for all locations. And unlike EarthTools, we return an actual time zone ID (e.g., "America/Los_Angeles") rather than just an offset. The API is also very fast: when used in batch mode, it is capable of returning thousands of results in a couple seconds.
9
+
10
+ Installation
11
+ ============
12
+
13
+ Install the gem:
14
+
15
+ gem install ask_geo
16
+
17
+ Or, if you are using bundler, add it to your Gemfile:
18
+
19
+ gem "ask_geo"
20
+
21
+ Usage
22
+ =====
23
+
24
+ First, you will need to create an [AskGeo](http://www.askgeo.com/) account. Currently they only support doing this via OpenID with a gmail account. Once you have created an account, they give you an Account ID and an API Key. You will need those to make calls to their web service through this library.
25
+
26
+ From there, usage of the client library is fairly straight-forward:
27
+
28
+ ```ruby
29
+ # Instantiate a client
30
+ client = AskGeo.new(:account_id => 'XXXXXX', :api_key => 'XXXXXX')
31
+
32
+ # Lookup a single lat/lon
33
+ resp = client.lookup("47.62057,-122.349761")
34
+ # => {
35
+ # "timeZone" => "America/Los_Angeles",
36
+ # "currentOffsetMs" => -25200000,
37
+ # "latitude" => 47.62057,
38
+ # "longitude" => -122.349761
39
+ # }
40
+
41
+ # Lookup a batch of lat/lon points (faster than multiple calls)
42
+ resp = client.lookup(["47.62057,-122.349761", "40.748529,-73.98563"])
43
+ # => [{
44
+ # "timeZone" => "America/Los_Angeles",
45
+ # "currentOffsetMs" => -25200000,
46
+ # "latitude" => 47.62057,
47
+ # "longitude" => -122.349761
48
+ # },
49
+ # {
50
+ # "timeZone" => "America/New_York",
51
+ # "currentOffsetMs" => -14400000,
52
+ # "latitude" => 40.748529,
53
+ # "longitude" => -73.98563
54
+ # }]
55
+
56
+ # Points can be specified by Hash too...
57
+ resp = client.lookup(:lat => 47.62057, :lon => -122.349761)
58
+
59
+ resp = client.lookup(
60
+ [
61
+ {:lat => 47.62057, :lon => -122.349761},
62
+ {:lat => 40.748529, :lon => -73.98563}
63
+ ]
64
+ )
65
+ ```
66
+
67
+ Contributing to ask_geo
68
+ =======================
69
+
70
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
71
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
72
+ * Fork the project
73
+ * Start a feature/bugfix branch
74
+ * Commit and push until you are happy with your contribution
75
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
76
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
77
+
78
+ Running the Tests
79
+ =================
80
+
81
+ In order to run the tests, you first need to edit `spec/spec_helper.rb` and follow the `TODO` comment to insert your AskGeo account ID and API key. Then, you should be able to run the tests with:
82
+
83
+ ```
84
+ rake spec
85
+ ```
86
+
87
+ Copyright
88
+ =========
89
+
90
+ Copyright (c) 2011 Scott W. Bradley. See LICENSE.txt for
91
+ further details.
92
+
93
+ AskGeo, GeoNames, and EarthTools are copyright/trademark their respective owners.
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "ask_geo"
18
+ gem.homepage = "http://github.com/scottwb/ask_geo"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Ruby client library for the AskGeo web service (http://www.askgeo.com)"}
21
+ gem.description = %Q{Ruby client library for the AskGeo web service (http://www.askgeo.com). AskGeo provides a simple, fast, and accurate web api for finding out the time zone information for a given location (latitude and longitude)."}
22
+ gem.email = "scottwb@gmail.com"
23
+ gem.authors = ["Scott W. Bradley"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "ask_geo #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,66 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{ask_geo}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Scott W. Bradley"]
12
+ s.date = %q{2011-07-19}
13
+ s.description = %q{Ruby client library for the AskGeo web service (http://www.askgeo.com). AskGeo provides a simple, fast, and accurate web api for finding out the time zone information for a given location (latitude and longitude)."}
14
+ s.email = %q{scottwb@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "ask_geo.gemspec",
28
+ "lib/ask_geo.rb",
29
+ "spec/ask_geo_spec.rb",
30
+ "spec/spec_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/scottwb/ask_geo}
33
+ s.licenses = ["MIT"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.6}
36
+ s.summary = %q{Ruby client library for the AskGeo web service (http://www.askgeo.com)"}
37
+
38
+ if s.respond_to? :specification_version then
39
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
43
+ s.add_runtime_dependency(%q<json>, [">= 0"])
44
+ s.add_development_dependency(%q<rake>, [">= 0"])
45
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
46
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
47
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
48
+ s.add_development_dependency(%q<rcov>, [">= 0"])
49
+ else
50
+ s.add_dependency(%q<json>, [">= 0"])
51
+ s.add_dependency(%q<rake>, [">= 0"])
52
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
53
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
54
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
55
+ s.add_dependency(%q<rcov>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<json>, [">= 0"])
59
+ s.add_dependency(%q<rake>, [">= 0"])
60
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
61
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
62
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
63
+ s.add_dependency(%q<rcov>, [">= 0"])
64
+ end
65
+ end
66
+
@@ -0,0 +1,49 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'cgi'
4
+ require 'json'
5
+
6
+ class AskGeo
7
+ class APIError < StandardError; end
8
+
9
+ attr_accessor :account_id, :api_key
10
+
11
+ def initialize(opts = {})
12
+ @account_id = opts[:account_id]
13
+ @api_key = opts[:api_key]
14
+ end
15
+
16
+ def base_url
17
+ "http://www.askgeo.com/api"
18
+ end
19
+
20
+ def serialize_point(point)
21
+ case point
22
+ when Hash
23
+ "#{point[:lat]},#{point[:lon]}"
24
+ else
25
+ # Assume this is something whose #to_s makes a "lat,lon" string.
26
+ point.to_s.gsub(/\s+/, '')
27
+ end
28
+ end
29
+
30
+ def serialize_points(points)
31
+ points = [points] if !points.kind_of?(Array)
32
+ points.map{|p| serialize_point(p)}.join(';')
33
+ end
34
+
35
+ def format_url(points)
36
+ "#{base_url}/#{account_id}/#{api_key}/timezone.json?points=#{CGI::escape(serialize_points(points))}"
37
+ end
38
+
39
+ def lookup(points)
40
+ response = JSON.parse(Net::HTTP.get URI.parse(format_url(points)))
41
+ if response['code'] != 0
42
+ raise APIError.new(response['message'] || 'Unknown server error')
43
+ end
44
+ data = response['data']
45
+ data.size == 1 ? data.first : data
46
+ rescue JSON::ParserError
47
+ raise APIError.new("Invalid server response")
48
+ end
49
+ end
@@ -0,0 +1,64 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "AskGeo" do
4
+ describe "#lookup" do
5
+ before :each do
6
+ @client = AskGeo.new(
7
+ :account_id => ASKGEO_ACCOUNT_ID,
8
+ :api_key => ASKGEO_API_KEY
9
+ )
10
+ end
11
+
12
+ it "should support points specified by comma-separated lat,lon string" do
13
+ @client.lookup("47.62057,-122.349761")['timeZone'].should == 'America/Los_Angeles'
14
+ end
15
+
16
+ it "should support points specified by {:lat,:lon} hash" do
17
+ @client.lookup(:lat => 47.62057, :lon => -122.349761)['timeZone'].should == 'America/Los_Angeles'
18
+ end
19
+
20
+ it "should get a single response for a single point" do
21
+ response = @client.lookup("47.62057,-122.349761")
22
+ response.should be_a Hash
23
+ response['timeZone'].should == 'America/Los_Angeles'
24
+ response['latitude'].should be_within(0.000001).of(47.62057)
25
+ response['longitude'].should be_within(0.0000001).of(-122.349761)
26
+ (response['currentOffsetMs'] % 3600000).should == 0
27
+ (-8..-7).should include(response['currentOffsetMs'] / 3600000)
28
+ end
29
+
30
+ it "should get multiple responses for multiple points" do
31
+ needle = {
32
+ :lat => 47.62057,
33
+ :lon => -122.349761
34
+ }
35
+
36
+ empire = {
37
+ :lat => 40.748529,
38
+ :lon => -73.98563
39
+ }
40
+
41
+ response = @client.lookup([needle, empire])
42
+ response.should be_an Array
43
+ response.should have(2).responses
44
+
45
+ needle_response = response.first
46
+ needle_response['timeZone'].should == 'America/Los_Angeles'
47
+ needle_response['latitude'].should be_within(0.000001).of(needle[:lat])
48
+ needle_response['longitude'].should be_within(0.0000001).of(needle[:lon])
49
+ (needle_response['currentOffsetMs'] % 3600000).should == 0
50
+ (-8..-7).should include(needle_response['currentOffsetMs'] / 3600000)
51
+
52
+ empire_response = response.last
53
+ empire_response['timeZone'].should == 'America/New_York'
54
+ empire_response['latitude'].should be_within(0.000001).of(empire[:lat])
55
+ empire_response['longitude'].should be_within(0.0000001).of(empire[:lon])
56
+ (empire_response['currentOffsetMs'] % 3600000).should == 0
57
+ (-5..-4).should include(empire_response['currentOffsetMs'] / 3600000)
58
+ end
59
+
60
+ it "should raise AskGeo::APIError on malformed points" do
61
+ expect{@client.lookup("47.62057")}.to raise_exception AskGeo::APIError
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'ask_geo'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ #
11
+ # TODO: Update these with the values provided by askgeo.com for your account.
12
+ #
13
+ ASKGEO_ACCOUNT_ID = '282009'
14
+ ASKGEO_API_KEY = 'fbic289vke82uhi8n8qarr2dpf'
15
+
16
+ RSpec.configure do |config|
17
+
18
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ask_geo
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Scott W. Bradley
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-07-19 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ requirement: &id001 !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ segments:
26
+ - 0
27
+ version: "0"
28
+ prerelease: false
29
+ type: :runtime
30
+ name: json
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ requirement: &id002 !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ segments:
38
+ - 0
39
+ version: "0"
40
+ prerelease: false
41
+ type: :development
42
+ name: rake
43
+ version_requirements: *id002
44
+ - !ruby/object:Gem::Dependency
45
+ requirement: &id003 !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ~>
48
+ - !ruby/object:Gem::Version
49
+ segments:
50
+ - 2
51
+ - 3
52
+ - 0
53
+ version: 2.3.0
54
+ prerelease: false
55
+ type: :development
56
+ name: rspec
57
+ version_requirements: *id003
58
+ - !ruby/object:Gem::Dependency
59
+ requirement: &id004 !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ~>
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 1
65
+ - 0
66
+ - 0
67
+ version: 1.0.0
68
+ prerelease: false
69
+ type: :development
70
+ name: bundler
71
+ version_requirements: *id004
72
+ - !ruby/object:Gem::Dependency
73
+ requirement: &id005 !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ segments:
78
+ - 1
79
+ - 6
80
+ - 4
81
+ version: 1.6.4
82
+ prerelease: false
83
+ type: :development
84
+ name: jeweler
85
+ version_requirements: *id005
86
+ - !ruby/object:Gem::Dependency
87
+ requirement: &id006 !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ prerelease: false
95
+ type: :development
96
+ name: rcov
97
+ version_requirements: *id006
98
+ description: Ruby client library for the AskGeo web service (http://www.askgeo.com). AskGeo provides a simple, fast, and accurate web api for finding out the time zone information for a given location (latitude and longitude)."
99
+ email: scottwb@gmail.com
100
+ executables: []
101
+
102
+ extensions: []
103
+
104
+ extra_rdoc_files:
105
+ - LICENSE.txt
106
+ - README.md
107
+ files:
108
+ - .document
109
+ - .rspec
110
+ - Gemfile
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - VERSION
115
+ - ask_geo.gemspec
116
+ - lib/ask_geo.rb
117
+ - spec/ask_geo_spec.rb
118
+ - spec/spec_helper.rb
119
+ has_rdoc: true
120
+ homepage: http://github.com/scottwb/ask_geo
121
+ licenses:
122
+ - MIT
123
+ post_install_message:
124
+ rdoc_options: []
125
+
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ segments:
133
+ - 0
134
+ version: "0"
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ segments:
140
+ - 0
141
+ version: "0"
142
+ requirements: []
143
+
144
+ rubyforge_project:
145
+ rubygems_version: 1.3.6
146
+ signing_key:
147
+ specification_version: 3
148
+ summary: Ruby client library for the AskGeo web service (http://www.askgeo.com)"
149
+ test_files: []
150
+