weatherfor 0.1.1 → 0.1.2

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: d91081045d7f71e401a73bb0dc6aa7cbd706e01a36ee249ecd7613752a6596bc
4
- data.tar.gz: 7d62a6c77ad4c31b081e06ef3a7dedf2a2e3817832ae3a37401bc1052bae78b8
3
+ metadata.gz: 8b4b91285f13701c20571438e8512fd6ae2a5f8eb622e7a7be34503bff366aed
4
+ data.tar.gz: 77651d9a2851a1c7ddf2d0ef31b63fa39f48ccec82f94ba050246a46f956250b
5
5
  SHA512:
6
- metadata.gz: d2fa9381e3eb2feb64523ccbeef5122633e62a7e1dcb2c9a8b6f83120c18679e33084e963fbd1abb33f0450e3b020c9d659b741db18c8f5629f267aa9b6a6ab9
7
- data.tar.gz: 01b1a2dde2cafd068a1c787b4b745c792f98f4efbc608f49a6ce8f0aa09749fc7dd97f21abe64276341e90b7fce5af1a5de9e8b4d92dc8740416bbe9dc10d9e6
6
+ metadata.gz: 47099721577ff9a0ce10a1baa1beb3cdb9f9f54684d9dfa920a5c454e680923e9acc4a064861baef308ae14663cf6c42da465d87bd9811b33c97c728dcf14254
7
+ data.tar.gz: de57fa381901230b6a056b0879ec4d36374a76cc1c8dc88caad73b977f26b7f6ac5a257fb42a90241c69fc3fed4a81de09fdb9f624247c431fc8f96b7d324b2e
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ AllCops:
2
+ NewCops: enable
data/Gemfile CHANGED
@@ -1,8 +1,11 @@
1
- source "https://rubygems.org"
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
2
4
  require 'net/http'
3
5
 
4
6
  # Specify your gem's dependencies in weatherfor.gemspec
5
7
  gemspec
6
8
 
