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

Class Method Details

.build(url:, config:, multipart: false) ⇒ Faraday::Connection

Parameters:

  • url (String)

    base URL for the connection

  • config (Configuration)
  • multipart (Boolean) (defaults to: false)

    enable the multipart request middleware

Returns:

  • (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.options.timeout = config.timeout
    f.options.open_timeout = config.open_timeout

    f.response :logger, config.logger, headers: false, bodies: false if config.logger

    f.adapter config.adapter
  end
end

.performFaraday::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.

Yield Returns:

  • (Faraday::Response)

Returns:

  • (Faraday::Response)

Raises:



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.message}"
rescue Faraday::ConnectionFailed, Faraday::SSLError => e
  raise ConnectionError, "connection failed: #{e.message}"
end