which_time 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2f00512fbef059ff379c8b1dc92224b0656a727c
4
+ data.tar.gz: 9a50402cb2c8f2b54fff344c43531b2426dd27e8
5
+ SHA512:
6
+ metadata.gz: 3ceb6ad9556aca9df7719d68fe6fd7dbcba2aeaaf73db07acaa0e0f7a759742b88ce2541222ae8cf830dd93d72d8bd126a8a8bb4cb738d01f0e662d5cb883ad7
7
+ data.tar.gz: ef53db6dc43da37e49d41e81dce3429d583b5eec6b016c24c933996b51420bd8b44a99d26e4a3babdd28cac7f1bf821ef20136da653c19615721daebfd27ebed
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in which_time.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 bmalets
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,72 @@
1
+ # WhichTime
2
+
3
+ Gem which gives you a simple way to find local time (timezone) of some place/address/city/country.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'which_time'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install which_time
18
+
19
+ ## Usage
20
+
21
+ ### 1. Get local time of some address
22
+ ```
23
+ WhichTime.in( somewhere, google_api_key, your_time )
24
+ ```
25
+ - "somewhere" - may be an address or some place ("Lviv city, Naykova str." or "'Naturlih' pub, Kyiv" e.g.)
26
+ - "google_api_key" - API_KEY of Google application with an access to TimeZone and Geocoding API
27
+ - "your_time" - is not mandatory (Time.now is default)
28
+
29
+ examples:
30
+ ```
31
+ WhichTime.in("Kyiv, pub 'Naturlih'", "AIzaSyCqfXRRJ1d8mCS_I0Kcs4XnaZ9KYRUrJVE")
32
+ # => 2015-07-06 03:53:10 +0300
33
+ WhichTime.in("Kyiv", "AIzaSyCqfXRRJ1d8mCS_I0Kcs4XnaZ9KYRUrJVE", 2.days.ago)
34
+ # => 2015-07-04 03:53:10 +0300
35
+ ```
36
+
37
+ ### 2. Get local time of some address (variant 2)
38
+ ```
39
+ WhichTime.new( somewhere, google_api_key, your_time ).time
40
+ # => 2015-07-06 03:53:10 +0300
41
+ ```
42
+
43
+ ### 3. Get timezone of some place/address/city/country
44
+ ```
45
+ WhichTime.new( somewhere, google_api_key ).timezone
46
+ ```
47
+ examples:
48
+ ```
49
+ WhichTime.new("Kyiv, pub 'Naturlih'", "AIzaSyCqfXRRJ1d8mCS_I0Kcs4XnaZ9KYRUrJVE").timezone
50
+ WhichTime.in("Kyiv", "AIzaSyCqfXRRJ1d8mCS_I0Kcs4XnaZ9KYRUrJVE").timezone
51
+ # => "Europe/Kiev"
52
+ ```
53
+
54
+ ### 4. Get coordinates of some place/address/location
55
+ ```
56
+ WhichTime.new( somewhere, google_api_key ).location
57
+ # => {"lat"=>50.4501, "lng"=>30.5234}
58
+ ```
59
+
60
+ ### 5. Get coordinates of some place/address/location (variant 2)
61
+ ```
62
+ WhichTime.new( somewhere, google_api_key ).coordinates
63
+ # => "50.4501,30.5234"
64
+ ```
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it ( https://github.com/[my-github-username]/which_time/fork )
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new('spec')
4
+
5
+ # If you want to make this the default task
6
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ class WhichTime
2
+ VERSION = "0.0.1"
3
+ end
data/lib/which_time.rb ADDED
@@ -0,0 +1,47 @@
1
+ %W( which_time/version net/http uri net/http json tzinfo).each{ |lib| require lib }
2
+
3
+ class WhichTime
4
+
5
+ def initialize address, api_key, time=Time.now
6
+ @address, @api_key, @timestamp = address.gsub(' ','+'), api_key, time.to_i
7
+ end
8
+
9
+ def location
10
+ @location ||= get_location['results'][0]['geometry']['location']
11
+ end
12
+
13
+ def coordinates
14
+ location.values.join(',')
15
+ end
16
+
17
+ def timezone
18
+ @timezone ||= get_timezone['timeZoneId']
19
+ end
20
+
21
+ def time
22
+ TZInfo::Timezone.get(timezone).local_to_utc Time.at(@timestamp)
23
+ end
24
+
25
+ def self.in address, api_key, time=Time.new
26
+ new(address, api_key, time).time
27
+ end
28
+
29
+ private
30
+
31
+ def request type, params
32
+ Net::HTTP.get URI.parse("https://maps.googleapis.com/maps/api/#{type}/json?#{URI.encode_www_form(params)}")
33
+ end
34
+
35
+ def response type, params
36
+ JSON.parse request(type, params)
37
+ end
38
+
39
+ def get_location
40
+ response 'geocode', address: @address, key: @api_key
41
+ end
42
+
43
+ def get_timezone
44
+ response 'timezone', timestamp: @timestamp, location: coordinates, key: @api_key
45
+ end
46
+
47
+ end
@@ -0,0 +1,8 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'which_time' # and any other gems you need
5
+
6
+ RSpec.configure do |config|
7
+ # some (optional) config here
8
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe WhichTime do
4
+
5
+ let(:address) { "Kyiv, str. Khmelnitskogo 3, pub 'Naturlih'" }
6
+ let(:api_key) { "AIzaSyCqfXRRJ1d8mCS_I0Kcs4XnaZ9KYRUrJVE" }
7
+ let(:sometime) { Time.utc(2015, 7, 5, 9, 10) }
8
+
9
+ let(:whichtime) { WhichTime.new( address, api_key, sometime ) }
10
+
11
+ it "should be whichtime object with correct instance variables" do
12
+ expect( whichtime.instance_variable_get("@address") ).to eq( "Kyiv,+str.+Khmelnitskogo+3,+pub+'Naturlih'" )
13
+ expect( whichtime.instance_variable_get("@api_key") ).to eq( "AIzaSyCqfXRRJ1d8mCS_I0Kcs4XnaZ9KYRUrJVE" )
14
+ expect( whichtime.instance_variable_get("@timestamp") ).to eq( sometime.to_i )
15
+ end
16
+
17
+ it "should be correct request to Google geocode API" do
18
+ geocode = whichtime.send :request, "geocode", address: address, key: api_key
19
+ expect( geocode.class.to_s ).to eq( "String" )
20
+
21
+ expect( geocode ).to include( "results" )
22
+ expect( geocode ).to include( "address_components" )
23
+ expect( geocode ).to include( "geometry" )
24
+ expect( geocode ).to include( "southwest" )
25
+ expect( geocode ).to include( "place_id" )
26
+ expect( geocode ).to include( "partial_match" )
27
+ expect( geocode ).to include( "status" )
28
+ end
29
+
30
+ it "should be correct response to Google geocode API" do
31
+ geocode = whichtime.send :response, "geocode", address: address, key: api_key
32
+ expect( geocode.class.to_s ).to eq( "Hash" )
33
+ expect( geocode["status"] ).to eq( "OK" )
34
+ end
35
+
36
+ it "should be correct search for geolocation" do
37
+ geolocation = whichtime.send :get_location
38
+ expect( geolocation["results"][0]["geometry"]["location"] ).to eq({"lat"=>50.4501, "lng"=>30.5234})
39
+ end
40
+
41
+ it "should return correct location" do
42
+ location = whichtime.location
43
+ expect( whichtime.instance_variable_get("@location") ).to eq({"lat"=>50.4501, "lng"=>30.5234})
44
+ expect( location ).to eq({"lat"=>50.4501, "lng"=>30.5234})
45
+ end
46
+
47
+ it "should return correct coordinates" do
48
+ expect( whichtime.coordinates ).to eq( "50.4501,30.5234" )
49
+ end
50
+
51
+ it "should be correct response to Google timezone API" do
52
+ timezone = whichtime.send :response, "timezone", timestamp: sometime.to_i, location: "50.4501,30.5234", key: api_key
53
+ expect( timezone.class.to_s ).to eq( "Hash" )
54
+
55
+ expect( timezone["status"] ).to eq( "OK" )
56
+ expect( timezone["dstOffset"] ).to eq( 3600 )
57
+ expect( timezone["timeZoneId"] ).to eq( "Europe/Kiev" )
58
+ expect( timezone["timeZoneName"] ).to eq( "Eastern European Summer Time")
59
+ end
60
+
61
+ it "should return correct timezone" do
62
+ expect( whichtime.timezone ).to eq( "Europe/Kiev" )
63
+ end
64
+
65
+ it "should return correct time" do
66
+ expect( whichtime.time.strftime("%Y-%m-%d %H:%M:%S") ).to eq( sometime.strftime("%Y-%m-%d %H:%M:%S") )
67
+ end
68
+
69
+ it "should return correct time by address" do
70
+ result = WhichTime.in(address, api_key, sometime)
71
+ expect( result.strftime("%Y-%m-%d %H:%M:%S") ).to eq( sometime.strftime("%Y-%m-%d %H:%M:%S") )
72
+ end
73
+
74
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'which_time/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "which_time"
8
+ spec.version = WhichTime::VERSION
9
+ spec.authors = ["bmalets"]
10
+ spec.email = ["b.malets@gmail.com"]
11
+ spec.summary = %q{ search local time by your location }
12
+ spec.description = %q{ this gem provides a possibility to search local time by street/place_name/city/region/country e.g. }
13
+ spec.homepage = "https://github.com/bmalets/which_time"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "tzinfo"
25
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: which_time
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - bmalets
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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: tzinfo
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
+ description: " this gem provides a possibility to search local time by street/place_name/city/region/country
70
+ e.g. "
71
+ email:
72
+ - b.malets@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/which_time.rb
83
+ - lib/which_time/version.rb
84
+ - spec/spec_helper.rb
85
+ - spec/which_time_spec.rb
86
+ - which_time.gemspec
87
+ homepage: https://github.com/bmalets/which_time
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.2.2
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: search local time by your location
111
+ test_files:
112
+ - spec/spec_helper.rb
113
+ - spec/which_time_spec.rb