class Trav3::ResponseCollection
Attributes
Public Class Methods
# File lib/trav3/response/response_collection.rb, line 26 def initialize(travis, collection) @travis = travis @collection = collection end
Public Instance Methods
Either the key or index of the item you wish to get depending on if this collection is a {#hash?} or an array.
If the item retrieved is a Hash or Array then the returned item will be another instance of `ResponseCollection`. Otherwise it will be a `String` unless the target does not exist and then it will be `nil`.
@param target [String, Integer] @return [ResponseCollection, String, nil]
# File lib/trav3/response/response_collection.rb, line 40 def [](target) result = collection[target] return ResponseCollection.new(travis, result) if collection?(result) result end
(see []
)
# File lib/trav3/response/response_collection.rb, line 48 def dig(*target) dug, *rest = target result = collection.dig(dug) if collection?(result) rc = ResponseCollection.new(travis, result) return rest.empty? ? rc : rc.dig(*rest) end result end
When the inner collection is an Array every item iterated over is yielded to you as a `ResponseCollection`.
If the inner collection is a {#hash?} then this method acts as though you've called `each` directly on that `Hash`.
@yieldparam [Array, ResponseCollection]
# File lib/trav3/response/response_collection.rb, line 67 def each(&block) return collection.each(&block) if hash? collection.each do |item| yield ResponseCollection.new(travis, item) end end
Either the key or index of the item you wish to get depending on if this collection is a {#hash?} or an array.
If the item retrieved is a Hash or Array then the returned item will be another instance of `ResponseCollection`. Otherwise it will be a `String`.
If the target does not exist and no block was given this will raise an exception. If a block was given, then that block will be evaluated and that return value returned.
@param target [String, Integer] @return [ResponseCollection, String, nil]
# File lib/trav3/response/response_collection.rb, line 88 def fetch(target) result = collection.fetch(target) { nil } return ResponseCollection.new(travis, result) if collection?(result) return result if result # For error raising behavior collection.fetch(target) unless block_given? yield end
When the inner collection is an Array it returns the first item as either a `ResponseCollection` or a `String`. If the Array is empty it returns `nil`.
If the inner collection is a {#hash?} then this simply returns `nil`.
@return [ResponseCollection, String, nil]
# File lib/trav3/response/response_collection.rb, line 106 def first self[0] end
Follows `@href` link within item. If `#hash?` returns `true` then `#follow` takes no parameters. If `#hash?` returns `false` then `#follow` takes an index parameter
for which item in the Array you wish to follow.
@param idx [Integer] (optional parameter) index of array of item to follow `@href` url from @return [Success, RequestError]
# File lib/trav3/response/response_collection.rb, line 117 def follow(idx = nil) if href? && !idx url = collection.fetch('@href') return travis.send(:get_path_with_opts, url) end result = fetch(idx) result.follow end
Reveals if the inner collection is a Hash or not.
@return [Boolean]
# File lib/trav3/response/response_collection.rb, line 130 def hash? collection.is_a? Hash end
When the inner collection is an Array it returns the last item as either a `ResponseCollection` or a `String`. If the Array is empty it returns `nil`.
If the inner collection is a {#hash?} then this simply returns `nil`.
@return [ResponseCollection, String, nil]
# File lib/trav3/response/response_collection.rb, line 141 def last self[-1] end
If `@warnings` was returned with the response this will return a `ResponseCollection` instance of them. Otherwise this returns `nil`.
@return [ResponseCollection, nil]
# File lib/trav3/response/response_collection.rb, line 149 def warnings return nil unless hash? self['@warnings'] end
Private Instance Methods
# File lib/trav3/response/response_collection.rb, line 157 def collection?(input) [Array, Hash].include? input.class end
# File lib/trav3/response/response_collection.rb, line 161 def href? collection.respond_to?(:key?) and collection.key?('@href') end