google_timezone 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
18
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in google_timezone.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 sck-v
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 ADDED
@@ -0,0 +1 @@
1
+ This file was created by JetBrains RubyMine 5.0 for binding GitHub repository
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # GoogleTimezone
2
+
3
+ Small gem to get timezone info by known coordinates using google timezone api.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'google_timezone'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install google_timezone
18
+
19
+ ## Usage
20
+
21
+ It uses latitude and longitude to retrieve timezone info.
22
+
23
+ GoogleTimezone.fetch(50.1196004, 8.679918299999999)
24
+
25
+ It will get `GoogleTimezone::Result` object which maps major google api responce items named in snake case.
26
+ More information [here](https://developers.google.com/maps/documentation/timezone/)
27
+ Also there is `GoogleTimezone::Result#success?` method. It returns true if responce was successful.
28
+
29
+ The bang version `fetch!` raises an error if google responce is not ok.
30
+
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'google_timezone/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "google_timezone"
8
+ gem.version = GoogleTimezone::VERSION
9
+ gem.authors = ["sck-v"]
10
+ gem.email = ["kryak.iv@gmail.com"]
11
+ gem.description = %q{Get timezone info by known coordinates}
12
+ gem.summary = %q{Small gem to get timezone info by known coordinates using google timezone api.
13
+ https://developers.google.com/maps/documentation/timezone/}
14
+ gem.homepage = ""
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{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_development_dependency 'rspec-mocks'
23
+ end
@@ -0,0 +1,51 @@
1
+ require 'json'
2
+ require 'open-uri'
3
+
4
+ module GoogleTimezone
5
+ class Base
6
+ @allowed_params = [:language, :sensor, :timestamp, :client, :signature]
7
+
8
+ def initialize(*args)
9
+ @lat, @lon = if args.first.is_a? Array
10
+ args.first
11
+ else
12
+ args[0..1]
13
+ end
14
+ @options = extract_options!(args)
15
+ @options.reject! { |key, value| !@allowed_params.include? key }
16
+ end
17
+
18
+ def fetch
19
+ location = [@lat, @lon].join(',')
20
+ params = { location: location, sensor: false, timestamp: Time.now.to_i }.merge(@options)
21
+ result = get_result(params)
22
+ Result.new(result)
23
+ end
24
+
25
+ def fetch!
26
+ result = fetch
27
+ raise_error(result.result) unless result.success?
28
+ result
29
+ end
30
+
31
+ private
32
+ def hash_to_query(hash)
33
+ require 'cgi' unless defined?(CGI) && defined?(CGI.escape)
34
+ hash.collect{ |p|
35
+ p[1].nil? ? nil : p.map{ |i| CGI.escape i.to_s } * '='
36
+ }.compact.sort * '&'
37
+ end
38
+
39
+ def url(params)
40
+ "https://maps.googleapis.com/maps/api/timezone/json?#{hash_to_query(params)}"
41
+ end
42
+
43
+ def extract_options!(*args)
44
+ args.last.is_a?(::Hash) ? pop : {}
45
+ end
46
+
47
+ def get_result(params)
48
+ open(url(params)) { |r| JSON.parse(r.read) }
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,36 @@
1
+
2
+ module GoogleTimezone
3
+ class Result
4
+ def initialize(result)
5
+ @result = result
6
+ end
7
+
8
+ def raw
9
+ @result
10
+ end
11
+
12
+ def success?
13
+ @result['status'].eql?('OK')
14
+ end
15
+
16
+ def dst_offset
17
+ @result.fetch('dstOffset',0)
18
+ end
19
+
20
+ def raw_offset
21
+ @result.fetch('rawOffset',0)
22
+ end
23
+
24
+ def time_zone_id
25
+ @result.fetch('timeZoneId','')
26
+ end
27
+
28
+ def time_zone_name
29
+ @result.fetch('timeZoneName','')
30
+ end
31
+
32
+ def result
33
+ @result.fetch('result','')
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module GoogleTimezone
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,15 @@
1
+ require 'google_timezone/version'
2
+ require 'google_timezone/base'
3
+ require 'google_timezone/result'
4
+
5
+ module GoogleTimezone
6
+ def fetch(*args)
7
+ Base.new(args).fetch
8
+ end
9
+
10
+ def fetch!(*args)
11
+ Base.new(args).fetch!
12
+ end
13
+
14
+ module_function :fetch, :fetch!
15
+ end
@@ -0,0 +1,13 @@
1
+ require 'google_timezone/result'
2
+
3
+ describe GoogleTimezone::Result do
4
+ it 'should be success' do
5
+ result = GoogleTimezone::Result.new({ 'status' => 'OK' })
6
+ result.should be_success
7
+ end
8
+
9
+ it 'should not be succsess' do
10
+ result = GoogleTimezone::Result.new({ 'status' => 'ZERO_RESULTS' })
11
+ result.should_not be_success
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ require 'google_timezone'
2
+
3
+ describe GoogleTimezone do
4
+ describe 'initialize' do
5
+ it 'correct with separate lat and lon' do
6
+ g = GoogleTimezone::Base.new(0,0)
7
+ g.should be_an_instance_of(GoogleTimezone::Base)
8
+ end
9
+
10
+ it 'correct with array lat and lon' do
11
+ g = GoogleTimezone::Base.new([0,0])
12
+ g.should be_an_instance_of(GoogleTimezone::Base)
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_timezone
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - sck-v
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
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: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec-mocks
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
+ description: Get timezone info by known coordinates
47
+ email:
48
+ - kryak.iv@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README
57
+ - README.md
58
+ - Rakefile
59
+ - google_timezone.gemspec
60
+ - lib/google_timezone.rb
61
+ - lib/google_timezone/base.rb
62
+ - lib/google_timezone/result.rb
63
+ - lib/google_timezone/version.rb
64
+ - spec/google_timezone_result_spec.rb
65
+ - spec/google_timezone_spec.rb
66
+ homepage: ''
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.24
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Small gem to get timezone info by known coordinates using google timezone
90
+ api. https://developers.google.com/maps/documentation/timezone/
91
+ test_files:
92
+ - spec/google_timezone_result_spec.rb
93
+ - spec/google_timezone_spec.rb