ArseniysWeatherApp 0.1.3 → 0.1.4

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: 3f4922b3cd32a82a547ad83f3716617be8d59a9b973f4b8f2b838f6536119c09
4
- data.tar.gz: 78b4381b0da44be9555eb5c1b00034b5076cabcd4783d01a7239f92106797ce7
3
+ metadata.gz: 6c7b486feee9a4fc832cb2fa78b8a2647d16709bf0fb357d5ff535f45619628f
4
+ data.tar.gz: 034deb0b4180118f9d6248b69e5d6e264acc0005228924c4f66601fa5db40ca9
5
5
  SHA512:
6
- metadata.gz: 933148f86e854cdb0337eff764b5f8ab5e49b7b3bd0a1b44ffa7ccf858cdc4b425e0e125e33a12e634a7b32f6cbd3764cbea1699aa3a4f7cfaa8cc55178a7f91
7
- data.tar.gz: 5e6c8d1d367e6a09332bcfa005b6cd85aa88c31e64a9fc7f9982cfeed84d83eac70d8131ea3278c9c1af58fdd7864779219a717ef63d8b153b41c068648d063d
6
+ metadata.gz: 85090f66d32734b18e95f73c33e399b7e34ab7a7b01a0af0899a06fc266b483d7f0d713cfa7f94196f3da481b508211b66aed3c21705c1ecc9b1d1e7b7a693e1
7
+ data.tar.gz: 211c1bcf5232b4c4b9006f96f5aac89ddb6dbf515348250db215a7f1a9c5c7a67f2ce0ca7d82f5bcc456bab3d7be397c34474872245e7eaf3072601e471250cd
data/.gitignore CHANGED
@@ -7,18 +7,11 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
  /spec/
10
- /bin/
11
10
  /.idea/
12
11
 
13
12
  # rspec failure tracking
14
13
  .rspec_status
15
14
  .rspec
16
- .rubocop.yml
17
- .ruby-version
18
- arseniys_weather_app.gemspec
19
- Gemfile
20
- Gemfile.lock
21
- Rakefile
22
15
  *.gem
23
16
  test.rb
24
17
  *.json
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "ArseniysWeatherApp"
5
+
6
+ my_weather = ["Run my_weather_today to see your weather forecast for today",
7
+ "Run my_weather_threedays to see your weather forecast for 3 days",
8
+ "Run location_avg_temp_today desired_latitude desired_longitude to see this location weather forecast for today",
9
+ "Run location_avg_temp_threedays desired_latitude desired_longitude to see this location weather forecast for 3 days"]
10
+ puts my_weather
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "ArseniysWeatherApp"
5
+
6
+ forecast = ArseniysWeatherApp::LocationWeatherForecast.new(lat: ARGV[0], lon: ARGV[1])
7
+ puts forecast.today_avg_temperature
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "ArseniysWeatherApp"
5
+
6
+ forecast = ArseniysWeatherApp::LocationWeatherForecast.new(lat: ARGV[0], lon: ARGV[1])
7
+ puts forecast.three_days_avg_temperature
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "ArseniysWeatherApp"
5
+
6
+ forecast = ArseniysWeatherApp::MyWeatherForecast.new
7
+ puts forecast.three_days_avg_temperature
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "ArseniysWeatherApp"
5
+
6
+ forecast = ArseniysWeatherApp::MyWeatherForecast.new
7
+ puts forecast.today_avg_temperature
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ArseniysWeatherApp
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.4"
5
5
  end
@@ -7,126 +7,118 @@ require "geokit"
7
7
  module ArseniysWeatherApp
8
8
  class Error < StandardError; end
9
9
 
10
- class Weather
11
- def initialize
12
- attach_yandex_api
13
- attach_ip_stack_api
14
- configure_geokit
15
- @spb_location = { lat: 59.9573887, lon: 30.3430287 }
16
- end
17
-
18
- def weather_from_json
19
- JSON.parse(File.read("WeatherForecasts.json"))
20
- end
21
-
22
- def weather_json(location: [59.9573887, 30.3430287],
23
- lang: "ru_RU", forecasts: 7,
24
- hours: false, extra: false)
25
- attach_yandex_api if @yandex_api.nil?
10
+ class Requests
11
+ def self.weather_json(location: [59.9573887, 30.3430287],
12
+ lang: "ru_RU", forecasts: 7,
13
+ hours: false, extra: false)
26
14
  url = "https://api.weather.yandex.ru/v2/forecast"
