open_weather_raoni 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7e4255f726b518acaead55e8cd40e44b6466a954182da3af26274643953bb00a
4
+ data.tar.gz: 3643cae5e9b4157e82e7d1d0a02cfd40f35df4315534b12cc43aa3a271b569e2
5
+ SHA512:
6
+ metadata.gz: ed3a6fcefffb1727b6d160721d6acb1978226d798e5162376933a9c98fd33e75ac01a4642c7a85570c51caead307a6efbadb605a013ea0c9aa26e10d48c4ecce
7
+ data.tar.gz: c5fad6ea6bc710d912cf50d179aa0173f1b39b862652308aebaa8e81992113f342647b4794ff25e5717641288b5a472ae1495e3dcae984c2565d9e0d5802eb88
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.2
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in open_weather_raoni.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ open_weather_raoni (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.7.0)
10
+ public_suffix (>= 2.0.2, < 5.0)
11
+ crack (0.4.5)
12
+ rexml
13
+ hashdiff (1.0.1)
14
+ json (2.5.1)
15
+ minitest (5.14.4)
16
+ public_suffix (4.0.6)
17
+ rake (13.0.3)
18
+ rexml (3.2.4)
19
+ webmock (3.12.0)
20
+ addressable (>= 2.3.6)
21
+ crack (>= 0.3.2)
22
+ hashdiff (>= 0.4.0, < 2.0.0)
23
+
24
+ PLATFORMS
25
+ ruby
26
+ x86_64-linux
27
+
28
+ DEPENDENCIES
29
+ json (~> 2.5.1)
30
+ minitest (~> 5.0)
31
+ open_weather_raoni!
32
+ rake (~> 13.0)
33
+ webmock (~> 3.12.0)
34
+
35
+ BUNDLED WITH
36
+ 2.2.11
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Raoni Renosto Coelho
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
+ # OpenWeatherRaoni
2
+
3
+ This gem has two features:
4
+ - Retrieve the current forecast for a given city
5
+ - Retrieve the next 5 days forecast for a given city.
6
+
7
+ This gem uses the Open Weather Map API (https://openweathermap.org)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'open_weather_raoni'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install open_weather_raoni
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ require "open_weather_raoni/open_weather_map_api"
29
+ ```
30
+
31
+ ```ruby
32
+ open_weather_api = OpenWeatherRaoni::OpenWeatherMapApi.new #your_open_weather_map_key
33
+ ```
34
+
35
+ Get a city current weather:
36
+ ```ruby
37
+ forecast = open_weather_api.current_weather "pato branco,BR"
38
+
39
+ puts forecast[:temperature]
40
+ puts forecast[:weather]
41
+ ```
42
+
43
+ Get a city next 5 days forecast:
44
+ ```ruby
45
+ result = open_weather_api.next_five_days_forecast "pato branco,BR"
46
+
47
+ result[:list].each do |forecast|
48
+ puts forecast[:temperature_average]
49
+ puts forecast[:date]
50
+ end
51
+ ```
52
+
53
+ Handling API errors:
54
+ ```ruby
55
+ if result[:error]
56
+ puts result[:message]
57
+ end
58
+ ```
59
+
60
+ ## Development
61
+
62
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
63
+
64
+ 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).
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/raonirenosto/open-weather-gem.
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
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 "open_weather_raoni"
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,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
@@ -0,0 +1,5 @@
1
+ require_relative "open_weather_raoni/version"
2
+ require "open_weather_raoni/open_weather_map_api"
3
+
4
+ module OpenWeatherRaoni
5
+ end
@@ -0,0 +1,134 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'openssl'
4
+ require "json"
5
+
6
+ module OpenWeatherRaoni
7
+ class OpenWeatherApiError < StandardError;
8
+ def initialize(message)
9
+ super(message)
10
+ end
11
+ end
12
+
13
+ class OpenWeatherMapApi
14
+
15
+ def initialize key
16
+ @api_key = key
17
+ @api_link = "https://community-open-weather-map.p.rapidapi.com"
18
+ end
19
+
20
+ def next_five_days_forecast city
21
+ response = ""
22
+ begin
23
+ response = api_five_day_weather city
24
+ rescue OpenWeatherRaoni::OpenWeatherApiError => e
25
+ e.inspect
26
+ return {
27
+ :error => true,
28
+ :message => e.message
29
+ }
30
+ end
31
+ forecasts = response["list"]
32
+ parse_five_days_result forecasts
33
+ end
34
+
35
+ def current_weather city
36
+ response = ""
37
+ begin
38
+ response = api_current_weather city
39
+ rescue OpenWeatherApiError => e
40
+ return {
41
+ :error => true,
42
+ :message => e.message
43
+ }
44
+ end
45
+ parse_current_weather response
46
+ end
47
+
48
+ def api_current_weather city
49
+ endpoint = "/weather"
50
+ city_param = "?q=#{city}"
51
+ unit_param = "&units=metric"
52
+ language = "&lang=pt_br"
53
+ composed_url = @api_link + endpoint + city_param + unit_param + language
54
+
55
+ call_open_weather_api composed_url
56
+ end
57
+
58
+ def api_five_day_weather city
59
+ endpoint = "/forecast"
60
+ city_param = "?q=#{city}"
61
+ unit_param = "&units=metric"
62
+ composed_url = @api_link + endpoint + city_param + unit_param
63
+
64
+ call_open_weather_api composed_url
65
+ end
66
+
67
+ def call_open_weather_api composed_url
68
+ url = URI(composed_url)
69
+
70
+ http = Net::HTTP.new(url.host, url.port)
71
+ http.use_ssl = true
72
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
73
+
74
+ request = Net::HTTP::Get.new(url)
75
+ request["x-rapidapi-key"] = @api_key
76
+ request["x-rapidapi-host"] = 'community-open-weather-map.p.rapidapi.com'
77
+
78
+ response = http.request(request)
79
+
80
+ body = ""
81
+ if response.code != "500"
82
+ body = JSON.parse(response.read_body)
83
+ if response.code != "200"
84
+ raise OpenWeatherRaoni::OpenWeatherApiError.new body["message"]
85
+ end
86
+ else
87
+ raise OpenWeatherRaoni::OpenWeatherApiError.new "Could not reach server"
88
+ end
89
+
90
+ return body
91
+ end
92
+
93
+
94
+ def parse_current_weather response
95
+ return {
96
+ :error => false,
97
+ :temperature => response["main"]["temp"],
98
+ :weather => response["weather"][0]["description"]
99
+ }
100
+ end
101
+
102
+ def parse_five_days_result list
103
+ forecasts = []
104
+
105
+ grouped_list = list.group_by { |g| g["dt_txt"].split[0] }
106
+
107
+ # ignore today
108
+ if (grouped_list.size > 5)
109
+ grouped_list.shift
110
+ end
111
+
112
+ grouped_list.each do |date, list|
113
+ temperature_average = calculate_temperature_average list
114
+ forecast_hash = {
115
+ :temperature_average => temperature_average,
116
+ :date => date
117
+ }
118
+ forecasts << forecast_hash
119
+ end
120
+
121
+ response = {
122
+ :error => false,
123
+ :list => forecasts
124
+ }
125
+
126
+ return response
127
+ end
128
+
129
+ def calculate_temperature_average list
130
+ temperature_sum = list.sum {|item| item["main"]["temp"]}
131
+ return (temperature_sum / list.size).floor(2)
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenWeatherRaoni
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/open_weather_raoni/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "open_weather_raoni"
7
+ spec.version = OpenWeatherRaoni::VERSION
8
+ spec.authors = ["Raoni Renosto Coelho"]
9
+ spec.email = ["raonicoelho@gmail.com"]
10
+
11
+ spec.summary = "Connect to OpenWeatherMap SDK, get a city current temperature, and for the next 5 days"
12
+ spec.homepage = "https://github.com/raonirenosto/open-weather-gem"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/raonirenosto/open-weather-gem"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ # Uncomment to register a new dependency of your gem
29
+ # spec.add_dependency "example-gem", "~> 1.0"
30
+ spec.add_development_dependency "minitest", "~> 5.0"
31
+ spec.add_development_dependency "json", "~> 2.5.1"
32
+ spec.add_development_dependency "webmock", "~> 3.12.0"
33
+
34
+ # For more information and examples about making a new gem, checkout our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_weather_raoni
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Raoni Renosto Coelho
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-02-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.5.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.5.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: webmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.12.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.12.0
55
+ description:
56
+ email:
57
+ - raonicoelho@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".ruby-version"
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/open_weather_raoni.rb
72
+ - lib/open_weather_raoni/open_weather_map_api.rb
73
+ - lib/open_weather_raoni/version.rb
74
+ - open_weather_raoni.gemspec
75
+ homepage: https://github.com/raonirenosto/open-weather-gem
76
+ licenses:
77
+ - MIT
78
+ metadata:
79
+ homepage_uri: https://github.com/raonirenosto/open-weather-gem
80
+ source_code_uri: https://github.com/raonirenosto/open-weather-gem
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 2.4.0
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.1.4
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Connect to OpenWeatherMap SDK, get a city current temperature, and for the
100
+ next 5 days
101
+ test_files: []