Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/typhoeus_fix/array_decoder.rb

Overview

Add Hash#is_typhoeus_array? method

Instance Method Summary collapse

Instance Method Details

#decode_typhoeus_arrayArray/Hash

If the hash is an array encoded by typhoeus an array is returned else the self is returned

Returns:

See Also:



53
54
55
56
57
58
59
# File 'lib/typhoeus_fix/array_decoder.rb', line 53

def decode_typhoeus_array
  if self.im_an_array_typhoeus_encoded?
    Hash[self.sort].values
  else
    self
  end
end

#im_an_array_typhoeus_encoded?TrueClass

Checks if hash is an Array encoded as a hash. Specifically will check for the hash to have this form: {‘0’ => v0, ‘1’ => v1, .., ‘n’ => vN }

Returns:

  • (TrueClass)


40
41
42
43
44
45
# File 'lib/typhoeus_fix/array_decoder.rb', line 40

def im_an_array_typhoeus_encoded?
  return false if self.empty?
  #return if array is empty or the key is not a valid number
  return false if self.empty? || self.keys.first.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil
  self.keys.map {|k| k.to_i}.sort == (0...self.keys.size).map {|i| i}
end