tropical 0.1.3 → 0.1.8

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,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 620cccda1d355f7b62533133cdb0d00fbade67391fd5e9458227ab5d96db0e61
4
- data.tar.gz: ea585a571761290cebc3d4c3e8b184a548cd2bdb727819259486b807af8ee172
3
+ metadata.gz: a02e1d2772e6ea9d2d5cfe3238daf51e7880e0e47aa4ea93b80bf06e99c86868
4
+ data.tar.gz: c9f2fb4a9d13e10a6bfd12eea9b1671190068a75f019ae8330782eea269d8c79
5
5
  SHA512:
6
- metadata.gz: 73eb9c2b1f232d34e5abcfce441551b982f1db7756234725704dfa3969006c340cccd5bf19a48f81019ca7036e030944a3f399598902cc2283ce394fd885d6ef
7
- data.tar.gz: 9ab9a9e3af21330494156356f0672e973d3f7adf687dc66520d54f7c59657fbd37ee48fc775b544851efc018e1725909e0435ae59298482d38638016c2bdfcb2
6
+ metadata.gz: 9e5cc4b58e8b16e6dfbfae6c4d575688bedca5ea6f32c789f49c55af29ddb9550ac4bdd2171aebfc15b245fe614c885392fecd8da31a944bc1db93a7ee12d4c9
7
+ data.tar.gz: f70a42ff3bd9092a81aa41e4842ec1b1af0416227d45de1561b6e0514af92604cc9bb56f7fdda88ee7f8466b1ee12c1d4c90a08e2740f5d6ff27126780aaa758
data/CODE_OF_CONDUCT.md CHANGED
@@ -69,8 +69,6 @@ Community leaders will follow these Community Impact Guidelines in determining t
69
69
 
70
70
  **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
71
 
72
- **Consequence**: A permanent ban from any sort of public interaction within the community.
73
-
74
72
  ## Attribution
75
73
 
76
74
  This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
data/Gemfile.lock CHANGED
@@ -1,13 +1,24 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tropical (0.1.3)
4
+ tropical (0.1.8)
5
+ activesupport (~> 6.1.2.1)
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
8
9
  specs:
10
+ activesupport (6.1.2.1)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 1.6, < 2)
13
+ minitest (>= 5.1)
14
+ tzinfo (~> 2.0)
15
+ zeitwerk (~> 2.3)
9
16
  ast (2.4.2)
17
+ concurrent-ruby (1.1.8)
10
18
  diff-lcs (1.4.4)
19
+ i18n (1.8.9)
20
+ concurrent-ruby (~> 1.0)
21
+ minitest (5.14.3)
11
22
  parallel (1.20.1)
12
23
  parser (3.0.0.0)
13
24
  ast (~> 2.4.1)
@@ -40,7 +51,10 @@ GEM
40
51
  rubocop-ast (1.4.1)
41
52
  parser (>= 2.7.1.5)
42
53
  ruby-progressbar (1.11.0)
54
+ tzinfo (2.0.4)
55
+ concurrent-ruby (~> 1.0)
43
56
  unicode-display_width (2.0.0)
57
+ zeitwerk (2.4.2)
44
58
 
45
59
  PLATFORMS
46
60
  x86_64-linux
data/README.md CHANGED
@@ -1,18 +1,13 @@
1
1
  # Tropical
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tropical`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Gem to consultation weather forecast up to 5 days.
6
4
 
7
5
  ## Installation
8
6
 
9
7
  Add this line to your application's Gemfile:
10
8
 
11
9
  ```ruby
