forecastr 0.1.2 → 0.1.3

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
  SHA1:
3
- metadata.gz: d09ac7372c7875677def284d8b97e62b74a47604
4
- data.tar.gz: b55a4a49c0f5ee1fe44007aa3cb1e5b048d6a7f5
3
+ metadata.gz: 9575109a60c5803a952035bf13e04107dd25f17a
4
+ data.tar.gz: fec0d1971cfbd5755ed2476f6720503cac9a021c
5
5
  SHA512:
6
- metadata.gz: e504546129d1d47ef83a5f5fc2b3656c5d9c94b6b3ca3285c62fc9f761760b740ab6a56f8d7ed53ec8603ce5899f19710347f42212141ed592f3b600c64557a0
7
- data.tar.gz: 087a4248703e4f8f8b2d06034ea01814459b9ffa501f50d2f3d3c807354f5ad401a1f244302c35c1f38cccae63e99ffba5ee4366e2a3ec211a6549f225f38a51
6
+ metadata.gz: aaf155808d600eb46cdae85b1f36607f7e5295176d9e11d1cf897898bfd2540c889c8b4e5042ed9216c18331a52aa1abf6f4a12777be95de39296830467c8f90
7
+ data.tar.gz: 1f1af391d6dc84c8662eae0c0f89bc1d05220c63f8079d00e007bd37dc4ebbc341556b7c919dc885f8f7697fa3aaa59013e98e2063435c3c85d947099adeeb35
@@ -3,3 +3,5 @@ require 'forecastr/radar'
3
3
  require 'forecastr/forecast'
4
4
  require 'forecastr/wind'
5
5
  require 'forecastr/temperature'
6
+ require 'forecastr/data_container'
7
+ require 'forecastr/client'
@@ -0,0 +1,23 @@
1
+ require "net/http"
2
+ require 'json'
3
+
4
+ module Forecastr
5
+ class Client
6
+ API_URL = "http://api.openweathermap.org/data/2.5/weather?"
7
+
8
+ class << self
9
+ def search_by_city(city_name)
10
+ uri = URI(API_URL + "q=" + city_name)
11
+ json = JSON.parse(Net::HTTP.get(uri))
12
+ Forecastr::DataContainer.new(json)
13
+ end
14
+
15
+ def search_by_coordinates(lat, lon)
16
+ uri = URI(API_URL + "lat=#{lat}&lon=#{lon}")
17
+ json = JSON.parse(Net::HTTP.get(uri))
18
+ Forecastr::DataContainer.new(json)
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ module Forecastr
2
+ class DataContainer
3
+
4
+ attr_reader :city, :longitude, :latitude, :pressure, :humidity, :clouds,
5
+ :temperature, :min_temperature, :max_temperature, :wind_speed, :wind_angle
6
+
7
+ def initialize(json)
8
+ @city = json['name']
9
+ @longitude = json['coord']['lon']
10
+ @latitude = json['coord']['lat']
11
+ @temperature = json['main']['temp']
12
+ @pressure = json['main']['pressure']
13
+ @humidity = json['main']['humidity']
14
+ @min_temperature = json['main']['temp_min']
15
+ @max_temperature = json['main']['temp_max']
16
+ @clouds = json['clouds']['all']
17
+ @wind_speed = json['wind']['speed']
18
+ @wind_angle = json['wind']['deg']
19
+ end
20
+ end
21
+ end
@@ -5,17 +5,17 @@ module Forecastr
5
5
  :pressure, :humidity, :min_temperature,
6
6
  :max_temperature, :clouds, :wind
7
7
 
