forecast_io 1.2.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +4 -0
- data/README.md +10 -10
- data/forecast_io.gemspec +1 -1
- data/lib/forecast_io.rb +34 -36
- data/lib/forecast_io/configuration.rb +27 -29
- data/lib/forecast_io/version.rb +2 -5
- data/spec/forecast_io/configuration_spec.rb +4 -4
- data/spec/forecast_io/forecast_io_spec.rb +22 -21
- data/spec/forecast_io/version_spec.rb +3 -3
- data/spec/spec_helper.rb +1 -1
- metadata +4 -4
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -23,7 +23,7 @@ require 'forecast_io'
|
|
23
23
|
You will need to set your API key before you can make requests to the [forecast.io](https://developer.darkskyapp.com/docs/v2) API.
|
24
24
|
|
25
25
|
```ruby
|
26
|
-
|
26
|
+
ForecastIO.configure do |configuration|
|
27
27
|
configuration.api_key = 'this-is-your-api-key'
|
28
28
|
end
|
29
29
|
```
|
@@ -31,10 +31,10 @@ end
|
|
31
31
|
Alternatively:
|
32
32
|
|
33
33
|
```ruby
|
34
|
-
|
34
|
+
ForecastIO.api_key = 'this-is-your-api-key'
|
35
35
|
```
|
36
36
|
|
37
|
-
You can then make requests to the `
|
37
|
+
You can then make requests to the `ForecastIO.forecast(latitude, longitude, options = {})` method.
|
38
38
|
|
39
39
|
Valid options in the `options` hash are:
|
40
40
|
|
@@ -47,25 +47,25 @@ Valid options in the `options` hash are:
|
|
47
47
|
Get the current forecast:
|
48
48
|
|
49
49
|
```ruby
|
50
|
-
forecast =
|
50
|
+
forecast = ForecastIO.forecast(37.8267, -122.423)
|
51
51
|
```
|
52
52
|
|
53
53
|
Get the current forecast at a given time:
|
54
54
|
|
55
55
|
```ruby
|
56
|
-
forecast =
|
56
|
+
forecast = ForecastIO.forecast(37.8267, -122.423, time: Time.new(2013, 3, 11).to_i)
|
57
57
|
```
|
58
58
|
|
59
59
|
Get the current forecast and use SI units:
|
60
60
|
|
61
61
|
```ruby
|
62
|
-
forecast =
|
62
|
+
forecast = ForecastIO.forecast(37.8267, -122.423, params: { units: 'si' })
|
63
63
|
```
|
64
64
|
|
65
65
|
The `forecast(...)` method will return a response that you can interact with in a more-friendly way, such as:
|
66
66
|
|
67
67
|
```ruby
|
68
|
-
forecast =
|
68
|
+
forecast = ForecastIO.forecast(37.8267, -122.423)
|
69
69
|
forecast.latitude
|
70
70
|
forecast.longitude
|
71
71
|
```
|
@@ -91,7 +91,7 @@ Alternatively:
|
|
91
91
|
```ruby
|
92
92
|
require 'typhoeus/adapters/faraday'
|
93
93
|
|
94
|
-
|
94
|
+
ForecastIO.connection = Faraday.new do |builder|
|
95
95
|
builder.adapter :typhoeus
|
96
96
|
end
|
97
97
|
```
|
@@ -99,11 +99,11 @@ end
|
|
99
99
|
You can also customise the default parameters passed through on each API call:
|
100
100
|
|
101
101
|
```ruby
|
102
|
-
|
102
|
+
ForecastIO.default_params = {units: 'si'}
|
103
103
|
|
104
104
|
# or
|
105
105
|
|
106
|
-
|
106
|
+
ForecastIO.configure do |configuration|
|
107
107
|
configuration.default_params = {units: 'si'}
|
108
108
|
end
|
109
109
|
```
|
data/forecast_io.gemspec
CHANGED
@@ -5,7 +5,7 @@ require 'forecast_io/version'
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "forecast_io"
|
8
|
-
s.version =
|
8
|
+
s.version = ForecastIO::VERSION
|
9
9
|
s.authors = ["David Czarnecki"]
|
10
10
|
s.email = ["me@davidczarnecki.com"]
|
11
11
|
s.homepage = "https://github.com/darkskyapp/forecast-ruby"
|
data/lib/forecast_io.rb
CHANGED
@@ -5,48 +5,46 @@ require 'hashie'
|
|
5
5
|
require 'multi_json'
|
6
6
|
require 'faraday'
|
7
7
|
|
8
|
-
module
|
9
|
-
|
10
|
-
extend Configuration
|
11
|
-
|
12
|
-
self.default_params = {}
|
13
|
-
|
14
|
-
class << self
|
15
|
-
# Retrieve the forecast for a given latitude and longitude.
|
16
|
-
#
|
17
|
-
# @param latitude [String] Latitude.
|
18
|
-
# @param longitude [String] Longitude.
|
19
|
-
# @param options [String] Optional parameters. Valid options are `:time` and `:params`.
|
20
|
-
def forecast(latitude, longitude, options = {})
|
21
|
-
forecast_url = "#{Forecast::IO.api_endpoint}/forecast/#{Forecast::IO.api_key}/#{latitude},#{longitude}"
|
22
|
-
forecast_url += ",#{options[:time]}" if options[:time]
|
23
|
-
|
24
|
-
forecast_response = get(forecast_url, options[:params])
|
25
|
-
|
26
|
-
if forecast_response.success?
|
27
|
-
return Hashie::Mash.new(MultiJson.load(forecast_response.body))
|
28
|
-
end
|
29
|
-
end
|
8
|
+
module ForecastIO
|
9
|
+
extend Configuration
|
30
10
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
11
|
+
self.default_params = {}
|
12
|
+
|
13
|
+
class << self
|
14
|
+
# Retrieve the forecast for a given latitude and longitude.
|
15
|
+
#
|
16
|
+
# @param latitude [String] Latitude.
|
17
|
+
# @param longitude [String] Longitude.
|
18
|
+
# @param options [String] Optional parameters. Valid options are `:time` and `:params`.
|
19
|
+
def forecast(latitude, longitude, options = {})
|
20
|
+
forecast_url = "#{ForecastIO.api_endpoint}/forecast/#{ForecastIO.api_key}/#{latitude},#{longitude}"
|
21
|
+
forecast_url += ",#{options[:time]}" if options[:time]
|
35
22
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
@connection = connection
|
23
|
+
forecast_response = get(forecast_url, options[:params])
|
24
|
+
|
25
|
+
if forecast_response.success?
|
26
|
+
return Hashie::Mash.new(MultiJson.load(forecast_response.body))
|
41
27
|
end
|
28
|
+
end
|
42
29
|
|
43
|
-
|
30
|
+
# Build or get an HTTP connection object.
|
31
|
+
def connection
|
32
|
+
@connection ||= Faraday.new
|
33
|
+
end
|
44
34
|
|
45
|
-
|
46
|
-
|
35
|
+
# Set an HTTP connection object.
|
36
|
+
#
|
37
|
+
# @param connection Connection object to be used.
|
38
|
+
def connection=(connection)
|
39
|
+
@connection = connection
|
40
|
+
end
|
47
41
|
|
48
|
-
|
49
|
-
|
42
|
+
private
|
43
|
+
|
44
|
+
def get(path, params = {})
|
45
|
+
params = ForecastIO.default_params.merge(params || {})
|
46
|
+
|
47
|
+
connection.get(path, params)
|
50
48
|
end
|
51
49
|
end
|
52
50
|
end
|
@@ -1,38 +1,36 @@
|
|
1
|
-
module
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
DEFAULT_FORECAST_IO_API_ENDPOINT = 'https://api.forecast.io'
|
1
|
+
module ForecastIO
|
2
|
+
module Configuration
|
3
|
+
# Default API endpoint
|
4
|
+
DEFAULT_FORECAST_IO_API_ENDPOINT = 'https://api.forecast.io'
|
6
5
|
|
7
|
-
|
8
|
-
|
6
|
+
# Forecast API endpoint
|
7
|
+
attr_writer :api_endpoint
|
9
8
|
|
10
|
-
|
11
|
-
|
9
|
+
# API key
|
10
|
+
attr_writer :api_key
|
12
11
|
|
13
|
-
|
14
|
-
|
12
|
+
# Default parameters
|
13
|
+
attr_accessor :default_params
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
15
|
+
# Yield self to be able to configure ForecastIO with block-style configuration.
|
16
|
+
#
|
17
|
+
# Example:
|
18
|
+
#
|
19
|
+
# ForecastIO.configure do |configuration|
|
20
|
+
# configuration.api_key = 'this-is-your-api-key'
|
21
|
+
# end
|
22
|
+
def configure
|
23
|
+
yield self
|
24
|
+
end
|
26
25
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
26
|
+
# API endpoint
|
27
|
+
def api_endpoint
|
28
|
+
@api_endpoint ||= DEFAULT_FORECAST_IO_API_ENDPOINT
|
29
|
+
end
|
31
30
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
31
|
+
# API key
|
32
|
+
def api_key
|
33
|
+
@api_key
|
36
34
|
end
|
37
35
|
end
|
38
36
|
end
|
data/lib/forecast_io/version.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe ForecastIO::Configuration do
|
4
4
|
describe '.configure' do
|
5
5
|
it 'should have default attributes' do
|
6
|
-
|
7
|
-
configuration.api_endpoint.should eql(
|
6
|
+
ForecastIO.configure do |configuration|
|
7
|
+
configuration.api_endpoint.should eql(ForecastIO::Configuration::DEFAULT_FORECAST_IO_API_ENDPOINT)
|
8
8
|
configuration.api_key.should be_nil
|
9
9
|
end
|
10
10
|
end
|
11
11
|
end
|
12
|
-
end
|
12
|
+
end
|
@@ -1,17 +1,20 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe ForecastIO do
|
4
4
|
describe '.default_params' do
|
5
5
|
it "defaults to an empty hash" do
|
6
|
-
|
6
|
+
ForecastIO.default_params.should == {}
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
10
|
describe '.forecast' do
|
11
|
+
before :each do
|
12
|
+
ForecastIO.api_key = 'this-is-an-api-key'
|
13
|
+
end
|
14
|
+
|
11
15
|
it 'should return a forecast for a given latitude, longitude' do
|
12
16
|
VCR.use_cassette('forecast_for_latitude_longitude', record: :once) do
|
13
|
-
|
14
|
-
forecast = Forecast::IO.forecast('37.8267','-122.423')
|
17
|
+
forecast = ForecastIO.forecast('37.8267','-122.423')
|
15
18
|
forecast.should_not be_nil
|
16
19
|
forecast.latitude.should == 37.8267
|
17
20
|
forecast.longitude.should == -122.423
|
@@ -22,8 +25,7 @@ describe Forecast::IO do
|
|
22
25
|
|
23
26
|
it 'should return a forecast for a given latitude, longitude and time' do
|
24
27
|
VCR.use_cassette('forecast_for_latitude_longitude_and_time') do
|
25
|
-
|
26
|
-
forecast = Forecast::IO.forecast('37.8267','-122.423', time: Time.utc(2013, 3, 11, 4).to_i)
|
28
|
+
forecast = ForecastIO.forecast('37.8267','-122.423', time: Time.utc(2013, 3, 11, 4).to_i)
|
27
29
|
forecast.should_not be_nil
|
28
30
|
forecast.latitude.should == 37.8267
|
29
31
|
forecast.longitude.should == -122.423
|
@@ -34,8 +36,7 @@ describe Forecast::IO do
|
|
34
36
|
|
35
37
|
it 'should return a forecast for a given latitude, longitude and query params' do
|
36
38
|
VCR.use_cassette('forecast_for_latitude_longitude_and_query_params') do
|
37
|
-
|
38
|
-
forecast = Forecast::IO.forecast('37.8267','-122.423', params: {units: 'si'})
|
39
|
+
forecast = ForecastIO.forecast('37.8267','-122.423', params: {units: 'si'})
|
39
40
|
forecast.should_not be_nil
|
40
41
|
forecast.latitude.should == 37.8267
|
41
42
|
forecast.longitude.should == -122.423
|
@@ -51,60 +52,60 @@ describe Forecast::IO do
|
|
51
52
|
before :each do
|
52
53
|
stub_const 'Faraday', stub(new: faraday)
|
53
54
|
|
54
|
-
|
55
|
+
ForecastIO.stub(api_key: 'abc123', connection: faraday)
|
55
56
|
end
|
56
57
|
|
57
58
|
context 'without default parameters' do
|
58
59
|
before :each do
|
59
|
-
|
60
|
+
ForecastIO.stub(default_params: {})
|
60
61
|
end
|
61
62
|
|
62
63
|
it "sends through a standard request" do
|
63
64
|
faraday.should_receive(:get).with(
|
64
65
|
'https://api.forecast.io/forecast/abc123/1.2,3.4', {}
|
65
|
-
).and_return
|
66
|
+
).and_return(response)
|
66
67
|
|
67
|
-
|
68
|
+
ForecastIO.forecast(1.2, 3.4)
|
68
69
|
end
|
69
70
|
|
70
71
|
it "sends through provided parameters" do
|
71
72
|
faraday.should_receive(:get).with(
|
72
73
|
'https://api.forecast.io/forecast/abc123/1.2,3.4', {units: 'si'}
|
73
|
-
).and_return
|
74
|
+
).and_return(response)
|
74
75
|
|
75
|
-
|
76
|
+
ForecastIO.forecast(1.2, 3.4, params: {units: 'si'})
|
76
77
|
end
|
77
78
|
end
|
78
79
|
|
79
80
|
context 'with default parameters' do
|
80
81
|
before :each do
|
81
|
-
|
82
|
+
ForecastIO.stub(default_params: {units: 'si'})
|
82
83
|
end
|
83
84
|
|
84
85
|
it "sends through the default parameters" do
|
85
86
|
faraday.should_receive(:get).with(
|
86
87
|
'https://api.forecast.io/forecast/abc123/1.2,3.4', {units: 'si'}
|
87
|
-
).and_return
|
88
|
+
).and_return(response)
|
88
89
|
|
89
|
-
|
90
|
+
ForecastIO.forecast(1.2, 3.4)
|
90
91
|
end
|
91
92
|
|
92
93
|
it "sends through the merged parameters" do
|
93
94
|
faraday.should_receive(:get).with(
|
94
95
|
'https://api.forecast.io/forecast/abc123/1.2,3.4',
|
95
96
|
{units: 'si', exclude: 'daily'}
|
96
|
-
).and_return
|
97
|
+
).and_return(response)
|
97
98
|
|
98
|
-
|
99
|
+
ForecastIO.forecast(1.2, 3.4, params: {exclude: 'daily'})
|
99
100
|
end
|
100
101
|
|
101
102
|
it "overwrites default parameters when appropriate" do
|
102
103
|
faraday.should_receive(:get).with(
|
103
104
|
'https://api.forecast.io/forecast/abc123/1.2,3.4',
|
104
105
|
{units: 'imperial'}
|
105
|
-
).and_return
|
106
|
+
).and_return(response)
|
106
107
|
|
107
|
-
|
108
|
+
ForecastIO.forecast(1.2, 3.4, params: {units: 'imperial'})
|
108
109
|
end
|
109
110
|
end
|
110
111
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: forecast_io
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-07-
|
12
|
+
date: 2013-07-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|
@@ -164,7 +164,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
164
164
|
version: '0'
|
165
165
|
segments:
|
166
166
|
- 0
|
167
|
-
hash:
|
167
|
+
hash: -4308199890416542600
|
168
168
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
169
169
|
none: false
|
170
170
|
requirements:
|
@@ -173,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
173
173
|
version: '0'
|
174
174
|
segments:
|
175
175
|
- 0
|
176
|
-
hash:
|
176
|
+
hash: -4308199890416542600
|
177
177
|
requirements: []
|
178
178
|
rubyforge_project: forecast_io
|
179
179
|
rubygems_version: 1.8.25
|