12
- gem 'tropical'
13
-
14
- # or
15
- gem 'tropical', git: 'https://github.com/valterandrei/tropical', branch: 'main'
10
+ gem 'tropical', '~> 0.1.8'
16
11
  ```
17
12
 
18
13
  And then execute:
@@ -21,7 +16,105 @@ And then execute:
21
16
 
22
17
  ## Usage
23
18
 
24
- TODO: Write usage instructions here
19
+ Ruby API
20
+ ```ruby
21
+ tropical = Tropical::OpenWeatherMap.new(
22
+ {
23
+ appid: 'your_api_key', # required
24
+ q: 'São Paulo', # required
25
+ lang: 'pt_br', # optional
26
+ units: 'metric', # optional
27
+ cnt: '50' # optional
28
+ }
29
+ )
30
+ ```
31
+ For more information about the params: [openweathermap](https://openweathermap.org/forecast5)
32
+
33
+ Note: The `mode` param is not avaible.
34
+
35
+ - City name
36
+ ```ruby
37
+ tropical.city_name
38
+
39
+ # => "São Paulo"
40
+ ```
41
+
42
+ - Population
43
+ ```ruby
44
+ tropical.population
45
+
46
+ # => 10021295
47
+ ```
48
+
49
+ - Country
50
+ ```ruby
51
+ tropical.country
52
+
53
+ # => "BR"
54
+ ```
55
+
56
+ - Timezone
57
+ ```ruby
58
+ tropical.timezone
59
+
60
+ # => -3
61
+ ```
62
+
63
+ - Current date
64
+ ```ruby
65
+ tropical.current_date
66
+
67
+ # => 2021-02-14 18:00:00 -0300
68
+ ```
69
+
70
+ - Coord
71
+ ```ruby
72
+ tropical.coord
73
+
74
+ # => {:lat=>-23.5475, :lon=>-46.6361}
75
+ ```
76
+
77
+ - Scale
78
+ ```ruby
79
+ tropical.scale
80
+
81
+ # => "°C"
82
+ ```
83
+
84
+ - Current temperature
85
+ ```ruby
86
+ tropical.current_temp
87
+
88
+ # => 26.92
89
+ ```
90
+
91
+ - Current weather
92
+ ```ruby
93
+ tropical.current_weather
94
+
95
+ # => "chuva moderada"
96
+ ```
97
+
98
+ - Full sumary
99
+ ```ruby
100
+ tropical.full_sumary
101
+
102
+ # => "27°C e chuva moderada em São Paulo em 14/02. Média para os próximos dias: 26°C em 14/02, 26°C em 15/02, 26°C em 16/02, 26°C em 17/02, 25°C em 18/02 e 24°C em 19/02."
103
+ ```
104
+
105
+ - Sumary current day
106
+ ```ruby
107
+ tropical.sumary_current_day
108
+
109
+ # => "27°C e chuva moderada em São Paulo em 14/02."
110
+ ```
111
+
112
+ - Sumary days forecast
113
+ ```ruby
114
+ tropical.sumary_days_forecast
115
+
116
+ # => "26°C em 14/02, 26°C em 15/02, 26°C em 16/02, 26°C em 17/02, 25°C em 18/02 e 24°C em 19/02."
117
+ ```
25
118
 
26
119
  ## Development
27
120
 
data/lib/tropical.rb CHANGED
@@ -1,5 +1,7 @@
1
+ require "active_support/all"
1
2
  require "date"
2
3
  require "json"
4
+ require "i18n"
3
5
  require "net/http"
4
6
  require "uri"
5
7
 
@@ -7,34 +9,37 @@ module Tropical
7
9
  class OpenWeatherMap
8
10
  BASE_URL = "https://api.openweathermap.org/data/2.5/forecast?".freeze
9
11
 
10
- attr_reader :data, :status
12
+ attr_reader :data, :params, :status
11
13
 
12
14
  def initialize(params)
13
- request_params = build_request_params(params)
14
- response = post(request_params)
15
+ @params = params
16
+ response = post(request_params)
15
17
 
16
18
  load_data(response)
17
19
  end
18
20
 
19
- def average_temp_by_days
20
- group_by_days = list.group_by { |item| item[:datetime].to_date }
21
- days = []
21
+ def city_name
22
+ data["city"]["name"]
23
+ end
22
24
 
23
- group_by_days.each do |day, temps|
24
- average = temps.sum { |time| time[:temp] } / temps.length
25
+ def country
26
+ data["city"]["country"]
27
+ end
25
28
 
26
- days << { day: day, average: average.round }
27
- end
29
+ def population
30
+ data["city"]["population"]
31
+ end
28
32
 
29
- days
33
+ def timezone
34
+ Time.at(data["city"]["timezone"]).zone.to_i
30
35
  end
31
36
 
32
- def city
33
- data["city"]["name"]
37
+ def coord
38
+ data["city"]["coord"].transform_keys(&:to_sym)
34
39
  end
35
40
 
36
41
  def current_date
37
- list.first[:datetime]
42
+ list.first[:dt]
38
43
  end
39
44
 
40
45
  def current_temp
@@ -45,56 +50,49 @@ module Tropical
45
50
  list.first[:description]
46
51
  end
47
52
 
48
- def list
49
- data["list"].map do |list_item|
50
- {
51
- datetime: Time.at(list_item["dt"]),
52
- temp: list_item["main"]["temp"],
53
- description: list_item["weather"].first["description"]
54
- }
55
- end
53
+ def scale
54
+ units = params[:units]
55
+
56
+ return "°C" if units == "metric"
57
+ return "°F" if units == "imperial"
58
+
59
+ "°K"
60
+ end
61
+
62
+ def full_sumary
63
+ "#{sumary_current_day} "\
64
+ "Média para os próximos dias: "\
65
+ "#{sumary_days_forecast}"
66
+ end
67
+
68
+ def sumary_current_day
69
+ "#{current_temp.round}#{scale} e #{current_weather} em "\
70
+ "#{city_name} em #{current_date.strftime("%d/%m")}."
56
71
  end
57
72
 
58
- def sumary
59
- message = format_message
73
+ def sumary_days_forecast
74
+ list = average_temp_by_days.map do |x|
75
+ "#{x[:average]}#{scale} em #{x[:day].strftime("%d/%m")}"
76
+ end
60
77
 
61
- "#{message.current_temp}°C e #{current_weather} em #{city} em #{message.current_date}. "\
62
- "Média para os próximos dias: #{message.first_day_average}°C em #{message.first_day_date}, "\
63
- "#{message.second_day_average}°C em #{message.second_day_date}, "\
64
- "#{message.third_day_average}°C em #{message.third_day_date}, "\
65
- "#{message.fourth_day_average}°C em #{message.fourth_day_date} "\
66
- "e #{message.fifth_day_average}°C em #{message.fifth_day_date}."
78
+ "#{list.to_sentence(words_connector: ", ", last_word_connector: " e ")}."
67
79
  end
68
80
 
69
81
  private
70
82
 
71
- def build_request_params(params)
83
+ def request_params
72
84
  link = ""
73
85
 
74
86
  params.each do |k, v|
75
- link += "&#{k}=#{v}" if v.is_a?(String) && !v.empty?
87
+ next if k == :mode
88
+ next unless v.is_a?(String) && v.present?
89
+
90
+ link += "&#{k}=#{I18n.transliterate(v)}"
76
91
  end
77
92
 
78
93
  BASE_URL + link
79
94
  end
80
95
 
81
- def format_message
82
- OpenStruct.new(
83
- current_temp: current_temp.round,
84
- current_date: current_date.strftime("%d/%m"),
85
- first_day_average: average_temp_by_days[0][:average],
86
- first_day_date: average_temp_by_days[0][:day].strftime("%d/%m"),
87
- second_day_average: average_temp_by_days[1][:average],
88
- second_day_date: average_temp_by_days[1][:day].strftime("%d/%m"),
89
- third_day_average: average_temp_by_days[2][:average],
90
- third_day_date: average_temp_by_days[2][:day].strftime("%d/%m"),
91
- fourth_day_average: average_temp_by_days[3][:average],
92
- fourth_day_date: average_temp_by_days[3][:day].strftime("%d/%m"),
93
- fifth_day_average: average_temp_by_days[4][:average],
94
- fifth_day_date: average_temp_by_days[4][:day].strftime("%d/%m")
95
- )
96
- end
97
-
98
96
  def load_data(response)
99
97
  @status = response.code
100
98
  @data = case response
@@ -107,6 +105,29 @@ module Tropical
107
105
  end
108
106
  end
109
107
 
108
+ def average_temp_by_days
109
+ group_by_days = list.group_by { |item| item[:dt].to_date }
110
+ days = []
111
+
112
+ group_by_days.each do |day, temps|
113
+ average = temps.sum { |time| time[:temp] } / temps.length
114
+
115
+ days << { day: day, average: average.round }
116
+ end
117
+
118
+ days
119
+ end
120
+
121
+ def list
122
+ data["list"].map do |list_item|
123
+ {
124
+ dt: Time.at(list_item["dt"]),
125
+ temp: list_item["main"]["temp"],
126
+ description: list_item["weather"].first["description"]
127
+ }
128
+ end
129
+ end
130
+
110
131
  def post(request_params)
111
132
  url = URI(request_params)
112
133
  http = Net::HTTP.new(url.host, url.port)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tropical
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.8"
5
5
  end
data/tropical.gemspec CHANGED
@@ -27,8 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ["lib"]
29
29
 
30
- # Uncomment to register a new dependency of your gem
31
- # spec.add_dependency "example-gem", "~> 1.0"
30
+ spec.add_dependency "activesupport", "~> 6.1.2.1"
32
31
 
33
32
  # For more information and examples about making a new gem, checkout our
34
33
  # guide at: https://bundler.io/guides/creating_gem.html
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tropical
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Valter
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-13 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2021-02-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.1.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 6.1.2.1
13
27
  description: Consultation weather forecast up to 5 days.
14
28
  email:
15
29
  - valterandrey@gmail.com