eztz 0.0.4 → 2.0.0

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,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YTI2MmIyYzdhNjEzZmVlNjhhNzQxNzk0MjY0YzcxMWVkNmYyZDhlNg==
5
- data.tar.gz: !binary |-
6
- OWI4OGMzMWM5MzRmZGRhOWM5YzMzNThlZGI0ZDJkMWE4M2YxNTdkNA==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- MDMxOGY1ZmYzMTc1ZjU2MjYwZWM2YTg3ODFiZDAzN2U3MTYxN2JlMGQ3Yjc2
10
- MjIzOTNlYzA2YTYwNjA4OTdkMmQ5ZjkyYTM4M2ZiNTZjZDAzOWU5YzdmNDE0
11
- NDY3ZTM1ZDA1YWUxNGM4MzY5MjYzZDM0YTAwZDFmMjNmMTNiZTk=
12
- data.tar.gz: !binary |-
13
- ZWFjYzk4MWZkZDNjODgzNzgzMTE4MjgyMTM1YTk3YTc1NjE4OTY4NDUxY2Ez
14
- ZGEzNWYzZGZhMjYwNWJmZDJmNzU5OWEyN2IyNmJhMmZhNjQ5ZGZhODNjMTIy
15
- MTQ4ZTFjYjgwY2QzNDE5YzM3YmVkM2JlOGU0NWRjMDk5YTA3ODQ=
2
+ SHA256:
3
+ metadata.gz: aac0186b216b00582be61d8225b7546bc14ae1848eb347a532a04ca4d86142c2
4
+ data.tar.gz: 87d018cd6c18a45bc0ae5b141585ae122f1d7776028c2563a63ff2880adacf12
5
+ SHA512:
6
+ metadata.gz: f11c757202ea3c80d0a579be9a3eaca66e017e59ecee708373ee25871f13c38ce635841c5872139347e64cf1a3ab7c3f1b906d7d6009e5ea68bff52743e52e54
7
+ data.tar.gz: 2fe7186d7b8d622a3399e1f717485775a0b61a4db4c0fa870513427fd6666aac50f34126b070d8bb05a5112ec5e9d3f0c31d746bd9544d177243e0aa5d2013e8
data/.editorconfig ADDED
@@ -0,0 +1,13 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = space
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
10
+
11
+ [*.md]
12
+ trim_trailing_whitespace = false
13
+
@@ -0,0 +1,31 @@
1
+ name: Tests
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ branches: [main]
8
+
9
+ jobs:
10
+ test:
11
+ strategy:
12
+ matrix:
13
+ os: [ubuntu-latest]
14
+ ruby-version: ["2.7", "3.0", "3.1", "3.2"]
15
+
16
+ runs-on: ${{ matrix.os }}
17
+
18
+ steps:
19
+ - uses: actions/checkout@v3
20
+ - name: Set up Ruby ${{ matrix.ruby-version }}
21
+ uses: ruby/setup-ruby@v1
22
+ with:
23
+ ruby-version: ${{ matrix.ruby-version }}
24
+ bundler-cache: true
25
+ - name: Run tests for Ruby ${{ matrix.ruby-version }} on ${{ matrix.os }}
26
+ uses: paambaati/codeclimate-action@v3.0.0
27
+ env:
28
+ CC_TEST_REPORTER_ID: ${{secrets.CC_TEST_REPORTER_ID}}
29
+ with:
30
+ coverageCommand: bundle exec rake
31
+ coverageLocations: ${{github.workspace}}/coverage/coverage.json:simplecov
data/.gitignore CHANGED
@@ -1,17 +1,11 @@
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
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /checksums/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ .tool-versions
data/.standard.yml ADDED
@@ -0,0 +1,2 @@
1
+ ruby_version: 2.7
2
+
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at chris@chaione.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
- source 'https://rubygems.org'
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
2
4
 
3
5
  # Specify your gem's dependencies in eztz.gemspec
4
6
  gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Chris Mason
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,15 +1,24 @@
1
1
  # Eztz
2
2
 
