Skip to content
This repository was archived by the owner on Apr 25, 2020. It is now read-only.

Commit 8753d31

Browse files
committed
Merge pull request #1 from norbert/faraday
Replace Typhoeus with Faraday
2 parents 3b5f8b1 + afa3bee commit 8753d31

5 files changed

Lines changed: 45 additions & 11 deletions

File tree

README.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ You can then make requests to the `Forecast::IO.forecast(latitude, longitide, op
3838

3939
Valid options in the `options` hash are:
4040

41-
* `:time` - UNIX time in seconds since the Unix epoch.
41+
* `:time` - Unix time in seconds.
4242
* `:params` - Query parameters that can contain the following:
4343
* `:jsonp` - JSONP callback.
4444
* `:units` - Return the API response in SI units, rather than the default Imperial units.
@@ -47,31 +47,49 @@ Valid options in the `options` hash are:
4747
Get the current forecast:
4848

4949
```ruby
50-
forecast = Forecast::IO.forecast('37.8267','-122.423')
50+
forecast = Forecast::IO.forecast(37.8267, -122.423)
5151
```
5252

5353
Get the current forecast at a given time:
5454

5555
```ruby
56-
forecast = Forecast::IO.forecast('37.8267','-122.423', time: Time.new(2013, 3, 11).to_i)
56+
forecast = Forecast::IO.forecast(37.8267, -122.423, time: Time.new(2013, 3, 11).to_i)
5757
```
5858

5959
Get the current forecast and use SI units:
6060

6161
```ruby
62-
forecast = Forecast::IO.forecast('37.8267','-122.423', params: {units: 'si'})
62+
forecast = Forecast::IO.forecast(37.8267, -122.423, params: { units: 'si' })
6363
```
6464

6565
The `forecast(...)` method will return a response that you can interact with in a more-friendly way, such as:
6666

6767
```ruby
68-
forecast = Forecast::IO.forecast('37.8267','-122.423')
68+
forecast = Forecast::IO.forecast(37.8267, -122.423)
6969
forecast.latitude
7070
forecast.longitude
7171
```
7272

7373
Please refer to the [forecast.io](https://developer.darkskyapp.com/docs/v2) API documentation for more information on the full response properties.
7474

75+
The HTTP requests are made with [Faraday](https://github.com/lostisland/faraday), which uses `Net::HTTP` by default. Changing the adapter is easy:
76+
77+
```ruby
78+
require 'typhoeus/adapters/faraday'
79+
80+
Faraday.default_adapter = :typhoeus
81+
```
82+
83+
Alternatively:
84+
85+
```ruby
86+
require 'typhoeus/adapters/faraday'
87+
88+
Forecast::IO.connection = Faraday.new do |builder|
89+
builder.adapter :typhoeus
90+
end
91+
```
92+
7593
## Contributing to forecast_io
7694

7795
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet

forecast_io.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ Gem::Specification.new do |s|
1919
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
2020
s.require_paths = ["lib"]
2121

22-
s.add_dependency('typhoeus')
22+
s.add_dependency('faraday')
2323
s.add_dependency('multi_json')
2424
s.add_dependency('hashie')
2525

2626
s.add_development_dependency('rake')
2727
s.add_development_dependency('rspec')
2828
s.add_development_dependency('vcr')
29+
s.add_development_dependency('typhoeus')
2930
end

lib/forecast_io.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
require 'hashie'
55
require 'multi_json'
6-
require 'typhoeus'
6+
require 'faraday'
77

88
module Forecast
99
module IO
@@ -20,15 +20,27 @@ def forecast(latitude, longitude, options = {})
2020
forecast_url += ",#{options[:time]}" if options[:time]
2121

2222
forecast_response = if options[:params]
23-
Typhoeus::Request.get(forecast_url, params: options[:params])
23+
connection.get(forecast_url, options[:params])
2424
else
25-
Typhoeus::Request.get(forecast_url)
25+
connection.get(forecast_url)
2626
end
2727

2828
if forecast_response.success?
2929
return Hashie::Mash.new(MultiJson.load(forecast_response.body))
3030
end
3131
end
32+
33+
# Build or get an HTTP connection object.
34+
def connection
35+
@connection ||= Faraday.new
36+
end
37+
38+
# Set an HTTP connection object.
39+
#
40+
# @param connection Connection object to be used.
41+
def connection=(connection)
42+
@connection = connection
43+
end
3244
end
3345
end
3446
end

spec/forecast_io/forecast_io_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
it 'should return a forecast for a given latitude, longitude and time' do
1818
VCR.use_cassette('forecast_for_latitude_longitide_and_time') do
1919
Forecast::IO.api_key = 'this-is-an-api-key'
20-
forecast = Forecast::IO.forecast('37.8267','-122.423', time: Time.new(2013, 3, 11).to_i)
20+
forecast = Forecast::IO.forecast('37.8267','-122.423', time: Time.utc(2013, 3, 11, 4).to_i)
2121
forecast.should_not be_nil
2222
forecast.latitude.should == 37.8267
2323
forecast.longitude.should == -122.423
@@ -38,4 +38,4 @@
3838
end
3939
end
4040
end
41-
end
41+
end

spec/spec_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
55

66
require 'vcr'
7+
require 'typhoeus/adapters/faraday'
78

89
VCR.configure do |c|
910
c.cassette_library_dir = 'spec/cassettes'
1011
c.hook_into :typhoeus
1112
c.allow_http_connections_when_no_cassette = false
1213
end
1314

15+
Faraday.default_adapter = :typhoeus
16+
1417
RSpec.configure do |config|
1518
config.before(:each) do
1619
Forecast::IO.api_key = nil

0 commit comments

Comments
 (0)