27
15
  params = { "lat" => location[0], "lon" => location[1],
28
16
  "lang" => lang, "limit" => forecasts,
29
17
  "hours" => hours, "extra" => extra }
30
- header = { "X-Yandex-API-Key" => @yandex_api }
18
+ header = { "X-Yandex-API-Key" => "01cafac0-8fd8-48ca-956a-f442580428a0" }
31
19
  response = Faraday.get(url, params, header)
32
20
  JSON.parse(response.body)
33
21
  end
34
22
 
35
- def cleanup_forecast(parts)
36
- parts["forecasts"][0]["parts"].delete_if { |key| %w[day_short night_short].include?(key) }
37
- end
38
-
39
- def cleanup_avg_temperature(parts)
40
- parts.delete_if { |key| %w[day_short night_short].include?(key) }
41
- { morning: parts["morning"]["temp_avg"], day: parts["day"]["temp_avg"],
42
- evening: parts["evening"]["temp_avg"], night: parts["night"]["temp_avg"] }
23
+ def self.ip_addr
24
+ url = "https://obscure-temple-73986.herokuapp.com/"
25
+ response = Faraday.get(url, { "get_ip" => "yes" })
26
+ JSON.parse(response.body)["ip_addr"]
43
27
  end
28
+ end
44
29
 
45
- def lat_lon_today_forecast(lat:, lon:)
46
- cleanup_forecast(weather_json(location: [lat, lon], forecasts: 1, hours: true))
30
+ class GeoInfo
31
+ def self.spb_location
32
+ { lat: 59.9573887, lon: 30.3430287 }
47
33
  end
48
34
 
49
- def lat_lon_today_avg_temperature(lat:, lon:)
50
- cleanup_avg_temperature(lat_lon_today_forecast(lat: lat, lon: lon))
35
+ def self.location_by_ip
36
+ Geokit::Geocoders::IpstackGeocoder.api_key = "a1b3e41fa2284fb6fd4db50b8629e884"
37
+ geokit = Geokit::Geocoders::IpstackGeocoder.geocode(Requests.ip_addr)
38
+ { lat: geokit.lat, lon: geokit.longitude }
51
39
  end
40
+ end
52
41
 
53
- def lat_lon_fact(lat:, lon:)
54
- weather_json(location: [lat, lon], forecasts: 1)["fact"]
42
+ class Forecast
43
+ def initialize(lat:, lon:)
44
+ @location = [lat, lon]
55
45
  end
56
46
 
57
- def lat_lon_fact_temperature(lat:, lon:)
58
- lat_lon_fact(lat: lat, lon: lon)["temp"]
47
+ def today
48
+ Requests.weather_json(location: @location, forecasts: 1, hours: true)
59
49
  end
60
50
 
61
- def spb_today_forecast
62
- lat_lon_today_forecast(lat: @spb_location[:lat], lon: @spb_location[:lon])
51
+ def three_days
52
+ Requests.weather_json(location: @location, forecasts: 3, hours: true)
63
53
  end
54
+ end
64
55
 
65
- def spb_today_avg_temperature
66
- cleanup_avg_temperature(spb_today_forecast)
56
+ class Cleaners
57
+ def self.avg_temperature(forecast)
58
+ forecast = forecast_json(forecast)
59
+ avg_temperature = {}
60
+ day_name = ["today", "tomorrow", "day after tomorrow"]
61
+ forecast["forecasts"].each_with_index do |day, i|
62
+ avg_temperature[day_name[i]] = { morning: day["parts"]["morning"]["temp_avg"],
63
+ day: day["parts"]["day"]["temp_avg"],
64
+ evening: day["parts"]["evening"]["temp_avg"],
65
+ night: day["parts"]["night"]["temp_avg"] }
66
+ end
67
+ avg_temperature
67
68
  end
68
69
 
69
- def spb_fact
70
- lat_lon_fact(lat: @spb_location[:lat], lon: @spb_location[:lon])
70
+ def self.forecast_json(parts)
71
+ parts["forecasts"].each do |day|
72
+ day["parts"].delete_if { |key| %w[day_short night_short].include?(key) }
73
+ end
74
+ parts
71
75
  end
72
76
 
73
- def spb_fact_temperature
74
- spb_fact["temp"]
77
+ def self.fact(parts)
78
+ parts["fact"]["temp"]
75
79
  end