3
+ [![Tests](https://github.com/cmason/eztz/actions/workflows/test.yml/badge.svg)](https://github.com/cmason/eztz/actions/workflows/test.yml)
4
+ [![Maintainability](https://api.codeclimate.com/v1/badges/0ffbd44773bb2ee6f9ed/maintainability)](https://codeclimate.com/github/cmason/eztz/maintainability)
5
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/0ffbd44773bb2ee6f9ed/test_coverage)](https://codeclimate.com/github/cmason/eztz/test_coverage)
6
+ [![Gem Version](https://badge.fury.io/rb/eztz.png)](http://badge.fury.io/rb/eztz)
7
+
3
8
  Ruby wrapper for the [Google Time Zone API](https://developers.google.com/maps/documentation/timezone)
4
9
 
5
- [![Code Climate](https://codeclimate.com/github/cmason/eztz.png)](https://codeclimate.com/github/cmason/eztz)
6
- [![Build Status](https://travis-ci.org/cmason/eztz.png?branch=master)](https://travis-ci.org/cmason/eztz)
10
+ ## Requirements
11
+
12
+ - Ruby >= 2.7
7
13
 
14
+ Other versions of Ruby may work, but the gem is tested against the above versions.
8
15
  ## Installation
9
16
 
10
17
  Add this line to your application's Gemfile:
11
18
 
12
- gem 'eztz'
19
+ ```ruby
20
+ gem 'eztz'
21
+ ```
13
22
 
14
23
  And then execute:
15
24
 
@@ -21,33 +30,44 @@ Or install it yourself as:
21
30
 
22
31
  ## Usage
23
32
 
24
- $ Eztz.timezone(lat: -33.86, lng: 151.20)
33
+ An API key is required to access the Google Time Zone API. You can create a new
34
+ API key from the [Google Cloud Console](https://developers.google.com/maps/documentation/timezone/get-api-key).
35
+
36
+ # Set your API key
37
+ Eztz.api_key = 'YOUR_API_KEY'
25
38
 
26
- => #<Hashie::Mash dstOffset=3600.0 rawOffset=36000.0 status="OK" timeZoneId="Australia/Sydney" timeZoneName="Australian Eastern Daylight Time">
39
+ # Pass location coordinates as a string
40
+ $ Eztz.timezone(location: '29.65,-95.28')
41
+
42
+ => #<Eztz::TimeZoneResponse:0x007fe71ba2df78 @timestamp=1488834591, @dst_offset=0, @error_message=nil, @raw_offset=-21600, @status="OK", @id="America/Chicago", @name="Central Standard Time">
27
43
 
28
44
  or
29
45
 
30
- $ Eztz.timezone(location: '-33.86,151.20')
46
+ # Pass location coordinates as an array
47
+ $ Eztz.timezone(location: [29.65, -95.28])
48
+
49
+ => #<Eztz::TimeZoneResponse:0x007fe71ba2df78 @timestamp=1488834591, @dst_offset=0, @error_message=nil, @raw_offset=-21600, @status="OK", @id="America/Chicago", @name="Central Standard Time">
31
50
 
32
- => #<Hashie::Mash dstOffset=3600.0 rawOffset=36000.0 status="OK" timeZoneId="Australia/Sydney" timeZoneName="Australian Eastern Daylight Time">
51
+ Optional parameters include:
33
52
 
34
- Params should take one of the following forms:
53
+ * `timestamp:` specifies the desired time as seconds since midnight, January 1, 1970 UTC. The Google Maps Time Zone API uses the timestamp to determine whether or not Daylight Savings should be applied. Times before 1970 can be expressed as negative values. Defaults to the current time.
54
+ * `language:` The language in which to return results Defaults to 'en'. A list of supported languages can be found at developers.google.com/maps/faq#languagesupport
35
55
 
36
- { location: "-33.86,151.20" }
37
- { lat: -33.86, lng: 151.20 }
56
+ ## Development
38
57
 
39
- ## Calculating the Local Time
58
+ After checking out the repo, run `bin/setup` to install dependencies.
59
+ Then, run `rake test` to run the tests. You can also run `bin/console`
60
+ for an interactive prompt that will allow you to experiment.
40
61
 
41
- The local time of a given location is the sum of the `Time.now.to_i` , and the `dstOffset` and `rawOffset` fields from the result.
62
+ To install this gem onto your local machine, run `bundle exec rake install`.
63
+ To release a new version, update the version number in `version.rb`,
64
+ and then run `bundle exec rake release`, which will create a git tag for the version,
65
+ push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
66
 
43
67
  ## Contributing
44
68
 
45
- 1. Fork it
46
- 2. Create your feature branch (`git checkout -b my-new-feature`)
47
- 3. Commit your changes (`git commit -am 'Added some feature'`)
48
- 4. Push to the branch (`git push origin my-new-feature`)
49
- 5. Create new Pull Request
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cmason/eztz. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
50
70
 
51
71
  ## License
52
72
 
53
- This code is provided under the MIT license. See [LICENSE](LICENSE) for more details.
73
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+ require "standard/rake"
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << "test"
9
+ t.libs << "lib"
10
+ t.test_files = FileList["test/**/*_test.rb"]
11
+ end
12
+
13
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ require 'bundler/setup'
4
+ require 'eztz'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/eztz.gemspec CHANGED
@@ -1,26 +1,33 @@
1
- # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/eztz/version', __FILE__)
1
+ # frozen_string_literal: true
3
2
 
4
- Gem::Specification.new do |gem|
5
- gem.name = "eztz"
6
- gem.authors = ["Chris Mason"]
7
- gem.email = ["chris@chaione.com"]
8
- gem.description = %q{Ruby wrapper for the Google Time Zone API}
9
- gem.license = 'MIT'
10
- gem.summary = gem.description
11
- gem.homepage = "https://github.com/cmason/eztz"
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "eztz/version"
12
6
 
13
- gem.files = `git ls-files`.split($\)
14
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
- gem.require_paths = ["lib"]
17
- gem.version = Eztz::VERSION
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "eztz"
9
+ spec.version = Eztz::VERSION
10
+ spec.authors = ["Chris Mason"]
11
+ spec.email = ["chris@chaione.com"]
18
12
 
19
- gem.add_development_dependency 'rake'
20
- gem.add_development_dependency 'rspec', '~> 2.12.0'
21
- gem.add_development_dependency 'vcr', '~> 2.4.0'
22
- gem.add_development_dependency 'webmock', '~> 1.9.3'
13
+ spec.summary = "Ruby wrapper for the Google Time Zone API"
14
+ spec.description = spec.summary
15
+ spec.homepage = "https://github.com/cmason/eztz"
16
+ spec.license = "MIT"
23
17
 
24
- gem.add_dependency 'httparty', '~> 0.10'
25
- gem.add_dependency 'hashie', '>= 1.2.0'
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7")
26
+
27
+ spec.add_development_dependency "bundler", ">= 2.2.33"
28
+ spec.add_development_dependency "minitest"
29
+ spec.add_development_dependency "rake", ">= 12.3.3"
30
+ spec.add_development_dependency "simplecov", ">= 0.22.0"
31
+ spec.add_development_dependency "standard"
32
+ spec.add_development_dependency "webmock"
26
33
  end
data/lib/eztz/client.rb CHANGED
@@ -1,49 +1,56 @@
1
- require 'httparty'
2
- require 'hashie'
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "json"
3
5
 
4
6
  module Eztz
7
+ # The Google Time Zone API client.
8
+ # If you need to make requests with multiple api keys, then you'll probably
9
+ # want to use this class directly. Otherwise, it will be easier to use
10
+ # {::Eztz#timezone Eztz.timezone}
5
11
  class Client
6
- include HTTParty
7
- base_uri 'https://maps.googleapis.com'
12
+ attr_reader :api_key
13
+
14
+ def initialize(api_key: Eztz.api_key)
15
+ @api_key = api_key
16
+ @uri = URI("https://maps.googleapis.com/maps/api/timezone/json")
17
+ end
8
18
 
9
19
  # Gets timezone information for a location on earth,
10
20
  # as well as that location's time offset from UTC.
11
21
  #
12
- # @param [Hash] params options to get timezone information.
13
- # Must include :location or :lat & :lng
14
- # @option params [String] :location a comma-separated lat,lng tuple. (eg. "-33.86,151.20")
15
- # @option params [String, Float] :lat latitude (eg. -33.86)
16
- # @option params [String, Float] :lng longitude (ex. 151.20)
17
- # @option params [Boolean] :sensor specifies whether the application requesting data is using a sensor. Defaults to _false_
18
- # @option params [Integer] :timestamp specifies the desired time as seconds since midnight, January 1, 1970 UTC. Defaults to _now_
19
- # @option params [String] :language the language in which to return results. Defaults to _en_.
20
- # @raise [ArgumentError] if :location or :lat and :lng are not provided
21
- # @return [Hashie::Mash] the resulting timezone data. Response has the following properties:
22
- # * *dstOffset* : the offset for daylight-savings time in seconds.
23
- # This will be zero if the time zone is not in Daylight Savings Time during the specified timestamp.
24
- # * *rawOffset* : the offset from UTC (in seconds) for the given location.
25
- # This does not take into effect daylight savings.
26
- # * *timeZoneId* : a string containing the ID of the time zone, such as "America/Los_Angeles" or "Australia/Sydney".
27
- # * *timeZoneName* : a string containing the long form name of the time zone.
28
- # This field will be localized if the language parameter is set.
29
- # eg. "Pacific Daylight Time" or "Australian Eastern Daylight Time"
30
- # * *status* : a string indicating the status of the response. Status will be one of the following:
31
- # * *OK* - indicates that the request was successful.
32
- # * *INVALID_REQUEST* - indicates that the request was malformed.
33
- # * *OVER_QUERY_LIMIT* - indicates the requestor has exceeded quota.
34
- # * *REQUEST_DENIED* - indicates that the the API did not complete the request. Confirm that the request was sent over http instead of https.
35
- # * *UNKNOWN_ERROR* - indicates an unknown error.
36
- # * *ZERO_RESULTS* - indicates that no time zone data could be found for the specified position or time.
37
- # Confirm that the request is for a location on land, and not over water.
38
- # @example Get timezone data
39
- # Eztz::Client.timezone(location: "-33.86,151.20")
40
- # => #<Hashie::Mash dstOffset=3600.0 rawOffset=36000.0 status="OK" timeZoneId="Australia/Sydney" timeZoneName="Australian Eastern Daylight Time">
41
- def self.timezone(params={})
42
- params[:location] = "#{params.delete(:lat)},#{params.delete(:lng)}" if params[:lat] && params[:lng]
43
- raise ArgumentError, 'You must provide a location' if params[:location].nil? || params[:location].empty? || params[:location] == ','
44
- params[:sensor] ||= false
45
- params[:timestamp] ||= Time.now.to_i
46
- Hashie::Mash.new self.get('/maps/api/timezone/json', query: params)
22
+ # @param location [String, Array] a comma-separated lat,lng tuple
23
+ # (eg. "-33.86,151.20"), representing the location to look up. Can also be
24
+ # an array (eg. [-33.86, 151.20]).
25
+ # @param timestamp [Integer] specifies the desired time as seconds
26
+ # since midnight, January 1, 1970 UTC. The Google Maps Time Zone API uses
27
+ # the timestamp to determine whether or not Daylight Savings should be
28
+ # applied. Times before 1970 can be expressed as negative values.
29
+ # Defaults to the current time.
30
+ # @param language [String] The language in which to return results
31
+ # Defaults to 'en'. A list of supported languages can be found at
32
+ # https://developers.google.com/maps/faq#languagesupport
33
+ # @raise [ArgumentError] if location is not provided
34
+ # @raise [ApiError] if the API returns an error response.
35
+ # @return [Eztz::TimeZoneResponse] the resulting timezone data.
36
+ def timezone(location:, timestamp: Time.now.utc.to_i, language: "en")
37
+ uri.query = query_params(location, timestamp, language)
38
+ res = Net::HTTP.get_response(uri)
39
+ raise ApiError, res.body unless res.is_a?(Net::HTTPSuccess)
40
+ TimeZoneResponse.new(timestamp, JSON.parse(res.body))
41
+ end
42
+
43
+ private
44
+
45
+ attr_reader :uri
46
+
47
+ def query_params(location, timestamp, language)
48
+ URI.encode_www_form(
49
+ location: (location.respond_to?(:join) ? location.join(",") : location),
50
+ timestamp: timestamp,
51
+ language: language,
52
+ key: api_key
53
+ )
47
54
  end
48
55
  end
49
56
  end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Eztz
6
+ # Thrown when the API returns an error response.
7
+ class ApiError < StandardError
8
+ attr_reader :message
9
+
10
+ def initialize(response)
11
+ @message = response
12
+ end
13
+
14
+ def to_s
15
+ @message.to_s
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Eztz
4
+ # Encapsulates the data from the timezone api
5
+ class TimeZoneResponse
6
+ # @!attribute dst_offset [r]
7
+ # @return [Integer] The offset for daylight-savings time in seconds.
8
+ # This will be zero if the time zone is not in Daylight Savings Time
9
+ # during the specified timestamp.
10
+ # @!attribute error_message [r]
11
+ # @return [String] More detailed information about the reasons behind the
12
+ # given status code, if other than OK.
13
+ # @!attribute id [r]
14
+ # @return [String] A string containing the ID of the time zone, such as
15
+ # "America/Los_Angeles" or "Australia/Sydney".
16
+ # @!attribute name [r]
17
+ # @return [String] A string containing the long form name of the time
18
+ # zone. This field will be localized if the language parameter was set.
19
+ # eg. "Pacific Daylight Time" or "Australian Eastern Daylight Time"
20
+ # @!attribute raw_offset [r]
21
+ # @return [Integer] The offset from UTC (in seconds) for the given
22
+ # location. This does not take into effect daylight savings.
23
+ # @!attribute status [r]
24
+ # @return [String] a string indicating the status of the response. Status
25
+ # will be one of the following:
26
+ # * *OK* - indicates that the request was successful.
27
+ # * *INVALID_REQUEST* - indicates that the request was malformed.
28
+ # * *OVER_QUERY_LIMIT* - indicates the requestor has exceeded quota.
29
+ # * *REQUEST_DENIED* - indicates that the the API did not complete the
30
+ # request. Confirm that the request was sent over http instead of
31
+ # https.
32
+ # * *UNKNOWN_ERROR* - indicates an unknown error.
33
+ # * *ZERO_RESULTS* - indicates that no time zone data could be found
34
+ # for the specified position or time. Confirm that the request is for
35
+ # a location on land, and not over water.
36
+ # @!attribute timestamp [r]
37
+ # @return [Integer] The timestamp used in the request.
38
+ attr_reader :dst_offset, :error_message, :id, :name,
39
+ :raw_offset, :status, :timestamp
40
+
41
+ # @param timestamp [Integer] The timestamp used in the request
42
+ # @param response [Hash] The parsed JSON response from the api call
43
+ def initialize(timestamp, response)
44
+ @timestamp = timestamp
45
+ @dst_offset, @error_message, @raw_offset, @status, @id, @name =
46
+ response.values_at(
47
+ "dstOffset", "error_message", "rawOffset", "status", "timeZoneId",
48
+ "timeZoneName"
49
+ )
50
+ end
51
+
52
+ def success?
53
+ status == "OK"
54
+ end
55
+
56
+ # The local time of the location is the sum of the :timestamp,
57
+ # :dst_offset and :raw_offset.
58
+ # @return [Time]
59
+ def local_time
60
+ Time.at(timestamp.to_i + dst_offset.to_i + raw_offset.to_i)
61
+ end
62
+
63
+ # The object as a Hash.
64
+ # @return [Hash]
65
+ def to_h
66
+ {
67
+ dst_offset: dst_offset,
68
+ error_message: error_message,
69
+ id: id,
70
+ name: name,
71
+ raw_offset: raw_offset,
72
+ status: status,
73
+ timestamp: timestamp
74
+ }
75
+ end
76
+ end
77
+ end
data/lib/eztz/version.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Eztz
2
- VERSION = "0.0.4"
4
+ VERSION = "2.0.0"
3
5
  end
data/lib/eztz.rb CHANGED
@@ -1,22 +1,68 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "eztz/client"
4
+ require "eztz/errors"
5
+ require "eztz/response"
2
6
  require "eztz/version"
3
7
 
8
+ # Ruby wrapper for the Google Time Zone API.
9
+ # @example Sample usage
10
+ # # Set your api key
11
+ # Eztz.api_key = 'YOUR_API_KEY'
12
+ # # location as a string
13
+ # Eztz.timezone(location: "-33.86,151.20")
14
+ # # location as an array
15
+ # Eztz.timezone(location: [-33.86,151.20])
4
16
  module Eztz
5
- # Gets timezone information for a location on earth,
6
- # as well as that location's time offset from UTC.
7
- #
8
- # @param [Hash] params options to get timezone information.
9
- # Must include :location or :lat & :lng
10
- # @option params [String] :location a comma-separated lat,lng tuple. (eg. "-33.86,151.20")
11
- # @option params [String, Float] :lat latitude (eg. -33.86)
12
- # @option params [String, Float] :lng longitude (ex. 151.20)
13
- # @raise [ArgumentError] if :location or :lat and :lng are not provided
14
- # @return [Hashie::Mash] the resulting timezone data.
15
- # @example Get timezone data
16
- # Eztz.timezone(lat: -33.86, lng: 151.20)
17
- # => #<Hashie::Mash dstOffset=3600.0 rawOffset=36000.0 status="OK" timeZoneId="Australia/Sydney" timeZoneName="Australian Eastern Daylight Time">
18
- # @see Client.timezone
19
- def self.timezone(params={})
20
- Eztz::Client.timezone(params)
17
+ class << self
18
+ # @!attribute api_key
19
+ # @return [String] Your application's API key.
20
+ # Api keys can be obtained at
21
+ # https://developers.google.com/maps/documentation/timezone/get-api-key
22
+ attr_accessor :api_key
23
+
24
+ # @example Configure api key with a configuration block
25
+ # Eztz.configure do |config|
26
+ # config.api_key = 'XYZ'
27
+ # end
28
+ def configure
29
+ yield self
30
+ true
31
+ end
32
+
33
+ # The Eztz client
34
+ # @return [Eztz::Client]
35
+ def client
36
+ @client ||= Eztz::Client.new
37
+ end
38
+
39
+ # Gets timezone information for a location on earth,
40
+ # as well as that location's time offset from UTC.
41
+ #
42
+ # @param location [String, Array] a comma-separated lat,lng tuple
43
+ # (eg. "-33.86,151.20"), representing the location to look up. Can also be
44
+ # an array (eg. [-33.86, 151.20]).
45
+ # @param timestamp [Integer] specifies the desired time as seconds
46
+ # since midnight, January 1, 1970 UTC. The Google Maps Time Zone API uses
47
+ # the timestamp to determine whether or not Daylight Savings should be
48
+ # applied. Times before 1970 can be expressed as negative values.
49
+ # Defaults to the current time.
50
+ # @param language [String] The language in which to return results
51
+ # Defaults to 'en'. A list of supported languages can be found at
52
+ # https://developers.google.com/maps/faq#languagesupport
53
+ # @raise [ArgumentError] if location is not provided
54
+ # @raise [ApiError] if the API returns an error response.
55
+ # @return [Eztz::TimeZoneResponse] the resulting timezone data.
56
+ # @example Get the timezone for a location
57
+ # Eztz.timezone(location: "-33.86,151.20")
58
+ # => #<Eztz::TimeZoneResponse:0x007fe71c906298 @timestamp=1488580176,
59
+ # @dst_offset=3600, @error_message=nil, @raw_offset=36000,
60
+ # @status="OK", @id="Australia/Sydney",
61
+ # @name="Australian Eastern Daylight Time">
62
+ def timezone(location:, timestamp: Time.now.utc.to_i, language: "en")
63
+ client.timezone(location: location,
64
+ timestamp: timestamp,
65
+ language: language)
66
+ end
21
67
  end
22
68
  end
metadata CHANGED
@@ -1,99 +1,99 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eztz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Mason
8
- autorequire:
9
- bindir: bin
8
+ autorequire:
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2013-09-04 00:00:00.000000000 Z
11
+ date: 2023-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rake
14
+ name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 2.2.33
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 2.2.33
27
27
  - !ruby/object:Gem::Dependency
28
- name: rspec
28
+ name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 2.12.0
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 2.12.0
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: vcr
42
+ name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 2.4.0
47
+ version: 12.3.3
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 2.4.0
54
+ version: 12.3.3
55
55
  - !ruby/object:Gem::Dependency
56
- name: webmock
56
+ name: simplecov
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: 1.9.3
61
+ version: 0.22.0
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: 1.9.3
68
+ version: 0.22.0
69
69
  - !ruby/object:Gem::Dependency
70
- name: httparty
70
+ name: standard
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ~>
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '0.10'
76
- type: :runtime
75
+ version: '0'
76
+ type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ~>
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '0.10'
82
+ version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: hashie
84
+ name: webmock
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ! '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: 1.2.0
90
- type: :runtime
89
+ version: '0'
90
+ type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ! '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
- version: 1.2.0
96
+ version: '0'
97
97
  description: Ruby wrapper for the Google Time Zone API
98
98
  email:
99
99
  - chris@chaione.com
@@ -101,49 +101,44 @@ executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
- - .gitignore
105
- - .rspec
106
- - .travis.yml
104
+ - ".editorconfig"
105
+ - ".github/workflows/test.yml"
106
+ - ".gitignore"
107
+ - ".standard.yml"
108
+ - CODE_OF_CONDUCT.md
107
109
  - Gemfile
108
- - LICENSE
110
+ - LICENSE.txt
109
111
  - README.md
110
112
  - Rakefile
113
+ - bin/console
114
+ - bin/setup
111
115
  - eztz.gemspec
112
116
  - lib/eztz.rb
113
117
  - lib/eztz/client.rb
118
+ - lib/eztz/errors.rb
119
+ - lib/eztz/response.rb
114
120
  - lib/eztz/version.rb
115
- - spec/acceptance/get_timezone_spec.rb
116
- - spec/eztz_spec.rb
117
- - spec/fixtures/cassettes/emtpy_results.yml
118
- - spec/fixtures/cassettes/valid_timezone_lookup.yml
119
- - spec/spec_helper.rb
120
121
  homepage: https://github.com/cmason/eztz
121
122
  licenses:
122
123
  - MIT
123
124
  metadata: {}
124
- post_install_message:
125
+ post_install_message:
125
126
  rdoc_options: []
126
127
  require_paths:
127
128
  - lib
128
129
  required_ruby_version: !ruby/object:Gem::Requirement
129
130
  requirements:
130
- - - ! '>='
131
+ - - ">="
131
132
  - !ruby/object:Gem::Version
132
- version: '0'
133
+ version: '2.7'
133
134
  required_rubygems_version: !ruby/object:Gem::Requirement
134
135
  requirements:
135
- - - ! '>='
136
+ - - ">="
136
137
  - !ruby/object:Gem::Version
137
138
  version: '0'
138
139
  requirements: []
139
- rubyforge_project:
140
- rubygems_version: 2.0.0
141
- signing_key:
140
+ rubygems_version: 3.4.6
141
+ signing_key:
142
142
  specification_version: 4
143
143
  summary: Ruby wrapper for the Google Time Zone API
144
- test_files:
145
- - spec/acceptance/get_timezone_spec.rb
146
- - spec/eztz_spec.rb
147
- - spec/fixtures/cassettes/emtpy_results.yml
148
- - spec/fixtures/cassettes/valid_timezone_lookup.yml
149
- - spec/spec_helper.rb
144
+ test_files: []
data/.rspec DELETED
@@ -1 +0,0 @@
1
- --format nested --colour
data/.travis.yml DELETED
@@ -1,10 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - "1.9.2"
4
- - "1.9.3"
5
- - "2.0.0"
6
- - jruby-19mode # JRuby in 1.9 mode
7
- - rbx-19mode
8
- # uncomment this line if your project needs to run something other than `rake`:
9
- script: bundle exec rspec spec
10
-
data/LICENSE DELETED
@@ -1,22 +0,0 @@
1
- Copyright (c) 2013 Chris Mason
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.
@@ -1,35 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Eztz do
4
- context "lookup timezone" do
5
- it "by location" do
6
- VCR.use_cassette("valid_timezone_lookup") do
7
- tz = Eztz.timezone(location: "37.7749295,-122.4194155", timestamp: 1362096689)
8
- tz.dstOffset.should == 0.0
9
- tz.rawOffset.should == -28800.0
10
- tz.status.should == 'OK'
11
- tz.timeZoneId.should == "America/Los_Angeles"
12
- tz.timeZoneName.should == "Pacific Standard Time"
13
- end
14
- end
15
-
16
- it "by :lat :lng" do
17
- VCR.use_cassette("valid_timezone_lookup") do
18
- tz = Eztz.timezone(lat: 37.7749295, lng: -122.4194155, timestamp: 1362096689)
19
- tz.dstOffset.should == 0.0
20
- tz.rawOffset.should == -28800.0
21
- tz.status.should == 'OK'
22
- tz.timeZoneId.should == "America/Los_Angeles"
23
- tz.timeZoneName.should == "Pacific Standard Time"
24
- end
25
- end
26
-
27
- it "with no results" do
28
- VCR.use_cassette("emtpy_results") do
29
- tz = Eztz.timezone(lat: 0, lng: 0, timestamp: 1362096689)
30
- tz.status.should == 'ZERO_RESULTS'
31
- tz.timeZpneId.should be_nil
32
- end
33
- end
34
- end
35
- end
data/spec/eztz_spec.rb DELETED
@@ -1,42 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Eztz do
4
- it "should define a version" do
5
- Eztz::VERSION.should_not be_nil
6
- end
7
-
8
- context "timezone lookup" do
9
- let(:query) {
10
- {
11
- query: {
12
- location:"37.7749295,-122.4194155",
13
- timestamp:1,
14
- sensor:false
15
- }
16
- }
17
- }
18
-
19
- context "with invalid parameters" do
20
- it "should raise an argument error" do
21
- expect { Eztz.timezone }.to raise_error ArgumentError
22
- expect { Eztz.timezone lat: '', lng: ''}.to raise_error ArgumentError
23
- end
24
- end
25
-
26
- context "by location" do
27
- let(:params) { {location: "37.7749295,-122.4194155", timestamp: 1, sensor: false} }
28
- it "should get timezone by location" do
29
- Eztz::Client.should_receive(:get).with("/maps/api/timezone/json", query )
30
- Eztz.timezone(params)
31
- end
32
- end
33
-
34
- context "by :lat :lng" do
35
- let(:params) { {lat: 37.7749295, lng: -122.4194155, timestamp: 1, sensor: false } }
36
- it "should get timezone by lat lng" do
37
- Eztz::Client.should_receive(:get).with("/maps/api/timezone/json", query )
38
- Eztz.timezone(params)
39
- end
40
- end
41
- end
42
- end
@@ -1,36 +0,0 @@
1
- ---
2
- http_interactions:
3
- - request:
4
- method: get
5
- uri: https://maps.googleapis.com/maps/api/timezone/json?location=0,0&sensor=false&timestamp=1362096689
6
- body:
7
- encoding: US-ASCII
8
- string: ''
9
- headers: {}
10
- response:
11
- status:
12
- code: 200
13
- message: OK
14
- headers:
15
- Content-Type:
16
- - application/json; charset=UTF-8
17
- Access-Control-Allow-Origin:
18
- - ! '*'
19
- Date:
20
- - Fri, 01 Mar 2013 14:55:57 GMT
21
- Server:
22
- - mafe
23
- Cache-Control:
24
- - private
25
- X-Xss-Protection:
26
- - 1; mode=block
27
- X-Frame-Options:
28
- - SAMEORIGIN
29
- Transfer-Encoding:
30
- - chunked
31
- body:
32
- encoding: US-ASCII
33
- string: ! "{\n \"status\" : \"ZERO_RESULTS\"\n}\n"
34
- http_version:
35
- recorded_at: Fri, 01 Mar 2013 14:55:57 GMT
36
- recorded_with: VCR 2.4.0
@@ -1,38 +0,0 @@
1
- ---
2
- http_interactions:
3
- - request:
4
- method: get
5
- uri: https://maps.googleapis.com/maps/api/timezone/json?location=37.7749295,-122.4194155&sensor=false&timestamp=1362096689
6
- body:
7
- encoding: US-ASCII
8
- string: ''
9
- headers: {}
10
- response:
11
- status:
12
- code: 200
13
- message: OK
14
- headers:
15
- Content-Type:
16
- - application/json; charset=UTF-8
17
- Access-Control-Allow-Origin:
18
- - ! '*'
19
- Date:
20
- - Fri, 01 Mar 2013 00:11:30 GMT
21
- Server:
22
- - mafe
23
- Cache-Control:
24
- - private
25
- X-Xss-Protection:
26
- - 1; mode=block
27
- X-Frame-Options:
28
- - SAMEORIGIN
29
- Transfer-Encoding:
30
- - chunked
31
- body:
32
- encoding: US-ASCII
33
- string: ! "{\n \"dstOffset\" : 0.0,\n \"rawOffset\" : -28800.0,\n \"status\"
34
- : \"OK\",\n \"timeZoneId\" : \"America/Los_Angeles\",\n \"timeZoneName\"
35
- : \"Pacific Standard Time\"\n}\n"
36
- http_version:
37
- recorded_at: Fri, 01 Mar 2013 00:11:30 GMT
38
- recorded_with: VCR 2.4.0
data/spec/spec_helper.rb DELETED
@@ -1,13 +0,0 @@
1
- lib = File.expand_path('../../lib', __FILE__)
2
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require 'rubygems'
4
- require 'rspec'
5
- require 'vcr'
6
- require 'httparty'
7
- require 'eztz'
8
-
9
- VCR.configure do |c|
10
- c.cassette_library_dir = 'spec/fixtures/cassettes'
11
- c.hook_into :webmock
12
- c.allow_http_connections_when_no_cassette = true
13
- end