weather_handler 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 932b86fc3dfa249cd24143a15528a69b24073d6ee8f4e6e9e9d950810751e6a8
4
- data.tar.gz: 52bbef68e094e955f313039c8092c86a3d9620f7b53a18557a15a84b762c704d
3
+ metadata.gz: acf49f41b812a13f406b6a56094b8b4c5be8f9a5573190c0a24633589fc343d1
4
+ data.tar.gz: 703e40cf12dfafcdc6f4e213494a4be4eaa89b0137a4749b6fb65c3d1e789d63
5
5
  SHA512:
6
- metadata.gz: d4e98e51c4ef51f756ea9d0f79e0442b03e90e29410f7ba77b1b41d48af52703a1ca3a3d1004ab6b06bccfdde899ef6dd53dc5fe16f4aac11f551396dc853ef7
7
- data.tar.gz: fef912cec5e714ce5ebf878e1f6ff6ffb783e595010fa8119fd4ff8763ab9840ff5adfe720c57b2e1cd547ecbf8c52cb9341649f001c60c06434140c673454f4
6
+ metadata.gz: 14c6524569a6336f247502da745cef01e6598f234f47a7d56e722898a68ab1342d2832cd759e89041df92ba2622399e0dca4ed494e2f12cadefe86eb2e143c83
7
+ data.tar.gz: 921896d617a58470d269afab691ed192593afad34d1faf77cefa571290cc3b6312122bbe41eac44a88957072856b6949915c13062666b1b411962a4b111c3b8d
data/Gemfile.lock CHANGED
@@ -1,7 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- weather_handler (0.1.0)
4
+ weather_handler (0.1.2)
5
+ rest-client (~> 2.1)
6
+ retryable (~> 3.0, >= 3.0.5)
5
7
 
6
8
  GEM
7
9
  remote: https://rubygems.org/
@@ -11,11 +13,11 @@ GEM
11
13
  unf (>= 0.0.5, < 1.0.0)
12
14
  dotenv (2.7.6)
13
15
  http-accept (1.7.0)
14
- http-cookie (1.0.3)
16
+ http-cookie (1.0.4)
15
17
  domain_name (~> 0.5)
16
18
  mime-types (3.3.1)
17
19
  mime-types-data (~> 3.2015)
18
- mime-types-data (3.2021.0225)
20
+ mime-types-data (3.2021.0704)
19
21
  netrc (0.11.0)
20
22
  rake (12.3.3)
21
23
  rest-client (2.1.0)
@@ -23,6 +25,7 @@ GEM
23
25
  http-cookie (>= 1.0.2, < 2.0)
24
26
  mime-types (>= 1.16, < 4.0)
25
27
  netrc (~> 0.8)
28
+ retryable (3.0.5)
26
29
  rspec (3.10.0)
27
30
  rspec-core (~> 3.10.0)
28
31
  rspec-expectations (~> 3.10.0)
@@ -46,7 +49,6 @@ PLATFORMS
46
49
  DEPENDENCIES
47
50
  dotenv (~> 2.7, >= 2.7.6)
48
51
  rake (~> 12.0)
49
- rest-client (~> 2.1)
50
52
  rspec (~> 3.0)
51
53
  weather_handler!
52
54
 
data/README.md CHANGED
@@ -24,8 +24,8 @@ After installation you new need to create instance of `WeatherHandler::Weather`
24
24
 
25
25
  For example: `weather = WeatherHandler::Weather.new('london')`. Now you have new instance of WeatherHandler::Weather which you can use to find out such information as:
26
26
 
