Class: Trav3::ResponseCollection
- Inherits:
-
Object
- Object
- Trav3::ResponseCollection
- Extended by:
- Forwardable
- Defined in:
- lib/trav3/response/response_collection.rb
Instance Method Summary collapse
-
#[](target) ⇒ ResponseCollection, ...
Either the key or index of the item you wish to get depending on if this collection is a #hash? or an array.
-
#count ⇒ Object
Forwards to :@collection.
-
#dig(*target) ⇒ ResponseCollection, ...
Either the key or index of the item you wish to get depending on if this collection is a #hash? or an array.
-
#each {|| ... } ⇒ Object
When the inner collection is an Array every item iterated over is yielded to you as a
ResponseCollection
. -
#empty? ⇒ Object
Forwards to :@collection.
-
#fetch(target) ⇒ ResponseCollection, ...
Either the key or index of the item you wish to get depending on if this collection is a #hash? or an array.
-
#first ⇒ ResponseCollection, ...
When the inner collection is an Array it returns the first item as either a
ResponseCollection
or aString
. -
#follow(idx = nil) ⇒ Success, RequestError
Follows
@href
link within item. -
#has_key? ⇒ Object
Forwards to :@collection.
-
#hash? ⇒ Boolean
Reveals if the inner collection is a Hash or not.
-
#initialize(travis, collection) ⇒ ResponseCollection
constructor
A new instance of ResponseCollection.
-
#key? ⇒ Object
Forwards to :@collection.
-
#keys ⇒ Object
Forwards to :@collection.
-
#last ⇒ ResponseCollection, ...
When the inner collection is an Array it returns the last item as either a
ResponseCollection
or aString
. -
#values ⇒ Object
Forwards to :@collection.
-
#warnings ⇒ ResponseCollection?
If
@warnings
was returned with the response this will return aResponseCollection
instance of them.
Constructor Details
#initialize(travis, collection) ⇒ ResponseCollection
Returns a new instance of ResponseCollection
26 27 28 29 |
# File 'lib/trav3/response/response_collection.rb', line 26 def initialize(travis, collection) @travis = travis @collection = collection end |
Instance Method Details
#[](target) ⇒ ResponseCollection, ...
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
.
40 41 42 43 44 45 |
# File 'lib/trav3/response/response_collection.rb', line 40 def [](target) result = collection[target] return ResponseCollection.new(travis, result) if collection?(result) result end |
#count ⇒ Object
Forwards to :@collection.
25 |
# File 'lib/trav3/response/response_collection.rb', line 25 def_delegators :@collection, :count, :keys, :values, :has_key?, :key?, :empty? |
#dig(*target) ⇒ ResponseCollection, ...
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
.
48 49 50 51 52 53 54 55 56 57 58 |
# 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 |
#each {|| ... } ⇒ Object
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
.
67 68 69 70 71 72 73 |
# 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 |
#empty? ⇒ Object
Forwards to :@collection.
25 |
# File 'lib/trav3/response/response_collection.rb', line 25 def_delegators :@collection, :count, :keys, :values, :has_key?, :key?, :empty? |
#fetch(target) ⇒ ResponseCollection, ...
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.
88 89 90 91 92 93 94 95 96 97 |
# 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 |
#first ⇒ ResponseCollection, ...
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
.
106 107 108 |
# File 'lib/trav3/response/response_collection.rb', line 106 def first self[0] end |
#follow(idx = nil) ⇒ Success, RequestError
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.
117 118 119 120 121 122 123 124 125 |
# 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 |
#has_key? ⇒ Object
Forwards to :@collection.
25 |
# File 'lib/trav3/response/response_collection.rb', line 25 def_delegators :@collection, :count, :keys, :values, :has_key?, :key?, :empty? |
#hash? ⇒ Boolean
Reveals if the inner collection is a Hash or not.
130 131 132 |
# File 'lib/trav3/response/response_collection.rb', line 130 def hash? collection.is_a? Hash end |
#key? ⇒ Object
Forwards to :@collection.
25 |
# File 'lib/trav3/response/response_collection.rb', line 25 def_delegators :@collection, :count, :keys, :values, :has_key?, :key?, :empty? |
#keys ⇒ Object
Forwards to :@collection.
25 |
# File 'lib/trav3/response/response_collection.rb', line 25 def_delegators :@collection, :count, :keys, :values, :has_key?, :key?, :empty? |
#last ⇒ ResponseCollection, ...
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
.
141 142 143 |
# File 'lib/trav3/response/response_collection.rb', line 141 def last self[-1] end |
#values ⇒ Object
Forwards to :@collection.
25 |
# File 'lib/trav3/response/response_collection.rb', line 25 def_delegators :@collection, :count, :keys, :values, :has_key?, :key?, :empty? |
#warnings ⇒ ResponseCollection?
If @warnings
was returned with the response this will return a ResponseCollection
instance of them. Otherwise this returns nil
.
149 150 151 152 153 |
# File 'lib/trav3/response/response_collection.rb', line 149 def warnings return nil unless hash? self['@warnings'] end |