Class: Craigslist::API::ResultSet
- Inherits:
-
Object
- Object
- Craigslist::API::ResultSet
- Includes:
- Enumerable
- Defined in:
- lib/craigslist/api/result_set.rb
Overview
Every Result from one bulk submission, plus the upload id Craigslist assigns to the batch.
Enumerable, so it behaves like the array of results it wraps while still answering the questions you actually have after a submission.
Instance Attribute Summary collapse
-
#raw ⇒ String?
readonly
The unparsed response body.
- #results ⇒ Array<Result> readonly
-
#upload_id ⇒ String?
readonly
Craigslist's identifier for this submission, taken from the channel description.
Instance Method Summary collapse
- #[](key) ⇒ Result?
-
#all_successful? ⇒ Boolean
Whether every posting succeeded.
- #any_failures? ⇒ Boolean
- #each {|result| ... } ⇒ Enumerator, ResultSet
- #empty? ⇒ Boolean
- #failed ⇒ Array<Result>
-
#initialize(results:, upload_id: nil, raw: nil) ⇒ ResultSet
constructor
A new instance of ResultSet.
- #inspect ⇒ Object
-
#posting_ids ⇒ Array<String>
Ids of postings that were created.
-
#size ⇒ Integer
(also: #length)
Number of results in the batch.
- #successful ⇒ Array<Result>
-
#warned ⇒ Array<Result>
Results carrying non-fatal warnings, which are easy to miss because they can accompany a success.
Constructor Details
#initialize(results:, upload_id: nil, raw: nil) ⇒ ResultSet
Returns a new instance of ResultSet.
29 30 31 32 33 34 35 |
# File 'lib/craigslist/api/result_set.rb', line 29 def initialize(results:, upload_id: nil, raw: nil) @results = Array(results).freeze @upload_id = upload_id @raw = raw @index = @results.each_with_object({}) { |r, memo| memo[r.key] = r }.freeze freeze end |
Instance Attribute Details
#raw ⇒ String? (readonly)
The unparsed response body.
Kept because a NOT_VALID with no explanation is otherwise a dead end, and reaching the raw XML should not require rebuilding the request by hand.
27 28 29 |
# File 'lib/craigslist/api/result_set.rb', line 27 def raw @raw end |
#results ⇒ Array<Result> (readonly)
14 15 16 |
# File 'lib/craigslist/api/result_set.rb', line 14 def results @results end |
#upload_id ⇒ String? (readonly)
Returns craigslist's identifier for this submission, taken from the channel description. Present in post mode.
18 19 20 |
# File 'lib/craigslist/api/result_set.rb', line 18 def upload_id @upload_id end |
Instance Method Details
#[](key) ⇒ Result?
45 46 47 |
# File 'lib/craigslist/api/result_set.rb', line 45 def [](key) @index[key.to_s] end |
#all_successful? ⇒ Boolean
Returns whether every posting succeeded.
66 67 68 |
# File 'lib/craigslist/api/result_set.rb', line 66 def all_successful? failed.empty? end |
#any_failures? ⇒ Boolean
71 72 73 |
# File 'lib/craigslist/api/result_set.rb', line 71 def any_failures? !failed.empty? end |
#each {|result| ... } ⇒ Enumerator, ResultSet
39 40 41 |
# File 'lib/craigslist/api/result_set.rb', line 39 def each(&block) results.each(&block) end |
#empty? ⇒ Boolean
86 87 88 |
# File 'lib/craigslist/api/result_set.rb', line 86 def empty? results.empty? end |
#failed ⇒ Array<Result>
55 56 57 |
# File 'lib/craigslist/api/result_set.rb', line 55 def failed select(&:failure?) end |
#inspect ⇒ Object
90 91 92 93 |
# File 'lib/craigslist/api/result_set.rb', line 90 def inspect "#<#{self.class.name} size=#{size} successful=#{successful.size} " \ "failed=#{failed.size} upload_id=#{upload_id.inspect}>" end |
#posting_ids ⇒ Array<String>
Returns ids of postings that were created.
76 77 78 |
# File 'lib/craigslist/api/result_set.rb', line 76 def posting_ids select(&:posted?).map(&:posting_id).compact end |
#size ⇒ Integer Also known as: length
Returns number of results in the batch.
81 82 83 |
# File 'lib/craigslist/api/result_set.rb', line 81 def size results.size end |
#successful ⇒ Array<Result>
50 51 52 |
# File 'lib/craigslist/api/result_set.rb', line 50 def successful select(&:success?) end |
#warned ⇒ Array<Result>
Returns results carrying non-fatal warnings, which are easy to miss because they can accompany a success.
61 62 63 |
# File 'lib/craigslist/api/result_set.rb', line 61 def warned select(&:warnings?) end |