27
- * current temperature (just call `weather.current_temperature('celsium')` and pass celsium, kelvin or fahrenheit argument dimension as string.)
28
- * feels like temperature (just call `weather.feels_like_temperature('fahrenheit')` and pass celsium, kelvin or fahrenheit argument dimension as string.)
27
+ * current temperature (just call `weather.current_temperature('celsium')` and pass celsium, kelvin or fahrenheit argument dimension as string. Default attribute is 'celsium'. So for this case you can use just as: `weather.current_temperature` and you will receive celsium value.
28
+ * feels like temperature (just call `weather.feels_like_temperature('fahrenheit')` and pass celsium, kelvin or fahrenheit argument dimension as string. Default attribute is 'celsium'. So for this case you can use just as: `weather.feels_like_temperature` and you will receive celsium value.
29
29
  * weather description (just call `weather.weather_description`)
30
30
  * humidity (just call `weather.humidity`)
31
31
  * pressure (just call `weather.pressure`)
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Adapters
4
+ class Weather
5
+ CONVERTATION_KELVIN_VALUE = 273.15
6
+ CONVERTATION_FAHRENHEIT_VALUE = 459.67
7
+ FAHRENHEIT_COEFFICIENT = 1.8
8
+ CELSIUM = 'celsium'
9
+ KELVIN = 'kelvin'
10
+ FAHRENHEIT = 'fahrenheit'
11
+ ERROR_MESSAGE = 'Please, try to type correct dimension. Correct types are: Celsium, Kelvin and Fahrenheit.'
12
+
13
+ def initialize(dimension, standard_amount)
14
+ @dimension = dimension
15
+ @standard_amount = standard_amount
16
+ end
17
+
18
+ def call
19
+ converter(dimension, standard_amount)
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :standard_amount, :dimension, :amount
25
+
26
+ def converter(dimension, quantity)
27
+ case dimension.downcase
28
+ when CELSIUM
29
+ "#{degrees_to_c(quantity)} C"
30
+ when KELVIN
31
+ "#{quantity} K"
32
+ when FAHRENHEIT
33
+ "#{degrees_to_f(quantity)} F"
34
+ else
35
+ ERROR_MESSAGE
36
+ end
37
+ end
38
+
39
+ def degrees_to_c(quantity)
40
+ (quantity - CONVERTATION_KELVIN_VALUE).round(1)
41
+ end
42
+
43
+ def degrees_to_f(quantity)
44
+ (quantity * FAHRENHEIT_COEFFICIENT - CONVERTATION_FAHRENHEIT_VALUE).round(1)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,23 @@
1
+ module Decorators
2
+ class OpenWeatherResponse
3
+ def initialize(data)
4
+ @data = data
5
+ end
6
+
7
+ def amount_in_kelvins
8
+ parsed_data['temp'].round(1)
9
+ end
10
+
11
+ def parsed_data
12
+ JSON.parse(data.body)['main']
13
+ end
14
+
15
+ def parsed_weather_description
16
+ JSON.parse(data.body)['weather'].first['description'].split.map(&:capitalize).join(' ')
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :data
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OpenWeatherClient
4
+ def initialize(city)
5
+ @city = city
6
+ end
7
+
8
+ def call
9
+ Decorators::OpenWeatherResponse.new(weather_data)
10
+ end
11
+
12
+ private
13
+
14
+ attr_reader :city
15
+
16
+ def weather_data
17
+ Retryable.retryable(tries: 5) do
18
+ @weather_data ||= RestClient.get(location_url)
19
+ end
20
+ end
21
+
22
+ def location_url
23
+ "api.openweathermap.org/data/2.5/weather?q=#{city}&appid=#{ENV['OPEN_WEATHER_API_KEY']}"
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module WeatherHandler
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -4,78 +4,45 @@ require 'weather_handler/version'
4
4
  require 'rest-client'
5
5
  require 'dotenv/load'
6
6
  require 'json'
7
+ require 'retryable'
8
+ require 'open_weather_client'
9
+ require 'adapters/weather'
10
+ require 'decorators/open_weather_response'
7
11
 
8
12
  module WeatherHandler
9
13
  class Weather
10
- CONVERTATION_KELVIN_VALUE = 273.15
11
- CONVERTATION_FAHRENHEIT_VALUE = 459.67
12
- FAHRENHEIT_COEFFICIENT = 1.8
13
- ERROR_MESSAGE = 'Please, try to type correct dimension. Correct types are: Celsium, Kelvin and Fahrenheit.'
14
14
  CELSIUM = 'celsium'
15
- KELVIN = 'kelvin'
16
- FAHRENHEIT = 'fahrenheit'
17
15
 
18
16
  def initialize(city)
19
- @city = city
17
+ @response = OpenWeatherClient.new(city).call
18
+ @converter = Adapters::Weather
19
+ @kelvin_amount = response.amount_in_kelvins
20
20
  end
21
21
 
22
- def current_temperature(dimension)
23
- converter(dimension)
22
+ def current_temperature(dimension = CELSIUM)
23
+ converter.new(dimension, kelvin_amount).call
24
24
  end
25
25
 
26
- def feels_like_temperature(dimension)
27
- amount = (JSON.parse(weather_data.body)['main']['feels_like']).round(1)
26
+ def feels_like_temperature(dimension = CELSIUM)
27
+ amount = (response.parsed_data['feels_like']).round(1)
28
28
 
29
- converter(dimension, amount)
29
+ converter.new(dimension, amount).call
30
30
  end
31
31
 
32
32
  def weather_description
33
- JSON.parse(weather_data.body)['weather'].first['description'].split.map(&:capitalize).join(' ')
33
+ response.parsed_weather_description
34
34
  end
35
35
 
36
36
  def humidity
37
- "#{JSON.parse(weather_data.body)['main']['humidity']} %"
37
+ "#{response.parsed_data['humidity']} %"
38
38
  end
39
39
 
40
40
  def pressure
41
- "#{JSON.parse(weather_data.body)['main']['pressure']} mb"
41
+ "#{response.parsed_data['pressure']} mb"
42
42
  end
43
43
 
44
44
  private
45
45
 
46
- attr_reader :city
47
-
48
- def weather_data
49
- @weather_data ||= RestClient.get(location_url)
50
- end
51
-
52
- def location_url
53
- "api.openweathermap.org/data/2.5/weather?q=#{city}&appid=#{ENV['OPEN_WEATHER_API_KEY']}"
54
- end
55
-
56
- def converter(dimension, quantity = degrees_in_k)
57
- case dimension.downcase
58
- when CELSIUM
59
- "#{degrees_to_c(quantity)} C"
60
- when KELVIN
61
- "#{quantity} K"
62
- when FAHRENHEIT
63
- "#{degrees_to_f(quantity)} F"
64
- else
65
- ERROR_MESSAGE
66
- end
67
- end
68
-
69
- def degrees_to_c(quantity)
70
- (quantity - CONVERTATION_KELVIN_VALUE).round(1)
71
- end
72
-
73
- def degrees_in_k
74
- (JSON.parse(weather_data.body)['main']['temp']).round(1)
75
- end
76
-
77
- def degrees_to_f(quantity)
78
- (quantity * FAHRENHEIT_COEFFICIENT - CONVERTATION_FAHRENHEIT_VALUE).round(1)
79
- end
46
+ attr_reader :response, :converter, :kelvin_amount
80
47
  end
81
48
  end
@@ -29,7 +29,8 @@ Gem::Specification.new do |spec|
29
29
  spec.add_development_dependency('rake', '~> 12.0')
30
30
  spec.add_development_dependency('rspec', '~> 3.0')
31
31
  spec.add_development_dependency('dotenv', '~> 2.7', '>= 2.7.6')
32
- spec.add_development_dependency('rest-client', '~> 2.1')
32
+ spec.add_dependency('rest-client', '~> 2.1')
33
+ spec.add_dependency('retryable', '~> 3.0', '>= 3.0.5')
33
34
 
34
35
  spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
35
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weather_handler
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
  - Ivan Viter
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-05-09 00:00:00.000000000 Z
11
+ date: 2021-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -65,13 +65,33 @@ dependencies:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
67
  version: '2.1'
68
- type: :development
68
+ type: :runtime
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
74
  version: '2.1'
75
+ - !ruby/object:Gem::Dependency
76
+ name: retryable
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 3.0.5
85
+ type: :runtime
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '3.0'
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 3.0.5
75
95
  description: This gem will find weather for your city
76
96
  email:
77
97
  - ivanviter@i.ua
@@ -92,6 +112,9 @@ files:
92
112
  - Rakefile
93
113
  - bin/console
94
114
  - bin/setup
115
+ - lib/adapters/weather.rb
116
+ - lib/decorators/open_weather_response.rb
117
+ - lib/open_weather_client.rb
95
118
  - lib/weather_handler.rb
96
119
  - lib/weather_handler/version.rb
97
120
  - weather_handler.gemspec
@@ -118,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
141
  - !ruby/object:Gem::Version
119
142
  version: '0'
120
143
  requirements: []
121
- rubygems_version: 3.1.2
144
+ rubygems_version: 3.2.5
122
145
  signing_key:
123
146
  specification_version: 4
124
147
  summary: Weather Handler gem is created for purpose of finding weather on any city