weatherizableAPI 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/.github/workflows/main.yml +19 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +58 -0
- data/LICENSE.txt +21 -0
- data/README.md +56 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/exe/weatherizableAPI +3 -0
- data/lib/open_weather_api/forecast.rb +52 -0
- data/lib/open_weather_api/messageable.rb +63 -0
- data/lib/open_weather_api/open_weather_api.rb +40 -0
- data/lib/open_weather_api/weather.rb +33 -0
- data/lib/weatherizableAPI.rb +8 -0
- data/lib/weatherizableAPI/version.rb +5 -0
- data/weatherizableAPI.gemspec +37 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cc9813780b621ab04fe68a7aedfb1e5c652a6bc809d8ba740d34e86a32791341
|
4
|
+
data.tar.gz: 6b92b8ee68e397b1d331e9c74706ab2125d195ed837c27314415838a751ec3ed
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 78a3a7731c5deff1f6ee3d4874bb7881bc64e8612f45a42a6a6f299af6894109dd0eceee3a90a3e50476c7d80cd3870cf8fac2b592927910d323a2e12d0aa898
|
7
|
+
data.tar.gz: 5424f25a4f15d6696f32f72bdeb815b50f1a1ba3fd5191778452ffbe9ebfa11c154064cd8675843f9272e780701ae7d32d62b84557f7773da6689489b13a1db2
|
@@ -0,0 +1,19 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on: [pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
steps:
|
9
|
+
- uses: actions/checkout@v2
|
10
|
+
- name: Set up Ruby
|
11
|
+
uses: ruby/setup-ruby@v1
|
12
|
+
with:
|
13
|
+
ruby-version: 2.7.0
|
14
|
+
- name: Run the default task
|
15
|
+
run: |
|
16
|
+
gem install bundler -v 2.2.11
|
17
|
+
bundle install
|
18
|
+
bundle exec rake
|
19
|
+
bundle exec rspec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
weatherizableAPI
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.7.0
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
weatherizableAPI (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (6.1.3)
|
10
|
+
activesupport (= 6.1.3)
|
11
|
+
activesupport (6.1.3)
|
12
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
+
i18n (>= 1.6, < 2)
|
14
|
+
minitest (>= 5.1)
|
15
|
+
tzinfo (~> 2.0)
|
16
|
+
zeitwerk (~> 2.3)
|
17
|
+
concurrent-ruby (1.1.8)
|
18
|
+
diff-lcs (1.4.4)
|
19
|
+
faraday (1.3.0)
|
20
|
+
faraday-net_http (~> 1.0)
|
21
|
+
multipart-post (>= 1.2, < 3)
|
22
|
+
ruby2_keywords
|
23
|
+
faraday-net_http (1.0.1)
|
24
|
+
i18n (1.8.9)
|
25
|
+
concurrent-ruby (~> 1.0)
|
26
|
+
minitest (5.14.3)
|
27
|
+
multipart-post (2.1.1)
|
28
|
+
rake (13.0.3)
|
29
|
+
rspec (3.10.0)
|
30
|
+
rspec-core (~> 3.10.0)
|
31
|
+
rspec-expectations (~> 3.10.0)
|
32
|
+
rspec-mocks (~> 3.10.0)
|
33
|
+
rspec-core (3.10.1)
|
34
|
+
rspec-support (~> 3.10.0)
|
35
|
+
rspec-expectations (3.10.1)
|
36
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
37
|
+
rspec-support (~> 3.10.0)
|
38
|
+
rspec-mocks (3.10.2)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.10.0)
|
41
|
+
rspec-support (3.10.2)
|
42
|
+
ruby2_keywords (0.0.4)
|
43
|
+
tzinfo (2.0.4)
|
44
|
+
concurrent-ruby (~> 1.0)
|
45
|
+
zeitwerk (2.4.2)
|
46
|
+
|
47
|
+
PLATFORMS
|
48
|
+
x86_64-linux
|
49
|
+
|
50
|
+
DEPENDENCIES
|
51
|
+
activemodel
|
52
|
+
faraday
|
53
|
+
rake (~> 13.0)
|
54
|
+
rspec (~> 3.0)
|
55
|
+
weatherizableAPI!
|
56
|
+
|
57
|
+
BUNDLED WITH
|
58
|
+
2.2.11
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Hugo Silva
|
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,56 @@
|
|
1
|
+
# WeatherizableAPI
|
2
|
+
|
3
|
+
This is a gem that aims to receive weather information according to the search options.
|
4
|
+
|
5
|
+
The main feature is to receive a descriptive text containing the current temperature and the weather forecast for the next 5 days. So you can prepare yourself and not be caught off guard by weather changes
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'weatherizableAPI'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install weatherizableAPI
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
First you need to create an account on [openweathermap](https://openweathermap.org/) and create an [API key](https://home.openweathermap.org/api_keys)
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'open_weather_api/messageable'
|
29
|
+
|
30
|
+
params = {
|
31
|
+
q: 'São Paulo',
|
32
|
+
units:'metric', # metric for ºC, imperial for ºF
|
33
|
+
lang:'pt_br',
|
34
|
+
appid: 'YOUR_API_KEY'
|
35
|
+
}
|
36
|
+
messageable = OpenWeatherApi::Messageable.new(@params)
|
37
|
+
messageable.prevision
|
38
|
+
=> "20.5ºC e nublado em 2021-02-22. Média para os proximos dias: 33.1ºC em 2021-02-23, 33.4ºC em 2021-02-24, 30.9ºC em 2021-02-25, 29.6ºC em 2021-02-26, 6.6ºC em 2021-02-27, "
|
39
|
+
|
40
|
+
```
|
41
|
+
|
42
|
+
###Note: In this release we only have answers in Brazilian Portuguese
|
43
|
+
|
44
|
+
## Development
|
45
|
+
|
46
|
+
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.
|
47
|
+
|
48
|
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/weatherizableAPI. 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/[USERNAME]/weatherizableAPI/blob/master/CODE_OF_CONDUCT.md).
|
53
|
+
|
54
|
+
## License
|
55
|
+
|
56
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "weatherizableAPI"
|
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
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
module OpenWeatherApi
|
5
|
+
class Forecast
|
6
|
+
include ActiveModel::Serializers::JSON
|
7
|
+
|
8
|
+
attr_accessor :id, :params, :attributes, :cod, :message, :cnt, :list, :city, :main
|
9
|
+
|
10
|
+
|
11
|
+
def attributes=(hash)
|
12
|
+
hash.each do |key, value|
|
13
|
+
send("#{key}=", value)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def attributes
|
18
|
+
instance_values
|
19
|
+
end
|
20
|
+
|
21
|
+
def error?
|
22
|
+
true unless cod.eq "200"
|
23
|
+
end
|
24
|
+
|
25
|
+
def error
|
26
|
+
message
|
27
|
+
end
|
28
|
+
|
29
|
+
def split_day(date)
|
30
|
+
day = []
|
31
|
+
list.each do |item|
|
32
|
+
if convert_date(item['dt']) == date
|
33
|
+
day << item
|
34
|
+
end
|
35
|
+
end
|
36
|
+
day
|
37
|
+
end
|
38
|
+
|
39
|
+
def average_temperature(date)
|
40
|
+
temp = 0
|
41
|
+
split_day(date).each do |item|
|
42
|
+
temp += item['main']['temp']
|
43
|
+
end
|
44
|
+
(temp / split_day(Date.today).count).round(1)
|
45
|
+
end
|
46
|
+
|
47
|
+
def convert_date(timestamp)
|
48
|
+
Time.at(timestamp).to_date
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'date'
|
3
|
+
require 'open_weather_api/open_weather_api'
|
4
|
+
|
5
|
+
module OpenWeatherApi
|
6
|
+
class Messageable
|
7
|
+
include ActiveModel::Serializers::JSON
|
8
|
+
|
9
|
+
def initialize(options = {})
|
10
|
+
@options = options
|
11
|
+
@forecast = open_weather_api.new(@options).forecast
|
12
|
+
@weather = open_weather_api.new(@options).weather
|
13
|
+
end
|
14
|
+
|
15
|
+
def forecast
|
16
|
+
@forecast
|
17
|
+
end
|
18
|
+
|
19
|
+
def weather
|
20
|
+
@weather
|
21
|
+
end
|
22
|
+
|
23
|
+
def prevision
|
24
|
+
"#{temp_now}. #{average_weather} "
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def open_weather_api
|
31
|
+
::OpenWeatherApi::OpenWeatherApi
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
def temp_type
|
37
|
+
case @options[:units]
|
38
|
+
when 'metric' then 'ºC'
|
39
|
+
when 'imperial' then 'ºF'
|
40
|
+
else ''
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def temp_now
|
45
|
+
"#{weather.main['temp'].round(1)}#{temp_type} e #{weather.weather.last['description']} em #{forecast.convert_date(weather.dt)}"
|
46
|
+
end
|
47
|
+
|
48
|
+
#FIXME: workarround to be fixed
|
49
|
+
def average_weather
|
50
|
+
message = "Média para os proximos dias: "
|
51
|
+
5.times do |t|
|
52
|
+
date = Date.today + 1 + t
|
53
|
+
message += forecast.average_temperature(date).to_s
|
54
|
+
message += temp_type
|
55
|
+
message += " em "
|
56
|
+
message += date.to_s
|
57
|
+
message += "#{t == 5 ? '.' : ',' } "
|
58
|
+
end
|
59
|
+
message
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'json'
|
3
|
+
require 'open_weather_api/weather'
|
4
|
+
require 'open_weather_api/forecast'
|
5
|
+
|
6
|
+
module OpenWeatherApi
|
7
|
+
class OpenWeatherApi
|
8
|
+
URL = ENV['OPEN_WEATER_API_URL'] || 'https://api.openweathermap.org/data/2.5/'
|
9
|
+
|
10
|
+
def initialize(options = {})
|
11
|
+
@options = options
|
12
|
+
end
|
13
|
+
|
14
|
+
def weather
|
15
|
+
url = URL+'weather'
|
16
|
+
@weather = ::OpenWeatherApi::Weather.new.from_json(get(url, @options).body)
|
17
|
+
@weather.params = @options
|
18
|
+
@weather
|
19
|
+
end
|
20
|
+
|
21
|
+
def forecast
|
22
|
+
url = URL+'forecast'
|
23
|
+
@forecast = ::OpenWeatherApi::Forecast.new.from_json(get(url, @options).body)
|
24
|
+
@forecast.params = @options
|
25
|
+
@forecast
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def get(url, params)
|
31
|
+
Faraday.get(url) do |req|
|
32
|
+
params.each do |key, value|
|
33
|
+
req.params[key] = value
|
34
|
+
end
|
35
|
+
req.headers['Accept'] = 'application/json'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
module OpenWeatherApi
|
4
|
+
class Weather
|
5
|
+
include ActiveModel::Serializers::JSON
|
6
|
+
|
7
|
+
attr_accessor :id, :coord, :weather, :main, :base, :visibility, :wind,
|
8
|
+
:clouds, :dt, :sys, :timezone, :name, :cod, :message, :params
|
9
|
+
|
10
|
+
def attributes=(hash)
|
11
|
+
hash.each do |key, value|
|
12
|
+
send("#{key}=", value)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def attributes
|
17
|
+
instance_values
|
18
|
+
end
|
19
|
+
|
20
|
+
def error?
|
21
|
+
true unless cod.eq 200
|
22
|
+
end
|
23
|
+
|
24
|
+
def error
|
25
|
+
message
|
26
|
+
end
|
27
|
+
|
28
|
+
def temperature
|
29
|
+
main['temp'].to_f
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/weatherizableAPI/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "weatherizableAPI"
|
7
|
+
spec.version = WeatherizableAPI::VERSION
|
8
|
+
spec.authors = ["Hugo Silva"]
|
9
|
+
spec.email = ["hugocardososilva@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "weatherizableAPI"
|
12
|
+
spec.description = "weatherizableAPI"
|
13
|
+
spec.homepage = "https://github.com/hugocardososilva/WeatherizableAPI"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
|
16
|
+
|
17
|
+
#spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
18
|
+
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
21
|
+
spec.metadata["changelog_uri"] = "https://github.com/hugocardososilva/WeatherizableAPI/blob/master/CHANGELOG.md"
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
26
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
# Uncomment to register a new dependency of your gem
|
33
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
34
|
+
|
35
|
+
# For more information and examples about making a new gem, checkout our
|
36
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: weatherizableAPI
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hugo Silva
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-02-22 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: weatherizableAPI
|
14
|
+
email:
|
15
|
+
- hugocardososilva@gmail.com
|
16
|
+
executables:
|
17
|
+
- weatherizableAPI
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".github/workflows/main.yml"
|
22
|
+
- ".gitignore"
|
23
|
+
- ".idea/.gitignore"
|
24
|
+
- ".idea/vcs.xml"
|
25
|
+
- ".rspec"
|
26
|
+
- ".ruby-gemset"
|
27
|
+
- ".ruby-version"
|
28
|
+
- CHANGELOG.md
|
29
|
+
- Gemfile
|
30
|
+
- Gemfile.lock
|
31
|
+
- LICENSE.txt
|
32
|
+
- README.md
|
33
|
+
- Rakefile
|
34
|
+
- bin/console
|
35
|
+
- bin/setup
|
36
|
+
- exe/weatherizableAPI
|
37
|
+
- lib/open_weather_api/forecast.rb
|
38
|
+
- lib/open_weather_api/messageable.rb
|
39
|
+
- lib/open_weather_api/open_weather_api.rb
|
40
|
+
- lib/open_weather_api/weather.rb
|
41
|
+
- lib/weatherizableAPI.rb
|
42
|
+
- lib/weatherizableAPI/version.rb
|
43
|
+
- weatherizableAPI.gemspec
|
44
|
+
homepage: https://github.com/hugocardososilva/WeatherizableAPI
|
45
|
+
licenses:
|
46
|
+
- MIT
|
47
|
+
metadata:
|
48
|
+
homepage_uri: https://github.com/hugocardososilva/WeatherizableAPI
|
49
|
+
source_code_uri: https://github.com/hugocardososilva/WeatherizableAPI
|
50
|
+
changelog_uri: https://github.com/hugocardososilva/WeatherizableAPI/blob/master/CHANGELOG.md
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 2.4.0
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubygems_version: 3.1.2
|
67
|
+
signing_key:
|
68
|
+
specification_version: 4
|
69
|
+
summary: weatherizableAPI
|
70
|
+
test_files: []
|