weatherb 0.1.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 +7 -0
- data/.editorconfig +17 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/DEFAULT_VALUES.md +99 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +60 -0
- data/LICENSE.txt +21 -0
- data/README.md +72 -0
- data/Rakefile +8 -0
- data/bin/console +29 -0
- data/bin/setup +8 -0
- data/lib/faraday_middleware/raise_error.rb +45 -0
- data/lib/weatherb.rb +9 -0
- data/lib/weatherb/api.rb +217 -0
- data/lib/weatherb/error.rb +30 -0
- data/lib/weatherb/version.rb +5 -0
- data/weatherb.gemspec +28 -0
- metadata +67 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: a7410d1705043e6695c4682afc9324963d5662d2cd10665cf1cfde42de7125e2
|
|
4
|
+
data.tar.gz: 91eaeb55e1c81c96f47f0a643423cb1e7320324056ee5de12ea378b007ffe5c5
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 2c492573e155c89b0687bd9873fd05ffb89349e21d60d4745efbd11a9fbb2cc8baea7084bde0815a83b94bf7c584993e0972866f05577d4cc5a074169dc72ee3
|
|
7
|
+
data.tar.gz: 9b0e5ea1081cf96394819fd20d924bfa26f40b292167e43d2cb9dc74acb14ffe7baeb9939270f718f65a2a207aa6a916f6e92adef1349080a4e74bf3a2d86967
|
data/.editorconfig
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# ╔═╗╔╦╗╦╔╦╗╔═╗╦═╗┌─┐┌─┐┌┐┌┌─┐┬┌─┐
|
|
2
|
+
# ║╣ ║║║ ║ ║ ║╠╦╝│ │ ││││├┤ ││ ┬
|
|
3
|
+
# o╚═╝═╩╝╩ ╩ ╚═╝╩╚═└─┘└─┘┘└┘└ ┴└─┘
|
|
4
|
+
#
|
|
5
|
+
# This file (`.editorconfig`) exists to help maintain consistent formatting
|
|
6
|
+
# throughout this project.
|
|
7
|
+
#
|
|
8
|
+
# To review what each of these options mean, see:
|
|
9
|
+
# http://editorconfig.org/
|
|
10
|
+
root = true
|
|
11
|
+
|
|
12
|
+
[*]
|
|
13
|
+
indent_style = space
|
|
14
|
+
indent_size = 2
|
|
15
|
+
trim_trailing_whitespace = true
|
|
16
|
+
insert_final_newline = true
|
|
17
|
+
charset = utf-8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -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 m.zackky@gmail.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 [https://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: https://contributor-covenant.org
|
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/DEFAULT_VALUES.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# The Default Values
|
|
2
|
+
List of default values for each API
|
|
3
|
+
|
|
4
|
+
## realtime
|
|
5
|
+
|
|
6
|
+
- unit_system: si
|
|
7
|
+
- fields:
|
|
8
|
+
- temp
|
|
9
|
+
- feels_like
|
|
10
|
+
- humidity
|
|
11
|
+
- wind_speed
|
|
12
|
+
- wind_direction
|
|
13
|
+
- baro_pressure
|
|
14
|
+
- precipitation
|
|
15
|
+
- sunrise
|
|
16
|
+
- sunset
|
|
17
|
+
- visibility
|
|
18
|
+
- weather_code
|
|
19
|
+
- dewpoint
|
|
20
|
+
- wind_gust
|
|
21
|
+
- precipitation_type
|
|
22
|
+
- cloud_cover
|
|
23
|
+
- cloud_base
|
|
24
|
+
- cloud_ceiling
|
|
25
|
+
- surface_shortwave_radiation
|
|
26
|
+
- moon_phase
|
|
27
|
+
|
|
28
|
+
## nowcast
|
|
29
|
+
|
|
30
|
+
- unit_system: si
|
|
31
|
+
- timestep: 1
|
|
32
|
+
- start_time: now
|
|
33
|
+
- end_time: nil
|
|
34
|
+
- fields:
|
|
35
|
+
- temp
|
|
36
|
+
- feels_like
|
|
37
|
+
- humidity
|
|
38
|
+
- wind_speed
|
|
39
|
+
- wind_direction
|
|
40
|
+
- baro_pressure
|
|
41
|
+
- precipitation
|
|
42
|
+
- sunrise
|
|
43
|
+
- sunset
|
|
44
|
+
- visibility
|
|
45
|
+
- weather_code
|
|
46
|
+
- dewpoint
|
|
47
|
+
- wind_gust
|
|
48
|
+
- precipitation_type
|
|
49
|
+
- cloud_cover
|
|
50
|
+
- cloud_base
|
|
51
|
+
- cloud_ceiling
|
|
52
|
+
- surface_shortwave_radiation
|
|
53
|
+
|
|
54
|
+
## hourly
|
|
55
|
+
|
|
56
|
+
- unit_system: si
|
|
57
|
+
- start_time: now
|
|
58
|
+
- end_time: nil
|
|
59
|
+
- fields:
|
|
60
|
+
- temp
|
|
61
|
+
- feels_like
|
|
62
|
+
- humidity
|
|
63
|
+
- wind_speed
|
|
64
|
+
- wind_direction
|
|
65
|
+
- baro_pressure
|
|
66
|
+
- precipitation
|
|
67
|
+
- sunrise
|
|
68
|
+
- sunset
|
|
69
|
+
- visibility
|
|
70
|
+
- weather_code
|
|
71
|
+
- dewpoint
|
|
72
|
+
- wind_gust
|
|
73
|
+
- precipitation_type
|
|
74
|
+
- precipitation_probability
|
|
75
|
+
- cloud_cover
|
|
76
|
+
- cloud_base
|
|
77
|
+
- cloud_ceiling
|
|
78
|
+
- surface_shortwave_radiation
|
|
79
|
+
- moon_phase
|
|
80
|
+
|
|
81
|
+
## daily
|
|
82
|
+
|
|
83
|
+
- unit_system: si
|
|
84
|
+
- start_time: now
|
|
85
|
+
- end_time: nil
|
|
86
|
+
- fields:
|
|
87
|
+
- temp
|
|
88
|
+
- feels_like
|
|
89
|
+
- humidity
|
|
90
|
+
- wind_speed
|
|
91
|
+
- wind_direction
|
|
92
|
+
- baro_pressure
|
|
93
|
+
- precipitation
|
|
94
|
+
- sunrise
|
|
95
|
+
- sunset
|
|
96
|
+
- visibility
|
|
97
|
+
- weather_code
|
|
98
|
+
- precipitation_probability
|
|
99
|
+
- precipitation_accumulation
|
data/Gemfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
source 'https://rubygems.org'
|
|
4
|
+
|
|
5
|
+
# Specify your gem's dependencies in weatherb.gemspec
|
|
6
|
+
gemspec
|
|
7
|
+
|
|
8
|
+
gem 'faraday', '~> 1.0'
|
|
9
|
+
gem 'faraday_middleware', '~> 1.0'
|
|
10
|
+
gem 'rake', '~> 12.0'
|
|
11
|
+
gem 'rspec', '~> 3.0'
|
|
12
|
+
gem 'rubocop', '~> 0.84', require: false
|
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
weatherb (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
ast (2.4.0)
|
|
10
|
+
diff-lcs (1.3)
|
|
11
|
+
faraday (1.0.1)
|
|
12
|
+
multipart-post (>= 1.2, < 3)
|
|
13
|
+
faraday_middleware (1.0.0)
|
|
14
|
+
faraday (~> 1.0)
|
|
15
|
+
multipart-post (2.1.1)
|
|
16
|
+
parallel (1.19.1)
|
|
17
|
+
parser (2.7.1.3)
|
|
18
|
+
ast (~> 2.4.0)
|
|
19
|
+
rainbow (3.0.0)
|
|
20
|
+
rake (12.3.3)
|
|
21
|
+
rexml (3.2.4)
|
|
22
|
+
rspec (3.9.0)
|
|
23
|
+
rspec-core (~> 3.9.0)
|
|
24
|
+
rspec-expectations (~> 3.9.0)
|
|
25
|
+
rspec-mocks (~> 3.9.0)
|
|
26
|
+
rspec-core (3.9.2)
|
|
27
|
+
rspec-support (~> 3.9.3)
|
|
28
|
+
rspec-expectations (3.9.2)
|
|
29
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
30
|
+
rspec-support (~> 3.9.0)
|
|
31
|
+
rspec-mocks (3.9.1)
|
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
33
|
+
rspec-support (~> 3.9.0)
|
|
34
|
+
rspec-support (3.9.3)
|
|
35
|
+
rubocop (0.84.0)
|
|
36
|
+
parallel (~> 1.10)
|
|
37
|
+
parser (>= 2.7.0.1)
|
|
38
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
39
|
+
rexml
|
|
40
|
+
rubocop-ast (>= 0.0.3)
|
|
41
|
+
ruby-progressbar (~> 1.7)
|
|
42
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
|
43
|
+
rubocop-ast (0.0.3)
|
|
44
|
+
parser (>= 2.7.0.1)
|
|
45
|
+
ruby-progressbar (1.10.1)
|
|
46
|
+
unicode-display_width (1.7.0)
|
|
47
|
+
|
|
48
|
+
PLATFORMS
|
|
49
|
+
ruby
|
|
50
|
+
|
|
51
|
+
DEPENDENCIES
|
|
52
|
+
faraday (~> 1.0)
|
|
53
|
+
faraday_middleware (~> 1.0)
|
|
54
|
+
rake (~> 12.0)
|
|
55
|
+
rspec (~> 3.0)
|
|
56
|
+
rubocop (~> 0.84)
|
|
57
|
+
weatherb!
|
|
58
|
+
|
|
59
|
+
BUNDLED WITH
|
|
60
|
+
2.1.4
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Zackky Muhammad
|
|
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
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# Weatherb
|
|
2
|
+
|
|
3
|
+
Weatherb comes from weather + rb (ruby file extension). It's basically a Ruby gem for retrieving data about weather using the [ClimaCell](https://www.climacell.co/) [API](https://www.climacell.co/weather-api/) in a simple way.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'weatherb'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle install
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install weatherb
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
To be able to use this gem, you need a ClimaCell account. [Sign in](https://developer.climacell.co/sign-in) to your ClimaCell account and visit the [dashboard](https://developer.climacell.co/dashboard/overview), there you'll find your API key.
|
|
24
|
+
```ruby
|
|
25
|
+
weatherb = Weatherb::API.new('this-is-your-api-key')
|
|
26
|
+
|
|
27
|
+
# Return a observational data at the present time
|
|
28
|
+
realtime = weatherb.realtime(lat: 40.784449, lon: -73.965208)
|
|
29
|
+
|
|
30
|
+
# Return a forecast data on a minute-by-minute basis
|
|
31
|
+
nowcast = weatherb.nowcast(lat: 40.784449, lon: -73.965208)
|
|
32
|
+
|
|
33
|
+
# Return a forecast data on an hourly basis
|
|
34
|
+
hourly = weatherb.hourly(lat: 40.784449, lon: -73.965208)
|
|
35
|
+
|
|
36
|
+
# Return a forecast data on a daily basis
|
|
37
|
+
daily = weatherb.daily(lat: 40.784449, lon: -73.965208)
|
|
38
|
+
```
|
|
39
|
+
The example above uses the [default values](DEFAULT_VALUES.md), you can also add some additional parameters.
|
|
40
|
+
```ruby
|
|
41
|
+
# You can change the unit system into Royal (us), the default is Metric (si)
|
|
42
|
+
us_realtime = weatherb.realtime(lat: 40.784449, lon: -73.965208, unit_system: 'us')
|
|
43
|
+
|
|
44
|
+
# You can also specify the return field(s)
|
|
45
|
+
temp__humidity_hourly = weatherb.hourly(lat: 40.784449, lon: -73.965208, fields: ['temp', 'humidity'])
|
|
46
|
+
|
|
47
|
+
# And timestep for nowcast
|
|
48
|
+
ten_minutes_nowcast = weatherb.nowcast(lat: 40.784449, lon: -73.965208, timestep: 10)
|
|
49
|
+
|
|
50
|
+
# Also start time and end time for nowcast, hourly, and daily API
|
|
51
|
+
next_time_daily = weatherb.daily(lat: 40.784449, lon: -73.965208, start_time: '2020-06-16T23:59:00', end_time: '2020-06-20T12:00:00')
|
|
52
|
+
```
|
|
53
|
+
Find out more about the ClimeCell API in [their documentation](https://developer.climacell.co/v3/reference).
|
|
54
|
+
|
|
55
|
+
## Development
|
|
56
|
+
|
|
57
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
58
|
+
|
|
59
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
60
|
+
|
|
61
|
+
## Contributing
|
|
62
|
+
|
|
63
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/zackijack/weatherb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/zackijack/weatherb/blob/master/CODE_OF_CONDUCT.md).
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
69
|
+
|
|
70
|
+
## Code of Conduct
|
|
71
|
+
|
|
72
|
+
Everyone interacting in the Weatherb project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/zackijack/weatherb/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require 'bundler/setup'
|
|
5
|
+
require 'weatherb'
|
|
6
|
+
|
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
9
|
+
|
|
10
|
+
def reload!(print = true)
|
|
11
|
+
puts 'Reloading ...' if print
|
|
12
|
+
# Main project directory.
|
|
13
|
+
root_dir = File.expand_path('..', __dir__)
|
|
14
|
+
# Directories within the project that should be reloaded.
|
|
15
|
+
reload_dirs = %w[lib]
|
|
16
|
+
# Loop through and reload every file in all relevant project directories.
|
|
17
|
+
reload_dirs.each do |dir|
|
|
18
|
+
Dir.glob("#{root_dir}/#{dir}/**/*.rb").each { |f| load(f) }
|
|
19
|
+
end
|
|
20
|
+
# Return true when complete.
|
|
21
|
+
true
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
25
|
+
# require "pry"
|
|
26
|
+
# Pry.start
|
|
27
|
+
|
|
28
|
+
require 'irb'
|
|
29
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'faraday'
|
|
4
|
+
|
|
5
|
+
module FaradayMiddleware
|
|
6
|
+
class RaiseError < Faraday::Middleware
|
|
7
|
+
CLIENT_ERROR_STATUSES = (400...500).freeze
|
|
8
|
+
SERVER_ERROR_STATUSES = (500...600).freeze
|
|
9
|
+
|
|
10
|
+
def call(env)
|
|
11
|
+
@app.call(env).on_complete do |response|
|
|
12
|
+
case response[:status].to_i
|
|
13
|
+
when 400
|
|
14
|
+
raise Weatherb::BadRequest, error_message(response)
|
|
15
|
+
when 401
|
|
16
|
+
raise Weatherb::Unauthorized, error_message(response)
|
|
17
|
+
when 403
|
|
18
|
+
raise Weatherb::Forbidden, error_message(response)
|
|
19
|
+
when 429
|
|
20
|
+
raise Weatherb::TooManyRequests, error_message(response)
|
|
21
|
+
when 500
|
|
22
|
+
raise Weatherb::InternalServerError, error_message(response)
|
|
23
|
+
when CLIENT_ERROR_STATUSES
|
|
24
|
+
raise Weatherb::ClientError, error_message(response)
|
|
25
|
+
when SERVER_ERROR_STATUSES
|
|
26
|
+
raise Weatherb::ServerError, error_message(response)
|
|
27
|
+
when nil
|
|
28
|
+
raise Weatherb::NilStatusError, error_message(response)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def error_message(response)
|
|
36
|
+
"status: #{response[:status]}, message: #{error_body(response[:body])}"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def error_body(body)
|
|
40
|
+
body = ::JSON.parse(body) if !body.nil? && !body.empty? && body.is_a?(String)
|
|
41
|
+
|
|
42
|
+
body['message'].gsub("\n", '')
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
data/lib/weatherb.rb
ADDED
data/lib/weatherb/api.rb
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'faraday'
|
|
4
|
+
require 'faraday_middleware'
|
|
5
|
+
require 'faraday_middleware/raise_error'
|
|
6
|
+
module Weatherb
|
|
7
|
+
# API object manage the default properties for fulfilling an API request.
|
|
8
|
+
class API
|
|
9
|
+
API_URL = 'https://api.climacell.co/v3'
|
|
10
|
+
|
|
11
|
+
DEFAULT_FIELDS = %w[
|
|
12
|
+
temp
|
|
13
|
+
feels_like
|
|
14
|
+
humidity
|
|
15
|
+
wind_speed
|
|
16
|
+
wind_direction
|
|
17
|
+
baro_pressure
|
|
18
|
+
precipitation
|
|
19
|
+
sunrise
|
|
20
|
+
sunset
|
|
21
|
+
visibility
|
|
22
|
+
weather_code
|
|
23
|
+
].freeze
|
|
24
|
+
|
|
25
|
+
DEFAULT_REALTIME_FIELDS = %w[
|
|
26
|
+
dewpoint
|
|
27
|
+
wind_gust
|
|
28
|
+
precipitation_type
|
|
29
|
+
cloud_cover
|
|
30
|
+
cloud_base
|
|
31
|
+
cloud_ceiling
|
|
32
|
+
surface_shortwave_radiation
|
|
33
|
+
moon_phase
|
|
34
|
+
].freeze
|
|
35
|
+
|
|
36
|
+
DEFAULT_NOWCAST_FIELDS = %w[
|
|
37
|
+
dewpoint
|
|
38
|
+
wind_gust
|
|
39
|
+
precipitation_type
|
|
40
|
+
cloud_cover
|
|
41
|
+
cloud_base
|
|
42
|
+
cloud_ceiling
|
|
43
|
+
surface_shortwave_radiation
|
|
44
|
+
].freeze
|
|
45
|
+
|
|
46
|
+
DEFAULT_HOURLY_FIELDS = %w[
|
|
47
|
+
dewpoint
|
|
48
|
+
wind_gust
|
|
49
|
+
precipitation_type
|
|
50
|
+
precipitation_probability
|
|
51
|
+
cloud_cover
|
|
52
|
+
cloud_base
|
|
53
|
+
cloud_ceiling
|
|
54
|
+
surface_shortwave_radiation
|
|
55
|
+
moon_phase
|
|
56
|
+
].freeze
|
|
57
|
+
|
|
58
|
+
DEFAULT_DAILY_FIELDS = %w[
|
|
59
|
+
precipitation_probability
|
|
60
|
+
precipitation_accumulation
|
|
61
|
+
].freeze
|
|
62
|
+
|
|
63
|
+
SUCCESS_STATUSES = (200..300).freeze
|
|
64
|
+
|
|
65
|
+
# Create a new instance of the Weatherb::API using your API key.
|
|
66
|
+
# Also create Faraday connection with ParseJson middleware.
|
|
67
|
+
#
|
|
68
|
+
# @param api_key [String] ClimaCell API key.
|
|
69
|
+
def initialize(api_key)
|
|
70
|
+
@api_key = api_key
|
|
71
|
+
|
|
72
|
+
@connection = Faraday.new API_URL do |conn|
|
|
73
|
+
conn.response :json, content_type: /\bjson$/
|
|
74
|
+
|
|
75
|
+
conn.use FaradayMiddleware::RaiseError
|
|
76
|
+
|
|
77
|
+
conn.adapter Faraday.default_adapter
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Realtime (<= 1min out)
|
|
82
|
+
# The realtime call provides observational data at the present time, down
|
|
83
|
+
# to the minute, for a specific location. Information is available globally,
|
|
84
|
+
# with high-resolution data available for Japan Western Europe, India, and
|
|
85
|
+
# the US.
|
|
86
|
+
#
|
|
87
|
+
# @param lat [Float] Latitude, -59.9 to 59.9.
|
|
88
|
+
# @param lon [Float] Longitude, -180 to 180.
|
|
89
|
+
# @param unit_system [String] Unit system, "si" or "us".
|
|
90
|
+
# @param fields [Array<String>] Selected fields from ClimaCell data layer (such as "precipitation" or "wind_gust").
|
|
91
|
+
#
|
|
92
|
+
# @return [Hash]
|
|
93
|
+
def realtime(lat:, lon:, unit_system: 'si', fields: DEFAULT_FIELDS | DEFAULT_REALTIME_FIELDS)
|
|
94
|
+
path = 'weather/realtime'
|
|
95
|
+
|
|
96
|
+
query = {
|
|
97
|
+
apikey: @api_key,
|
|
98
|
+
lat: lat,
|
|
99
|
+
lon: lon,
|
|
100
|
+
unit_system: unit_system,
|
|
101
|
+
fields: fields
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
response = @connection.get(path, query)
|
|
105
|
+
response_body(response)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Nowcast (<= 360min out)
|
|
109
|
+
# The nowcast call provides forecasting data on a minute-by-minute basis,
|
|
110
|
+
# based on ClimaCell’s proprietary sensing technology and models.
|
|
111
|
+
#
|
|
112
|
+
# 6 hours of nowcasting is available for the US. 3 hours of precipitation
|
|
113
|
+
# and 6 hours of non-precipitation data is provided for Japan and Western
|
|
114
|
+
# Europe.
|
|
115
|
+
#
|
|
116
|
+
# @param lat [Float] Latitude, -87 to 89.
|
|
117
|
+
# @param lon [Float] Longitude, -180 to 180.
|
|
118
|
+
# @param unit_system [String] Unit system, "si" or "us".
|
|
119
|
+
# @param timestep [Integer] Time step in minutes, 1-60.
|
|
120
|
+
# @param start_time [String] Start time in ISO 8601 format "2019-03-20T14:09:50Z", or "now".
|
|
121
|
+
# @param end_time [String] End time in ISO 8601 format "2019-03-20T14:09:50Z".
|
|
122
|
+
# @param fields [Array<String>] Selected fields from ClimaCell data layer (such as "precipitation" or "wind_gust").
|
|
123
|
+
#
|
|
124
|
+
# @return [Hash]
|
|
125
|
+
def nowcast(lat:, lon:, unit_system: 'si', timestep: 1, start_time: 'now', end_time: nil, fields: DEFAULT_FIELDS | DEFAULT_NOWCAST_FIELDS)
|
|
126
|
+
path = 'weather/nowcast'
|
|
127
|
+
|
|
128
|
+
query = {
|
|
129
|
+
apikey: @api_key,
|
|
130
|
+
lat: lat,
|
|
131
|
+
lon: lon,
|
|
132
|
+
unit_system: unit_system,
|
|
133
|
+
timestep: timestep,
|
|
134
|
+
start_time: start_time,
|
|
135
|
+
end_time: end_time,
|
|
136
|
+
fields: fields
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
response = @connection.get(path, query)
|
|
140
|
+
response_body(response)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Hourly (<= 96hr out)
|
|
144
|
+
# The hourly call provides a global hourly forecast, up to 96 hours
|
|
145
|
+
# (4 days) out, for a specific location.
|
|
146
|
+
#
|
|
147
|
+
# @param lat [Float] Latitude, -87 to 89.
|
|
148
|
+
# @param lon [Float] Longitude, -180 to 180.
|
|
149
|
+
# @param unit_system [String] Unit system, "si" or "us".
|
|
150
|
+
# @param start_time [String] Start time in ISO 8601 format "2019-03-20T14:09:50Z", or "now".
|
|
151
|
+
# @param end_time [String] End time in ISO 8601 format "2019-03-20T14:09:50Z".
|
|
152
|
+
# @param fields [Array<String>] Selected fields from ClimaCell data layer (such as "precipitation" or "wind_gust").
|
|
153
|
+
#
|
|
154
|
+
# @return [Hash]
|
|
155
|
+
def hourly(lat:, lon:, unit_system: 'si', start_time: 'now', end_time: nil, fields: DEFAULT_FIELDS | DEFAULT_HOURLY_FIELDS)
|
|
156
|
+
path = 'weather/forecast/hourly'
|
|
157
|
+
|
|
158
|
+
query = {
|
|
159
|
+
apikey: @api_key,
|
|
160
|
+
lat: lat,
|
|
161
|
+
lon: lon,
|
|
162
|
+
unit_system: unit_system,
|
|
163
|
+
start_time: start_time,
|
|
164
|
+
end_time: end_time,
|
|
165
|
+
fields: fields
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
response = @connection.get(path, query)
|
|
169
|
+
response_body(response)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# Daily (<= 15d out)
|
|
173
|
+
# The daily API call provides a global daily forecast with summaries up to
|
|
174
|
+
# 15 days out.
|
|
175
|
+
#
|
|
176
|
+
# Note
|
|
177
|
+
# Daily results are returned and calculated based on 6am to 6am periods
|
|
178
|
+
# (meteorological timeframe) in UTC (GMT+0). Therefore, requesting forecast
|
|
179
|
+
# for locations with negative GMT offset may provide the first element with
|
|
180
|
+
# yesterday's date.
|
|
181
|
+
#
|
|
182
|
+
# @param lat [Float] Latitude, -87 to 89.
|
|
183
|
+
# @param lon [Float] Longitude, -180 to 180.
|
|
184
|
+
# @param unit_system [String] Unit system, "si" or "us".
|
|
185
|
+
# @param start_time [String] Start time in ISO 8601 format "2019-03-20T14:09:50Z", or "now".
|
|
186
|
+
# @param end_time [String] End time in ISO 8601 format "2019-03-20T14:09:50Z".
|
|
187
|
+
# @param fields [Array<String>] Selected fields from ClimaCell data layer (such as "precipitation" or "wind_gust").
|
|
188
|
+
#
|
|
189
|
+
# @return [Hash]
|
|
190
|
+
def daily(lat:, lon:, unit_system: 'si', start_time: 'now', end_time: nil, fields: DEFAULT_FIELDS | DEFAULT_DAILY_FIELDS)
|
|
191
|
+
path = 'weather/forecast/daily'
|
|
192
|
+
|
|
193
|
+
query = {
|
|
194
|
+
apikey: @api_key,
|
|
195
|
+
lat: lat,
|
|
196
|
+
lon: lon,
|
|
197
|
+
unit_system: unit_system,
|
|
198
|
+
start_time: start_time,
|
|
199
|
+
end_time: end_time,
|
|
200
|
+
fields: fields
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
response = @connection.get(path, query)
|
|
204
|
+
response_body(response)
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
private
|
|
208
|
+
|
|
209
|
+
def response_body(response)
|
|
210
|
+
if SUCCESS_STATUSES.member?(response.status)
|
|
211
|
+
response.body
|
|
212
|
+
else
|
|
213
|
+
{ 'status' => response.status, 'body' => response.body }
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Weatherb
|
|
4
|
+
# Custom error class for rescuing from all Weatherb errors
|
|
5
|
+
class Error < StandardError; end
|
|
6
|
+
|
|
7
|
+
# Raised when ClimaCell returns the HTTP status code 400
|
|
8
|
+
class BadRequest < Error; end
|
|
9
|
+
|
|
10
|
+
# Raised when ClimaCell returns the HTTP status code 401
|
|
11
|
+
class Unauthorized < Error; end
|
|
12
|
+
|
|
13
|
+
# Raised when ClimaCell returns the HTTP status code 403
|
|
14
|
+
class Forbidden < Error; end
|
|
15
|
+
|
|
16
|
+
# Raised when ClimaCell returns the HTTP status code 429
|
|
17
|
+
class TooManyRequests < Error; end
|
|
18
|
+
|
|
19
|
+
# Raised when ClimaCell returns the HTTP status code 500
|
|
20
|
+
class InternalServerError < Error; end
|
|
21
|
+
|
|
22
|
+
# Raised when ClimaCell returns the HTTP status code 4xx
|
|
23
|
+
class ClientError < Error; end
|
|
24
|
+
|
|
25
|
+
# Raised when ClimaCell returns the HTTP status code 5xx
|
|
26
|
+
class ServerError < Error; end
|
|
27
|
+
|
|
28
|
+
# Raised when ClimaCell returns the HTTP status code is nil
|
|
29
|
+
class NilStatusError < Error; end
|
|
30
|
+
end
|
data/weatherb.gemspec
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'lib/weatherb/version'
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = 'weatherb'
|
|
7
|
+
spec.version = Weatherb::VERSION
|
|
8
|
+
spec.authors = ['Zackky Muhammad']
|
|
9
|
+
spec.email = ['m.zackky@gmail.com']
|
|
10
|
+
|
|
11
|
+
spec.summary = 'A simple Ruby gem that can tell you all about the weather, superb!'
|
|
12
|
+
spec.description = 'Weatherb comes from weather + rb (ruby file extension).' \
|
|
13
|
+
" It's basically a Ruby gem for retrieving data about weather" \
|
|
14
|
+
' using the ClimaCell API in a simple way.'
|
|
15
|
+
spec.homepage = 'https://github.com/zackijack/weatherb'
|
|
16
|
+
spec.license = 'MIT'
|
|
17
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
|
|
18
|
+
|
|
19
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
|
20
|
+
spec.metadata['source_code_uri'] = 'https://github.com/zackijack/weatherb'
|
|
21
|
+
spec.metadata['changelog_uri'] = "https://github.com/zackijack/weatherb/releases/tag/v#{spec.version}"
|
|
22
|
+
|
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
25
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
26
|
+
end
|
|
27
|
+
spec.require_paths = ['lib']
|
|
28
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: weatherb
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Zackky Muhammad
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2020-06-17 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Weatherb comes from weather + rb (ruby file extension). It's basically
|
|
14
|
+
a Ruby gem for retrieving data about weather using the ClimaCell API in a simple
|
|
15
|
+
way.
|
|
16
|
+
email:
|
|
17
|
+
- m.zackky@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
extensions: []
|
|
20
|
+
extra_rdoc_files: []
|
|
21
|
+
files:
|
|
22
|
+
- ".editorconfig"
|
|
23
|
+
- ".gitignore"
|
|
24
|
+
- ".rspec"
|
|
25
|
+
- ".travis.yml"
|
|
26
|
+
- CODE_OF_CONDUCT.md
|
|
27
|
+
- DEFAULT_VALUES.md
|
|
28
|
+
- Gemfile
|
|
29
|
+
- Gemfile.lock
|
|
30
|
+
- LICENSE.txt
|
|
31
|
+
- README.md
|
|
32
|
+
- Rakefile
|
|
33
|
+
- bin/console
|
|
34
|
+
- bin/setup
|
|
35
|
+
- lib/faraday_middleware/raise_error.rb
|
|
36
|
+
- lib/weatherb.rb
|
|
37
|
+
- lib/weatherb/api.rb
|
|
38
|
+
- lib/weatherb/error.rb
|
|
39
|
+
- lib/weatherb/version.rb
|
|
40
|
+
- weatherb.gemspec
|
|
41
|
+
homepage: https://github.com/zackijack/weatherb
|
|
42
|
+
licenses:
|
|
43
|
+
- MIT
|
|
44
|
+
metadata:
|
|
45
|
+
homepage_uri: https://github.com/zackijack/weatherb
|
|
46
|
+
source_code_uri: https://github.com/zackijack/weatherb
|
|
47
|
+
changelog_uri: https://github.com/zackijack/weatherb/releases/tag/v0.1.0
|
|
48
|
+
post_install_message:
|
|
49
|
+
rdoc_options: []
|
|
50
|
+
require_paths:
|
|
51
|
+
- lib
|
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
|
+
requirements:
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: 2.4.0
|
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
requirements: []
|
|
63
|
+
rubygems_version: 3.1.3
|
|
64
|
+
signing_key:
|
|
65
|
+
specification_version: 4
|
|
66
|
+
summary: A simple Ruby gem that can tell you all about the weather, superb!
|
|
67
|
+
test_files: []
|