Skip to content

Commit

Permalink
Cache Faraday::Connection for persistent adapters.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed May 8, 2020
1 parent 9868f08 commit e3f7a77
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
### 0.1.1 (Next)

* [#12](https://github.com/dblock/open-weather-ruby-client/pull/12): Cache Faraday::Connection for persistent adapters - [@dblock](https://github.com/dblock).
* Your contribution here.

### 0.1.0 (5/1/2020)
### 0.1.0 (2020/05/01)

* Initial public release - [@dblock](https://github.com/dblock).
36 changes: 19 additions & 17 deletions lib/open_weather/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,28 @@ def headers
end

def connection
options = {
headers: headers.merge('Accept' => 'application/json; charset=utf-8')
}
@connection ||= begin
options = {
headers: headers.merge('Accept' => 'application/json; charset=utf-8')
}

options[:headers]['User-Agent'] = user_agent if user_agent
options[:proxy] = proxy if proxy
options[:ssl] = { ca_path: ca_path, ca_file: ca_file } if ca_path || ca_file
options[:headers]['User-Agent'] = user_agent if user_agent
options[:proxy] = proxy if proxy
options[:ssl] = { ca_path: ca_path, ca_file: ca_file } if ca_path || ca_file

request_options = {}
request_options[:timeout] = timeout if timeout
request_options[:open_timeout] = open_timeout if open_timeout
options[:request] = request_options if request_options.any?
request_options = {}
request_options[:timeout] = timeout if timeout
request_options[:open_timeout] = open_timeout if open_timeout
options[:request] = request_options if request_options.any?

::Faraday::Connection.new(endpoint, options) do |connection|
connection.use ::Faraday::Request::Multipart
connection.use ::Faraday::Request::UrlEncoded
connection.use ::OpenWeather::Response::RaiseError
connection.use ::FaradayMiddleware::ParseJson, content_type: /\bjson$/
connection.response :logger, logger if logger
connection.adapter ::Faraday.default_adapter
::Faraday::Connection.new(endpoint, options) do |connection|
connection.use ::Faraday::Request::Multipart
connection.use ::Faraday::Request::UrlEncoded
connection.use ::OpenWeather::Response::RaiseError
connection.use ::FaradayMiddleware::ParseJson, content_type: /\bjson$/
connection.response :logger, logger if logger
connection.adapter ::Faraday.default_adapter
end
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions spec/open_weather/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
expect(client.user_agent).to eq OpenWeather::Config.user_agent
expect(client.user_agent).to include OpenWeather::VERSION
end
it 'caches the Faraday connection to allow persistent adapters' do
first = client.send(:connection)
second = client.send(:connection)
expect(first).to equal second
end
(OpenWeather::Config::ATTRIBUTES - [:logger]).each do |key|
it "sets #{key}" do
expect(client.send(key)).to eq OpenWeather::Config.send(key)
Expand Down

0 comments on commit e3f7a77

Please sign in to comment.