Class: Ipregistry::BatchResponse

Inherits:
Object
  • Object
show all
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.message}"
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

Instance Method Summary collapse

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

#resultsArray<Result> (readonly)

Returns the entries, in input order.

Returns:

  • (Array<Result>)

    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

#eachObject

Yields each Result in input order.



75
76
77
# File 'lib/ipregistry/batch_response.rb', line 75

def each(&)
  @results.each(&)
end

#errorsArray<ApiError>

The per-entry errors, in order, skipping successful entries.

Returns:



96
97
98
# File 'lib/ipregistry/batch_response.rb', line 96

def errors
  @results.select(&:failure?).map(&:error)
end

#sizeObject Also known as: length



84
85
86
# File 'lib/ipregistry/batch_response.rb', line 84

def size
  @results.size
end

#valuesObject

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