forecastr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2c466cf08d42456dbf4d89ee4ab831dac22b417b
4
+ data.tar.gz: 00fbd6506159bf305cdbbaaaf0795bf3f229874c
5
+ SHA512:
6
+ metadata.gz: 3988f786efee10f8bd984ac0155d65a3ee84648734a10117644e068237f94efd422f7eb9cf7fc464cd144bd8d3001c20b91d28ee4758642fa1ab305df7af1698
7
+ data.tar.gz: 0a6afd306904c55c941829865e80f0415829fdd85d455b6af3e89133ae240e37d6572ec5d226e7ee6c022d1dfd2ca0ff7ad2364c6638d23d898a77e1c9661abf
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in forecastr.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ile Eftimov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Forecastr
2
+
3
+ A simple gem for openweathermap.org API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'forecastr'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install forecastr
18
+
19
+ ## Usage
20
+
21
+ You can search for forecast by city:
22
+ ```ruby
23
+ london = Forecastr::Radar.find_by_city("London,UK")
24
+ london.temperature.to_celsius
25
+ # => 18
26
+ ```
27
+
28
+ Or, you can search for forecast by coordinates:
29
+ ```ruby
30
+ london = Forecastr::Radar.find_by_city(51.5072, 0.1275)
31
+ london.temperature.to_celsius
32
+ # => 18
33
+ ```
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/forecastr.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'forecastr/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "forecastr"
8
+ spec.version = Forecastr::VERSION
9
+ spec.authors = ["Ile Eftimov"]
10
+ spec.email = ["ileeftimov@gmail.com"]
11
+ spec.description = %q{Simple gem to use with openweathermap.org API}
12
+ spec.summary = %q{Simple gem to use with openweathermap.org API}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "webmock"
25
+ end
data/lib/forecastr.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "forecastr/version"
2
+ require 'forecastr/radar'
3
+ require 'forecastr/forecast'
4
+ require 'forecastr/wind'
5
+ require 'forecastr/temperature'
@@ -0,0 +1,22 @@
1
+ module Forecastr
2
+ class Forecast
3
+
4
+ attr_reader :city, :longitude, :latitude, :temperature,
5
+ :pressure, :humidity, :min_temperature,
6
+ :max_temperature, :clouds, :wind
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)
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ require "net/http"
2
+ require 'json'
3
+
4
+ module Forecastr
5
+ class Radar
6
+ API_URL = "http://api.openweathermap.org/data/2.5/weather?"
7
+
8
+ class << self
9
+ def find_by_city(city_name)
10
+ radar = new
11
+ radar.find_by_city(city_name)
12
+ end
13
+ end
14
+
15
+ def find_by_city(city_name)
16
+ uri = URI(API_URL + "q=" + city_name)
17
+ @json = JSON.parse(Net::HTTP.get(uri))
18
+ Forecastr::Forecast.new(@json)
19
+ end
20
+
21
+ def find_by_coordinates(lat, lon)
22
+ uri = URI(API_URL + "lat=#{lat}&lon=#{lon}")
23
+ @json = JSON.parse(Net::HTTP.get(uri))
24
+ Forecastr::Forecast.new(@json)
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ module Forecastr
2
+ class Temperature
3
+
4
+ def initialize(kelvin)
5
+ @kelvin = kelvin
6
+ end
7
+
8
+ def to_celsius
9
+ (@kelvin - 273.15).round(2)
10
+ end
11
+
12
+ def to_farenheit
13
+ (1.8 * (@kelvin - 273.15) + 32).round(2)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Forecastr
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,21 @@
1
+ module Forecastr
2
+ class Wind
3
+ DIRECTIONS = ["N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"]
4
+
5
+ attr_reader :speed, :direction
6
+
7
+ def initialize(json)
8
+ @json = json
9
+ end
10
+
11
+ def speed
12
+ "#{@json['wind']['speed']} m/s"
13
+ end
14
+
15
+ def direction
16
+ degrees = @json['wind']['deg']
17
+ val = ((degrees/22.5) + 0.5).to_i
18
+ DIRECTIONS[val % 16]
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ {"id":88319,"dt":1345284000,"name":"Benghazi",
2
+ "coord":{"lat":32.12,"lon":20.07},
3
+ "main":{"temp":306.15,"pressure":1013,"humidity":44,"temp_min":306,"temp_max":306},
4
+ "wind":{"speed":1,"deg":-7},
5
+ "weather":[
6
+ {"id":520,"main":"Rain","description":"light intensity shower rain","icon":"09d"},
7
+ {"id":500,"main":"Rain","description":"light rain","icon":"10d"},
8
+ {"id":701,"main":"Mist","description":"mist","icon":"50d"}
9
+ ],
10
+ "clouds":{"all":90},
11
+ "rain":{"3h":3}
12
+ }
@@ -0,0 +1 @@
1
+ {"coord":{"lon":-0.13,"lat":51.51},"sys":{"message":0.047,"country":"GB","sunrise":1395381569,"sunset":1395425736},"weather":[{"id":520,"main":"Rain","description":"light intensity shower rain","icon":"09n"}],"base":"cmc stations","main":{"temp":280.79,"pressure":1001,"humidity":70,"temp_min":279.26,"temp_max":282.15},"wind":{"speed":5.1,"deg":180,"var_beg":150,"var_end":210},"rain":{"1h":5.08},"clouds":{"all":92},"dt":1395441145,"id":2643743,"name":"London","cod":200}
@@ -0,0 +1 @@
1
+ {"coord":{"lon":21.43,"lat":42},"sys":{"message":0.0427,"country":"MK","sunrise":1395376499,"sunset":1395420461},"weather":[{"id":800,"main":"Clear","description":"Sky is Clear","icon":"01n"}],"base":"cmc stations","main":{"temp":283.85,"pressure":1021,"humidity":70,"temp_min":280.15,"temp_max":285.93},"wind":{"speed":1.16,"deg":128.501},"clouds":{"all":0},"dt":1395441000,"id":863860,"name":"Opština Karpoš","cod":200}
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe Forecastr::Forecast do
4
+ let(:json) { JSON.parse(fixture('data.json').read) }
5
+ let(:forecast) { Forecastr::Forecast.new(json) }
6
+
7
+ it 'has a city' do
8
+ expect(forecast.city).to eq "Benghazi"
9
+ end
10
+
11
+ describe 'coordinates' do
12
+ it 'has longitude' do
13
+ expect(forecast.longitude).to eq 20.07
14
+ end
15
+
16
+ it 'has latitude' do
17
+ expect(forecast.latitude).to eq 32.12
18
+ end
19
+ end
20
+
21
+ describe 'current forecast' do
22
+ describe 'temperatures' do
23
+ context 'celsius' do
24
+ it 'has current temperature' do
25
+ expect(forecast.temperature.to_celsius).to eq 33.0
26
+ end
27
+
28
+ it 'has minimal temperature' do
29
+ expect(forecast.min_temperature.to_celsius).to eq 32.85
30
+ end
31
+
32
+ it 'has maximal temperature' do
33
+ expect(forecast.max_temperature.to_celsius).to eq 32.85
34
+ end
35
+ end
36
+ end
37
+
38
+ it 'has pressure' do
39
+ expect(forecast.pressure).to eq 1013
40
+ end
41
+
42
+ it 'has humidity' do
43
+ expect(forecast.humidity).to eq 44
44
+ end
45
+
46
+ end
47
+
48
+ describe 'clouds' do
49
+ it 'has cloud coverage in %' do
50
+ expect(forecast.clouds).to eq 90
51
+ end
52
+ end
53
+
54
+ describe 'wind' do
55
+ it 'has speed in m/s' do
56
+ expect(forecast.wind.speed).to eq "1 m/s"
57
+ end
58
+
59
+ it 'has direction' do
60
+ expect(forecast.wind.direction).to eq "N"
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Forecastr::Radar do
4
+ let(:radar) { Forecastr::Radar.new }
5
+
6
+ it 'finds forecast for city' do
7
+ 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'})
8
+
9
+ forecast = radar.find_by_city("London,UK")
10
+
11
+ expect(forecast.city).to eq "London"
12
+ expect(forecast.temperature.to_celsius).to eq 7.64
13
+ expect(forecast.humidity).to eq 70
14
+ expect(forecast.pressure).to eq 1001
15
+ end
16
+
17
+
18
+ it 'finds forecast by coordinates' do
19
+ 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'})
20
+
21
+ forecast = radar.find_by_coordinates(42.000, 21.4333)
22
+
23
+ expect(forecast.city).to eq "Opština Karpoš"
24
+ expect(forecast.temperature.to_celsius).to eq 10.7
25
+ expect(forecast.humidity).to eq 70
26
+ expect(forecast.pressure).to eq 1021
27
+ end
28
+
29
+
30
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe Forecastr::Temperature do
4
+ let(:temperature) { Forecastr::Temperature.new(306.11) }
5
+
6
+ describe "formats" do
7
+ it '#to_celsius' do
8
+ expect(temperature.to_celsius).to eq 32.96
9
+ end
10
+
11
+ it '#to_farenheit' do
12
+ expect(temperature.to_farenheit).to eq 91.33
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe Forecastr::Wind do
4
+ let(:wind) { Forecastr::Wind.new("wind" => {"speed" => 2.5,"deg" => -37}) }
5
+
6
+ it 'has speed in m/s' do
7
+ expect(wind.speed).to eq "2.5 m/s"
8
+ end
9
+
10
+ it 'has has direction' do
11
+ expect(wind.direction).to eq "NNW"
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ require './lib/forecastr'
2
+ require 'webmock'
3
+
4
+ include WebMock::API
5
+
6
+ def fixture_path
7
+ File.expand_path('../fixtures', __FILE__)
8
+ end
9
+
10
+ def fixture(file)
11
+ File.new(fixture_path + '/' + file)
12
+ end
13
+
14
+ def stub_get(path)
15
+ stub_request(:get, path)
16
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: forecastr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ile Eftimov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Simple gem to use with openweathermap.org API
70
+ email:
71
+ - ileeftimov@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - forecastr.gemspec
82
+ - lib/forecastr.rb
83
+ - lib/forecastr/forecast.rb
84
+ - lib/forecastr/radar.rb
85
+ - lib/forecastr/temperature.rb
86
+ - lib/forecastr/version.rb
87
+ - lib/forecastr/wind.rb
88
+ - spec/fixtures/data.json
89
+ - spec/fixtures/london.json
90
+ - spec/fixtures/skopje.json
91
+ - spec/forecastr/forecast_spec.rb
92
+ - spec/forecastr/radar_spec.rb
93
+ - spec/forecastr/temperature_spec.rb
94
+ - spec/forecastr/wind_spec.rb
95
+ - spec/spec_helper.rb
96
+ homepage: ''
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.0.3
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Simple gem to use with openweathermap.org API
120
+ test_files:
121
+ - spec/fixtures/data.json
122
+ - spec/fixtures/london.json
123
+ - spec/fixtures/skopje.json
124
+ - spec/forecastr/forecast_spec.rb
125
+ - spec/forecastr/radar_spec.rb
126
+ - spec/forecastr/temperature_spec.rb
127
+ - spec/forecastr/wind_spec.rb
128
+ - spec/spec_helper.rb