7
- gem "rake", "~> 12.0"
8
- gem "rspec", "~> 3.0"
9
+ gem 'rake', '~> 12.0'
10
+ gem 'rspec', '~> 3.0'
11
+ gem 'rubocop', require: false
data/Gemfile.lock CHANGED
@@ -1,13 +1,20 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- weatherfor (0.1.1)
4
+ weatherfor (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ ast (2.4.2)
9
10
  diff-lcs (1.4.4)
11
+ parallel (1.20.1)
12
+ parser (3.0.1.0)
13
+ ast (~> 2.4.1)
14
+ rainbow (3.0.0)
10
15
  rake (12.3.3)
16
+ regexp_parser (2.1.1)
17
+ rexml (3.2.5)
11
18
  rspec (3.10.0)
12
19
  rspec-core (~> 3.10.0)
13
20
  rspec-expectations (~> 3.10.0)
@@ -21,6 +28,19 @@ GEM
21
28
  diff-lcs (>= 1.2.0, < 2.0)
22
29
  rspec-support (~> 3.10.0)
23
30
  rspec-support (3.10.2)
31
+ rubocop (1.12.1)
32
+ parallel (~> 1.10)
33
+ parser (>= 3.0.0.0)
34
+ rainbow (>= 2.2.2, < 4.0)
35
+ regexp_parser (>= 1.8, < 3.0)
36
+ rexml
37
+ rubocop-ast (>= 1.2.0, < 2.0)
38
+ ruby-progressbar (~> 1.7)
39
+ unicode-display_width (>= 1.4.0, < 3.0)
40
+ rubocop-ast (1.4.1)
41
+ parser (>= 2.7.1.5)
42
+ ruby-progressbar (1.11.0)
43
+ unicode-display_width (2.0.0)
24
44
 
25
45
  PLATFORMS
26
46
  ruby
@@ -28,6 +48,7 @@ PLATFORMS
28
48
  DEPENDENCIES
29
49
  rake (~> 12.0)
30
50
  rspec (~> 3.0)
51
+ rubocop
31
52
  weatherfor!
32
53
 
33
54
  BUNDLED WITH
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Weatherfor
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/weatherfor`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Welcome to Weatherfor gem!
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ The purpose of this Gem is to make API requests to OpenWeatherAPI to query meteorological data to process the average temperature for the next five days.
6
6
 
7
7
  ## Installation
8
8
 
@@ -22,8 +22,29 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ After you install the gem you'll have access to this methods:
26
26
 
27
+ To instance the gem class
28
+ ```ruby
29
+ req = Weatherfor::ApiConsultant.new(CityName, OpenWeatherApiKey)
30
+ ```
31
+
32
+ With this initialization you can check the parsed data:
33
+
34
+ Methodo to consult average temperature in days:
35
+ ```ruby
36
+ req.weather_in_days
37
+ ```
38
+
39
+ Method to consult the raw data:
40
+ ```ruby
41
+ req.list
42
+ ```
43
+
44
+ Method to consult the city request data:
45
+ ```ruby
46
+ req.list
47
+ ```
27
48
  ## Development
28
49
 
29
50
  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.
@@ -32,7 +53,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
53
 
33
54
  ## Contributing
34
55
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/weatherfor.
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/k41n3w/weatherfor.
36
57
 
37
58
 
38
59
  ## License
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
data/bin/console CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "weatherfor"
4
+ require 'bundler/setup'
5
+ require 'weatherfor'
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +11,5 @@ require "weatherfor"
10
11
  # require "pry"
11
12
  # Pry.start
12
13
 
13
- require "irb"
14
+ require 'irb'
14
15
  IRB.start(__FILE__)
data/lib/weatherfor.rb CHANGED
@@ -1,20 +1,65 @@
1
- require "weatherfor/version"
1
+ # frozen_string_literal: true
2
+
3
+ require 'weatherfor/version'
2
4
  require 'net/http'
3
5
  require 'json'
4
6
 
5
7
  module Weatherfor
6
8
  class Error < StandardError; end
7
-
9
+
10
+ # Make the API request and parse data
8
11
  class ApiConsultant
9
12
  def initialize(city, api_id)
10
- uri = URI("https://api.openweathermap.org/data/2.5/forecast?q=#{city}&appid=#{api_id}&lang=pt_br&units=metric")
11
- res = Net::HTTP.get_response(uri);nil
13
+ uri = url(city, api_id)
14
+ res = Net::HTTP.get_response(uri)
15
+
16
+ @data = case res.code
17
+ when '200'
18
+ JSON.parse(res.body)
19
+ else
20
+ { error: res.message }
21
+ end
22
+ end
23
+
24
+ def weather_in_days
25
+ avg_temp_in_days
26
+
27
+ "#{today_avg_temp}°C e #{current_temp_desc} em #{city_name} em #{current_date}." \
28
+ " Média para os próximos dias: #{parse_avg_text}"
29
+ end
30
+
31
+ def avg_temp_in_days
32
+ @arr = []
33
+ @json['list'].group_by { |item| Time.at(item['dt']).strftime('%m-%d-%Y') }.each do |date, data|
34
+ avg_temp = data.sum { |info| info['main']['temp'] }
35
+ avg_temp /= data.count
36
+ @arr << { avg_temp: avg_temp, date: date }
37
+ end
38
+ end
39
+
40
+ def today_avg_temp
41
+ @arr.first[:avg_temp].round(0)
42
+ end
12
43
 
13
- if res.code == '200'
14
- @json = JSON.parse res.body
15
- else
16
- return { 'error' => res.message }
44
+ def city_name
45
+ @json['city']['name']
46
+ end
47
+
48
+ def current_temp_desc
49
+ @json['list'][0]['weather'][0]['description']
50
+ end
51
+
52
+ def current_date
53
+ Time.now.strftime('%d/%m')
54
+ end
55
+
56
+ def parse_avg_text
57
+ text = ''
58
+ @arr.last(4).each_with_index do |item, index|
59
+ text += "#{item[:avg_temp].round(0)}°C em #{parse_date(item[:date])}"
60
+ text += index < 3 ? ', ' : '.'
17
61
  end
62
+ text
18
63
  end
19
64
 
20
65
  def city
@@ -25,36 +70,12 @@ module Weatherfor
25
70
  @json['list']
26
71
  end
27
72
 
28
- def weather_in_days
29
- arr = []
30
- @json['list'].group_by{ |item| Time.at(item['dt']).strftime("%m-%d-%Y") }.each do |date,data|
31
- avg_temp = data.sum { |info| info['main']['temp'] }
32
- avg_temp = avg_temp / 8
33
- arr << { avg_temp: avg_temp, date: date }
34
- end
35
-
36
- parse_avg_text(arr)
37
- end
38
-
39
- private
40
-
41
- def parse_avg_text(arr)
42
- current_temp = @json['list'][0]['main']['temp']
43
- city_name = @json['city']['name']
44
- weather_description = @json['list'][0]['weather'][0]['description']
45
- current_date = arr.first[:date].gsub('-', '/').delete_suffix('/2021')
46
- text = "#{current_temp.round(0)}°C e #{weather_description} em #{city_name} em #{current_date}. Média para os próximos dias: "
47
-
48
- arr.last(4).each_with_index do |item, index|
49
- text += "#{item[:avg_temp].round(0)}°C em #{item[:date].gsub('-', '/').delete_suffix('/2021')}"
50
- if index < 3
51
- text += ", "
52
- else
53
- text += "."
54
- end
55
- end
56
-
57
- text
73
+ def parse_date(date)
74
+ date.gsub('-', '/').delete_suffix('/2021')
75
+ end
76
+
77
+ def url(city, api_id)
78
+ URI("https://api.openweathermap.org/data/2.5/forecast?q=#{city}&appid=#{api_id}&lang=pt_br&units=metric")
58
79
  end
59
80
  end
60
81
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Weatherfor
2
- VERSION = "0.1.1"
4
+ VERSION = '0.1.2'
3
5
  end
data/weatherfor.gemspec CHANGED
@@ -1,31 +1,27 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'lib/weatherfor/version'
2
4
 
3
5
  Gem::Specification.new do |spec|
4
- spec.name = "weatherfor"
6
+ spec.name = 'weatherfor'
5
7
  spec.version = Weatherfor::VERSION
6
- spec.authors = ["Caio Ramos"]
7
- spec.email = ["kaineo@hotmai.l."]
8
+ spec.authors = ['Caio Ramos']
9
+ spec.email = ['kaineo@hotmai.l.']
8
10
 
9
11
  spec.summary = 'Api to check the average weather forecast for the next five days'
10
12
  spec.description = 'Sumario'
11
- spec.homepage = "https://github.com/k41n3w/weatherfor"
12
- spec.license = "MIT"
13
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
-
15
- if spec.respond_to?(:metadata)
16
- # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
17
- else
18
- raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
19
- end
13
+ spec.homepage = 'https://github.com/k41n3w/weatherfor'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 2.4.0'
20
16
 
21
- spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata['homepage_uri'] = spec.homepage
22
18
 
23
19
  # Specify which files should be added to the gem when it is released.
24
20
  # 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('..', __FILE__)) do
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
22
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
23
  end
28
- spec.bindir = "exe"
24
+ spec.bindir = 'exe'
29
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
- spec.require_paths = ["lib"]
26
+ spec.require_paths = ['lib']
31
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weatherfor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caio Ramos
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-13 00:00:00.000000000 Z
11
+ date: 2021-04-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Sumario
14
14
  email:
@@ -19,6 +19,7 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - ".gitignore"
21
21
  - ".rspec"
22
+ - ".rubocop.yml"
22
23
  - ".travis.yml"
23
24
  - Gemfile
24
25
  - Gemfile.lock
@@ -43,7 +44,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
43
44
  requirements:
44
45
  - - ">="
45
46
  - !ruby/object:Gem::Version
46
- version: 2.3.0
47
+ version: 2.4.0
47
48
  required_rubygems_version: !ruby/object:Gem::Requirement
48
49
  requirements:
49
50
  - - ">="