80
+ end
76
81
 
77
- def my_today_forecast
78
- location = acquire_location
79
- lat_lon_today_forecast(lat: location[0], lon: location[1])
82
+ class LocationWeatherForecast
83
+ def initialize(lat:, lon:)
84
+ @forecast = Forecast.new(lat: lat, lon: lon)
80
85
  end
81
86
 
82
- def my_today_avg_temperature
83
- cleanup_avg_temperature(my_today_forecast)
87
+ def fact
88
+ Cleaners.fact(@forecast.today)
84
89
  end
85
90
 
86
- def my_fact
87
- location = acquire_location
88
- lat_lon_fact(lat: location[0], lon: location[1])
91
+ def today_avg_temperature
92
+ Cleaners.avg_temperature(@forecast.today)
89
93
  end
90
94
 
91
- def my_fact_temperature
92
- my_fact["temp"]
95
+ def three_days_avg_temperature
96
+ Cleaners.avg_temperature(@forecast.three_days)
93
97
  end
94
98
 
95
- private
96
-
97
- def attach_yandex_api
98
- return unless @yandex_api.nil?
99
-
100
- url = "https://obscure-temple-73986.herokuapp.com/"
101
- response = Faraday.get(url, { "weather" => "yes" })
102
- @yandex_api = JSON.parse(response.body)["message"]
99
+ def today_forecast_json
100
+ Cleaners.forecast_json(@forecast.today)
103
101
  end
104
102
 
105
- def configure_geokit
106
- Geokit::Geocoders::IpstackGeocoder.api_key = @ip_stack_api
103
+ def three_days_forecast_json
104
+ Cleaners.forecast_json(@forecast.three_days)
107
105
  end
106
+ end
108
107
 
109
- def acquire_location
110
- geokit = Geokit::Geocoders::IpstackGeocoder.geocode(acquire_ip_addr)
111
- [geokit.lat, geokit.longitude]
108
+ class MyWeatherForecast
109
+ def initialize
110
+ location = GeoInfo.location_by_ip
111
+ @location_weather = LocationWeatherForecast.new(lat: location[:lat], lon: location[:lon])
112
112
  end
113
113
 
114
- def attach_ip_stack_api
115
- return unless @ip_stack_api.nil?
114
+ def method_missing(method_name)
115
+ raise NoMethodError unless @location_weather.methods.include?(method_name)
116
116
 
117
- url = "https://obscure-temple-73986.herokuapp.com/"
118
- response = Faraday.get(url, { "get_ip" => "yes" })
119
- @ip_stack_api = JSON.parse(response.body)["ip_stack_api"]
117
+ @location_weather.method(method_name).call
120
118
  end
121
119
 
122
- def acquire_ip_addr
123
- url = "https://obscure-temple-73986.herokuapp.com/"
124
- response = Faraday.get(url, { "get_ip" => "yes" })
125
- JSON.parse(response.body)["ip_addr"]
120
+ def respond_to_missing?(method_name, include_private = false)
121
+ @location_weather.methods.include?(method_name)
126
122
  end
127
123
  end
128
124
  end
129
-
130
- # f = File.new("WeatherForecasts.json", "w")
131
- # f.write(Application.new.weather_json(forecasts: 1).to_json)
132
- # f.close
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ArseniysWeatherApp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arseniy Chirkov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-12 00:00:00.000000000 Z
11
+ date: 2021-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -43,13 +43,23 @@ description: |-
43
43
  tells you what weather is right now
44
44
  email:
45
45
  - supernich19@gmail.com
46
- executables: []
46
+ executables:
47
+ - arseniys_weather_app
48
+ - location_avg_temp_threedays
49
+ - location_avg_temp_today
50
+ - my_weather_threedays
51
+ - my_weather_today
47
52
  extensions: []
48
53
  extra_rdoc_files: []
49
54
  files:
50
55
  - ".gitignore"
51
56
  - LICENSE.txt
52
57
  - README.md
58
+ - exe/arseniys_weather_app
59
+ - exe/location_avg_temp_threedays
60
+ - exe/location_avg_temp_today
61
+ - exe/my_weather_threedays
62
+ - exe/my_weather_today
53
63
  - lib/ArseniysWeatherApp.rb
54
64
  - lib/ArseniysWeatherApp/version.rb
55
65
  homepage: https://github.com/Supernich/ArseniysWeatherApp