Class: Craigslist::API::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/craigslist/api/configuration.rb

Overview

Immutable configuration for a Client.

Instances are frozen on construction. Nothing mutates configuration at request time, which keeps a single client safe to share across threads. Multi-account setups build one client per account rather than swapping credentials on a global.

Constant Summary collapse

DEFAULT_BULK_HOST =

Host serving the RSS bulk posting interface (posting creation).

"https://post.craigslist.org"
DEFAULT_BAPI_HOST =

Host serving the JSON Bulkpost API (everything after creation).

"https://bapi.craigslist.org"
DEFAULT_REFERENCE_HOST =

Host serving public areas/categories reference data. No auth required.

"https://reference.craigslist.org"
DEFAULT_SCOPES =

Top-level OAuth scopes. Scopes are hierarchical, so requesting bulkpost.posting also grants bulkpost.posting.delete and friends.

%w[
  bulkpost.posting
  bulkpost.account.billing
  bulkpost.account.message
  bulkpost.account.stats
].freeze
DEFAULT_TIMEOUT =

Bulk submissions can carry base64 image payloads, so the default read timeout is generous.

120
DEFAULT_OPEN_TIMEOUT =

Connection establishment timeout, in seconds.

15

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email:, password:, account_id:, scopes: DEFAULT_SCOPES, bulk_host: DEFAULT_BULK_HOST, bapi_host: DEFAULT_BAPI_HOST, reference_host: DEFAULT_REFERENCE_HOST, timeout: DEFAULT_TIMEOUT, open_timeout: DEFAULT_OPEN_TIMEOUT, user_agent: "craigslist-api-ruby/#{VERSION}", logger: nil, adapter: Faraday.default_adapter) ⇒ Configuration

Returns a new instance of Configuration.

Parameters:

  • email (String)

    the craigslist account email used to log in

  • password (String)

    the craigslist account password

  • account_id (String, Integer)

    craigslist account number with posting credit, for which email is an authorized buyer

  • scopes (Array<String>) (defaults to: DEFAULT_SCOPES)

    OAuth scopes to request

  • bulk_host (String) (defaults to: DEFAULT_BULK_HOST)

    override the RSS interface host

  • bapi_host (String) (defaults to: DEFAULT_BAPI_HOST)

    override the JSON API host

  • reference_host (String) (defaults to: DEFAULT_REFERENCE_HOST)

    override the reference data host

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT)

    read timeout in seconds

  • open_timeout (Integer) (defaults to: DEFAULT_OPEN_TIMEOUT)

    connection timeout in seconds

  • user_agent (String) (defaults to: "craigslist-api-ruby/#{VERSION}")

    value sent as User-Agent

  • logger (Logger, nil) (defaults to: nil)

    when set, Faraday logs requests to it

  • adapter (Symbol) (defaults to: Faraday.default_adapter)

    Faraday adapter to use

Raises:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/craigslist/api/configuration.rb', line 54

def initialize(
  email:,
  password:,
  account_id:,
  scopes: DEFAULT_SCOPES,
  bulk_host: DEFAULT_BULK_HOST,
  bapi_host: DEFAULT_BAPI_HOST,
  reference_host: DEFAULT_REFERENCE_HOST,
  timeout: DEFAULT_TIMEOUT,
  open_timeout: DEFAULT_OPEN_TIMEOUT,
  user_agent: "craigslist-api-ruby/#{VERSION}",
  logger: nil,
  adapter: Faraday.default_adapter
)
  @email = presence!(email, :email)
  @password = presence!(password, :password)
  @account_id = presence!(, :account_id).to_s
  @scopes = Array(scopes).map(&:to_s).freeze
  @bulk_host = bulk_host
  @bapi_host = bapi_host
  @reference_host = reference_host
  @timeout = timeout
  @open_timeout = open_timeout
  @user_agent = user_agent
  @logger = logger
  @adapter = adapter

  freeze
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



37
38
39
# File 'lib/craigslist/api/configuration.rb', line 37

def 
  @account_id
end

#adapterObject (readonly)

Returns the value of attribute adapter.



37
38
39
# File 'lib/craigslist/api/configuration.rb', line 37

def adapter
  @adapter
end

#bapi_hostObject (readonly)

Returns the value of attribute bapi_host.



37
38
39
# File 'lib/craigslist/api/configuration.rb', line 37

def bapi_host
  @bapi_host
end

#bulk_hostObject (readonly)

Returns the value of attribute bulk_host.



37
38
39
# File 'lib/craigslist/api/configuration.rb', line 37

def bulk_host
  @bulk_host
end

#emailObject (readonly)

Returns the value of attribute email.



37
38
39
# File 'lib/craigslist/api/configuration.rb', line 37

def email
  @email
end

#loggerObject (readonly)

Returns the value of attribute logger.



37
38
39
# File 'lib/craigslist/api/configuration.rb', line 37

def logger
  @logger
end

#open_timeoutObject (readonly)

Returns the value of attribute open_timeout.



37
38
39
# File 'lib/craigslist/api/configuration.rb', line 37

def open_timeout
  @open_timeout
end

#passwordObject (readonly)

Returns the value of attribute password.



37
38
39
# File 'lib/craigslist/api/configuration.rb', line 37

def password
  @password
end

#reference_hostObject (readonly)

Returns the value of attribute reference_host.



37
38
39
# File 'lib/craigslist/api/configuration.rb', line 37

def reference_host
  @reference_host
end

#scopesObject (readonly)

Returns the value of attribute scopes.



37
38
39
# File 'lib/craigslist/api/configuration.rb', line 37

def scopes
  @scopes
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



37
38
39
# File 'lib/craigslist/api/configuration.rb', line 37

def timeout
  @timeout
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



37
38
39
# File 'lib/craigslist/api/configuration.rb', line 37

def user_agent
  @user_agent
end

Instance Method Details

#basic_authorizationString

HTTP Basic credential for the token endpoint.

Encoded with Array#pack rather than the base64 gem, which stopped being a default gem in Ruby 3.4 and would otherwise add a dependency.

Returns:

  • (String)


98
99
100
# File 'lib/craigslist/api/configuration.rb', line 98

def basic_authorization
  "Basic #{["#{client_id}:#{password}"].pack("m0")}"
end

#client_idString

The OAuth2 client_id, which Craigslist defines as the account email and account id joined by a semicolon.

Returns:

  • (String)


88
89
90
# File 'lib/craigslist/api/configuration.rb', line 88

def client_id
  "#{email};#{}"
end

#inspectString Also known as: to_s

Redacts the password so credentials never leak into logs or exception output through a stray inspect.

Returns:

  • (String)


116
117
118
# File 'lib/craigslist/api/configuration.rb', line 116

def inspect
  "#<#{self.class.name} email=#{email.inspect} account_id=#{.inspect} password=[FILTERED]>"
end

#post_urlString

Returns the URL RSS submissions are posted to.

Returns:

  • (String)

    the URL RSS submissions are posted to



108
109
110
# File 'lib/craigslist/api/configuration.rb', line 108

def post_url
  "#{bulk_host}/bulk-rss/post"
end

#validate_urlString

Returns the URL RSS submissions are validated against.

Returns:

  • (String)

    the URL RSS submissions are validated against



103
104
105
# File 'lib/craigslist/api/configuration.rb', line 103

def validate_url
  "#{bulk_host}/bulk-rss/validate"
end