Class: Trav3::ResponseCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/trav3/response/response_collection.rb

Instance Method Summary collapse

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.

Parameters:

  • target (String, Integer)

Returns:



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

#countObject

Forwards to :@collection.

See Also:

  • Hash#count or Array#count


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.

Parameters:

  • target (String, Integer)

Returns:



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.

Yield Parameters:



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.

See Also:

  • Hash#empty? or Array#empty?


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.

Parameters:

  • target (String, Integer)

Returns:



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

#firstResponseCollection, ...

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.

Returns:



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.

Parameters:

  • idx (Integer) (defaults to: nil)

    (optional parameter) index of array of item to follow @href url from

Returns:



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.

See Also:

  • Hash#has_key?


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.

Returns:

  • (Boolean)


130
131
132
# File 'lib/trav3/response/response_collection.rb', line 130

def hash?
  collection.is_a? Hash
end

#key?Object

Forwards to :@collection.

See Also:

  • Hash#key?


25
# File 'lib/trav3/response/response_collection.rb', line 25

def_delegators :@collection, :count, :keys, :values, :has_key?, :key?, :empty?

#keysObject

Forwards to :@collection.

See Also:

  • Hash#keys


25
# File 'lib/trav3/response/response_collection.rb', line 25

def_delegators :@collection, :count, :keys, :values, :has_key?, :key?, :empty?

#lastResponseCollection, ...

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.

Returns:



141
142
143
# File 'lib/trav3/response/response_collection.rb', line 141

def last
  self[-1]
end

#valuesObject

Forwards to :@collection.

See Also:

  • Hash#values


25
# File 'lib/trav3/response/response_collection.rb', line 25

def_delegators :@collection, :count, :keys, :values, :has_key?, :key?, :empty?

#warningsResponseCollection?

If @warnings was returned with the response this will return a ResponseCollection instance of them. Otherwise this returns nil.

Returns:



149
150
151
152
153
# File 'lib/trav3/response/response_collection.rb', line 149

def warnings
  return nil unless hash?

  self['@warnings']
end