8
- def initialize(json)
9
- @city = json['name']
10
- @longitude = json['coord']['lon']
11
- @latitude = json['coord']['lat']
12
- @temperature = Forecastr::Temperature.new(json['main']['temp'])
13
- @pressure = json['main']['pressure']
14
- @humidity = json['main']['humidity']
15
- @min_temperature = Forecastr::Temperature.new(json['main']['temp_min'])
16
- @max_temperature = Forecastr::Temperature.new(json['main']['temp_max'])
17
- @clouds = json['clouds']['all']
18
- @wind = Forecastr::Wind.new(json)
8
+ def initialize(data)
9
+ @city = data.city
10
+ @longitude = data.longitude
11
+ @latitude = data.latitude
12
+ @temperature = Forecastr::Temperature.new(data.temperature)
13
+ @pressure = data.pressure
14
+ @humidity = data.humidity
15
+ @min_temperature = Forecastr::Temperature.new(data.min_temperature)
16
+ @max_temperature = Forecastr::Temperature.new(data.max_temperature)
17
+ @clouds = data.clouds
18
+ @wind = Forecastr::Wind.new(data.wind_speed, data.wind_angle)
19
19
  end
20
20
 
21
21
  end
@@ -3,30 +3,24 @@ require 'json'
3
3
 
4
4
  module Forecastr
5
5
  class Radar
6
- API_URL = "http://api.openweathermap.org/data/2.5/weather?"
7
-
8
6
  class << self
9
7
  def find_by_city(city_name)
10
- radar = new
11
- radar.find_by_city(city_name)
8
+ new.find_by_city(city_name)
12
9
  end
13
10
 
14
11
  def find_by_coordinates(lat, lon)
15
- radar = new
16
- radar.find_by_coordinates(lat, lon)
12
+ new.find_by_coordinates(lat, lon)
17
13
  end
18
14
  end
19
15
 
20
- def find_by_city(city_name)
21
- uri = URI(API_URL + "q=" + city_name)
22
- @json = JSON.parse(Net::HTTP.get(uri))
23
- Forecastr::Forecast.new(@json)
16
+ def find_by_city(name)
17
+ data = Forecastr::Client.search_by_city(name)
18
+ Forecastr::Forecast.new(data)
24
19
  end
25
20
 
26
21
  def find_by_coordinates(lat, lon)
27
- uri = URI(API_URL + "lat=#{lat}&lon=#{lon}")
28
- @json = JSON.parse(Net::HTTP.get(uri))
29
- Forecastr::Forecast.new(@json)
22
+ data = Forecastr::Client.search_by_coordinates(lat, lon)
23
+ Forecastr::Forecast.new(data)
30
24
  end
31
25
 
32
26
  end
@@ -12,5 +12,9 @@ module Forecastr
12
12
  def to_farenheit
13
13
  (1.8 * (@kelvin - 273.15) + 32).round(2)
14
14
  end
15
+
16
+ def to_s
17
+ "#{to_celsius} °C"
18
+ end
15
19
  end
16
20
  end
@@ -1,3 +1,3 @@
1
1
  module Forecastr
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -4,18 +4,22 @@ module Forecastr
4
4
 
5
5
  attr_reader :speed, :direction
6
6
 
7
- def initialize(json)
8
- @json = json
7
+ def initialize(speed, angle)
8
+ @speed = speed
9
+ @angle = angle
9
10
  end
10
11
 
11
12
  def speed
12
- "#{@json['wind']['speed']} m/s"
13
+ "#{@speed} m/s"
13
14
  end
14
15
 
15
16
  def direction
16
- degrees = @json['wind']['deg']
17
- val = ((degrees/22.5) + 0.5).to_i
17
+ val = ((@angle/22.5) + 0.5).to_i
18
18
  DIRECTIONS[val % 16]
19
19
  end
20
+
21
+ def to_s
22
+ "#{speed} #{direction}"
23
+ end
20
24
  end
21
25
  end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Forecastr::Client do
