Class: Craigslist::API::Client
- Inherits:
-
Object
- Object
- Craigslist::API::Client
- Defined in:
- lib/craigslist/api/client.rb
Overview
The single entry point for both halves of the Craigslist bulk posting platform.
Craigslist splits the work across two services with different formats and different authentication: postings are created through an RSS interface authenticated with credentials embedded in the XML, and managed afterwards through a JSON API authenticated with an OAuth2 bearer token. One set of credentials covers both. This client owns that seam so callers do not have to think about it — #post and #posting are the same object's methods, and the token lifecycle is invisible.
Configuration is frozen and nothing mutates at request time, so a client is safe to share across threads. Talking to several accounts means building several clients, which is deliberate: there is no global to reconfigure and no ambient state to get wrong.
Instance Attribute Summary collapse
- #account ⇒ Resources::Account readonly
- #billing ⇒ Resources::Billing readonly
- #config ⇒ Configuration readonly
- #images ⇒ Resources::Images readonly
- #postings ⇒ Resources::Postings readonly
-
#reference ⇒ Reference
readonly
Public areas and categories data.
Class Method Summary collapse
-
.from_config(config) ⇒ Client
Builds a client from an existing Configuration.
Instance Method Summary collapse
-
#account_messages ⇒ Array<Hash>
Notices attached to the most recent JSON API response.
- #area_for_zip(zip) ⇒ Hash{Symbol => String}
- #credit ⇒ CreditSummary
-
#initialize(email:, password:, account_id:, **options) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ Object (also: #to_s)
-
#post(postings) ⇒ ResultSet
Creates postings.
-
#posting(posting_id) ⇒ PostingHandle
A handle for working with one live posting.
- #posting_blocks ⇒ Array<PostingBlock>
- #pricing(area:, category:) ⇒ Money?
-
#reset_token! ⇒ void
Discards the cached OAuth token, forcing the next JSON call to re-authenticate.
- #stats(start: nil, stop: nil) ⇒ Array<PostingStats>
-
#validate(postings) ⇒ ResultSet
Checks postings without creating anything.
Constructor Details
#initialize(email:, password:, account_id:, **options) ⇒ Client
Returns a new instance of Client.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/craigslist/api/client.rb', line 55 def initialize(email:, password:, account_id:, **) @config = Configuration.new( email: email, password: password, account_id: account_id, ** ) build_components end |
Instance Attribute Details
#account ⇒ Resources::Account (readonly)
118 119 120 |
# File 'lib/craigslist/api/client.rb', line 118 def account @account end |
#billing ⇒ Resources::Billing (readonly)
115 116 117 |
# File 'lib/craigslist/api/client.rb', line 115 def billing @billing end |
#config ⇒ Configuration (readonly)
48 49 50 |
# File 'lib/craigslist/api/client.rb', line 48 def config @config end |
#images ⇒ Resources::Images (readonly)
112 113 114 |
# File 'lib/craigslist/api/client.rb', line 112 def images @images end |
#postings ⇒ Resources::Postings (readonly)
109 110 111 |
# File 'lib/craigslist/api/client.rb', line 109 def postings @postings end |
#reference ⇒ Reference (readonly)
Returns public areas and categories data.
121 122 123 |
# File 'lib/craigslist/api/client.rb', line 121 def reference @reference end |
Class Method Details
.from_config(config) ⇒ Client
Builds a client from an existing Craigslist::API::Configuration.
70 71 72 73 74 75 |
# File 'lib/craigslist/api/client.rb', line 70 def self.from_config(config) allocate.tap do |client| client.instance_variable_set(:@config, config) client.send(:build_components) end end |
Instance Method Details
#account_messages ⇒ Array<Hash>
Notices attached to the most recent JSON API response.
Craigslist repeats these on every response until acknowledged, so it is worth surfacing them somewhere a human will look.
158 159 160 |
# File 'lib/craigslist/api/client.rb', line 158 def @json_transport. end |
#area_for_zip(zip) ⇒ Hash{Symbol => String}
142 143 144 |
# File 'lib/craigslist/api/client.rb', line 142 def area_for_zip(zip) postings.area_for_zip(zip) end |
#credit ⇒ CreditSummary
124 125 126 |
# File 'lib/craigslist/api/client.rb', line 124 def credit billing.credit end |
#inspect ⇒ Object Also known as: to_s
170 171 172 |
# File 'lib/craigslist/api/client.rb', line 170 def inspect "#<#{self.class.name} email=#{config.email.inspect} account_id=#{config.account_id.inspect}>" end |
#post(postings) ⇒ ResultSet
Creates postings.
Does not raise when individual postings fail — a batch with some rejections is ordinary. Inspect the returned ResultSet.
96 97 98 |
# File 'lib/craigslist/api/client.rb', line 96 def post(postings) bulk.post(postings) end |
#posting(posting_id) ⇒ PostingHandle
A handle for working with one live posting.
104 105 106 |
# File 'lib/craigslist/api/client.rb', line 104 def posting(posting_id) PostingHandle.new(self, posting_id) end |
#posting_blocks ⇒ Array<PostingBlock>
129 130 131 |
# File 'lib/craigslist/api/client.rb', line 129 def posting_blocks billing.posting_blocks end |
#pricing(area:, category:) ⇒ Money?
136 137 138 |
# File 'lib/craigslist/api/client.rb', line 136 def pricing(area:, category:) billing.pricing(area: area, category: category) end |
#reset_token! ⇒ void
This method returns an undefined value.
Discards the cached OAuth token, forcing the next JSON call to re-authenticate. Rarely needed; the token refreshes itself.
166 167 168 |
# File 'lib/craigslist/api/client.rb', line 166 def reset_token! @token_provider.invalidate! end |
#stats(start: nil, stop: nil) ⇒ Array<PostingStats>
148 149 150 |
# File 'lib/craigslist/api/client.rb', line 148 def stats(start: nil, stop: nil) account.stats(start: start, stop: stop) end |