Module: Craigslist::API::Connection
- Defined in:
- lib/craigslist/api/connection.rb
Overview
Builds the Faraday connections the client uses.
Connections are built once per client and reused. The adapter is taken from configuration so host applications can swap in their own (or a test stub) without this gem forcing a choice on them.
Class Method Summary collapse
- .build(url:, config:, multipart: false) ⇒ Faraday::Connection
-
.perform ⇒ Faraday::Response
Runs a request, translating Faraday's transport exceptions into this library's error hierarchy.
Class Method Details
.build(url:, config:, multipart: false) ⇒ Faraday::Connection
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/craigslist/api/connection.rb', line 20 def build(url:, config:, multipart: false) Faraday.new(url: url) do |f| f.request :multipart if multipart f.headers["User-Agent"] = config.user_agent f..timeout = config.timeout f..open_timeout = config.open_timeout f.response :logger, config.logger, headers: false, bodies: false if config.logger f.adapter config.adapter end end |
.perform ⇒ Faraday::Response
Runs a request, translating Faraday's transport exceptions into this library's error hierarchy.
Deliberately does not use Faraday's :raise_error middleware. That
middleware keys purely on HTTP status, and neither Craigslist API treats
status as the source of truth — so status handling lives with the code
that also understands in-band failures.
45 46 47 48 49 50 51 |
# File 'lib/craigslist/api/connection.rb', line 45 def perform yield rescue Faraday::TimeoutError => e raise TimeoutError, "request timed out: #{e.}" rescue Faraday::ConnectionFailed, Faraday::SSLError => e raise ConnectionError, "connection failed: #{e.}" end |