4
+ describe '#search_by_city' do
5
+ it 'gets data from API for city' do
6
+ stub_get("http://api.openweathermap.org/data/2.5/weather?q=London,UK").to_return(:body => fixture('london.json'), :headers => {:content_type => 'application/json; charset=utf-8'})
7
+ results = Forecastr::Client.search_by_city('London,UK')
8
+
9
+ expect(results.city).to eq "London"
10
+ end
11
+ end
12
+
13
+ describe '#search_by_coordinates' do
14
+ it 'gets data from API by coordinates' do
15
+ stub_get("http://api.openweathermap.org/data/2.5/weather?lat=42.0&lon=21.4333").to_return(:body => fixture('skopje.json'), :headers => {:content_type => 'application/json; charset=utf-8'})
16
+ results = Forecastr::Client.search_by_coordinates(42.0, 21.4333)
17
+
18
+ expect(results.city).to eq "Opština Karpoš"
19
+ end
20
+ end
21
+
22
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Forecastr::DataContainer do
4
+ let(:json) { JSON.parse(fixture('skopje.json').read) }
5
+ let(:container) { Forecastr::DataContainer.new(json) }
6
+
7
+ it 'contains json results' do
8
+ expect(container.city).to eq "Opština Karpoš"
9
+ expect(container.longitude).to eq 21.43
10
+ expect(container.latitude).to eq 42
11
+ expect(container.temperature).to eq 283.85
12
+ expect(container.pressure).to eq 1021
13
+ expect(container.humidity).to eq 70
14
+ expect(container.min_temperature).to eq 280.15
15
+ expect(container.max_temperature).to eq 285.93
16
+ expect(container.clouds).to eq 0
17
+ expect(container.wind_speed).to eq 1.16
18
+ expect(container.wind_angle).to eq 128.501
19
+ end
20
+ end
@@ -1,8 +1,9 @@
1
- require 'spec_helper'
1
+ require_relative '../spec_helper'
2
2
 
3
3
  describe Forecastr::Forecast do
4
4
  let(:json) { JSON.parse(fixture('data.json').read) }
5
- let(:forecast) { Forecastr::Forecast.new(json) }
5
+ let(:data) { Forecastr::DataContainer.new(json) }
6
+ let(:forecast) { Forecastr::Forecast.new(data) }
6
7
 
7
8
  it 'has a city' do
8
9
  expect(forecast.city).to eq "Benghazi"
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Forecastr::Wind do
4
- let(:wind) { Forecastr::Wind.new("wind" => {"speed" => 2.5,"deg" => -37}) }
4
+ let(:wind) { Forecastr::Wind.new(2.5, -37) }
5
5
 
6
6
  it 'has speed in m/s' do
7
7
  expect(wind.speed).to eq "2.5 m/s"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forecastr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ile Eftimov
@@ -80,6 +80,8 @@ files:
80
80
  - Rakefile
81
81
  - forecastr.gemspec
82
82
  - lib/forecastr.rb
83
+ - lib/forecastr/client.rb
84
+ - lib/forecastr/data_container.rb
83
85
  - lib/forecastr/forecast.rb
84
86
  - lib/forecastr/radar.rb
85
87
  - lib/forecastr/temperature.rb
@@ -88,6 +90,8 @@ files:
88
90
  - spec/fixtures/data.json
89
91
  - spec/fixtures/london.json
90
92
  - spec/fixtures/skopje.json
93
+ - spec/forecastr/client_spec.rb
94
+ - spec/forecastr/data_container_spec.rb
91
95
  - spec/forecastr/forecast_spec.rb
92
96
  - spec/forecastr/radar_spec.rb
93
97
  - spec/forecastr/temperature_spec.rb
@@ -121,6 +125,8 @@ test_files:
121
125
  - spec/fixtures/data.json
122
126
  - spec/fixtures/london.json
123
127
  - spec/fixtures/skopje.json
128
+ - spec/forecastr/client_spec.rb
129
+ - spec/forecastr/data_container_spec.rb
124
130
  - spec/forecastr/forecast_spec.rb
125
131
  - spec/forecastr/radar_spec.rb
126
132
  - spec/forecastr/temperature_spec.rb