Class: Ipregistry::BatchResponse
- Inherits:
-
Object
- Object
- Ipregistry::BatchResponse
- Includes:
- Enumerable
- Defined in:
- lib/ipregistry/batch_response.rb
Overview
Ordered results of a batch operation (Client#batch_lookup or Client#parse_user_agents). Each entry may independently succeed or fail (for example on an invalid IP address), so entries are Result objects inspected element by element:
response = client.batch_lookup(["8.8.8.8", "not-an-ip"])
response.each do |result|
if result.success?
puts result.value.location.country.name
else
warn "entry failed: #{result.error.}"
end
end
BatchResponse is Enumerable, so map, select, zip, and friends work
as expected. Use #values for the successful values only, or
Result#value! to raise a failed entry's error.
Defined Under Namespace
Classes: Result
Instance Attribute Summary collapse
-
#results ⇒ Array<Result>
readonly
The entries, in input order.
Instance Method Summary collapse
-
#[](index) ⇒ Object
The Result at the given index.
-
#each ⇒ Object
Yields each Result in input order.
-
#errors ⇒ Array<ApiError>
The per-entry errors, in order, skipping successful entries.
-
#initialize(results) ⇒ BatchResponse
constructor
A new instance of BatchResponse.
- #size ⇒ Object (also: #length)
-
#values ⇒ Object
The successfully resolved values, in order, skipping failed entries.
Constructor Details
#initialize(results) ⇒ BatchResponse
Returns a new instance of BatchResponse.
70 71 72 |
# File 'lib/ipregistry/batch_response.rb', line 70 def initialize(results) @results = results.freeze end |
Instance Attribute Details
#results ⇒ Array<Result> (readonly)
Returns the entries, in input order.
68 69 70 |
# File 'lib/ipregistry/batch_response.rb', line 68 def results @results end |
Instance Method Details
#[](index) ⇒ Object
The Result at the given index.
80 81 82 |
# File 'lib/ipregistry/batch_response.rb', line 80 def [](index) @results[index] end |
#each ⇒ Object
Yields each Result in input order.
75 76 77 |
# File 'lib/ipregistry/batch_response.rb', line 75 def each(&) @results.each(&) end |
#errors ⇒ Array<ApiError>
The per-entry errors, in order, skipping successful entries.
96 97 98 |
# File 'lib/ipregistry/batch_response.rb', line 96 def errors @results.select(&:failure?).map(&:error) end |
#size ⇒ Object Also known as: length
84 85 86 |
# File 'lib/ipregistry/batch_response.rb', line 84 def size @results.size end |
#values ⇒ Object
The successfully resolved values, in order, skipping failed entries.
90 91 92 |
# File 'lib/ipregistry/batch_response.rb', line 90 def values @results.select(&:success?).map(&:value) end |