class Cb::Utils::ResponseArrayExtractor

Public Class Methods

extract(response_hash, key, singular_key = nil) click to toggle source
# File lib/cb/utils/response_array_extractor.rb, line 14
def self.extract(response_hash, key, singular_key = nil)
  new(response_hash, key, singular_key).extract
end
new(response_hash, key, singular_key) click to toggle source
# File lib/cb/utils/response_array_extractor.rb, line 18
def initialize(response_hash, key, singular_key)
  @response_hash = response_hash
  @key = key
  @singular_key = singular_key || key[0..key.length - 2]
end

Public Instance Methods

extract() click to toggle source
# File lib/cb/utils/response_array_extractor.rb, line 24
def extract
  if response_has_collection?
    extract_array_from_collection
  elsif response_has_array?
    build_array_from_delimited_values
  else
    []
  end
end

Private Instance Methods

build_array_from_delimited_values() click to toggle source
# File lib/cb/utils/response_array_extractor.rb, line 44
def build_array_from_delimited_values
  @response_hash[@key].split(',')
end
extract_array_from_collection() click to toggle source
# File lib/cb/utils/response_array_extractor.rb, line 48
def extract_array_from_collection
  if @response_hash[@key][@singular_key].is_a?(Array)
    @response_hash[@key][@singular_key]
  else
    [@response_hash[@key][@singular_key]]
  end
end
response_has_array?() click to toggle source
# File lib/cb/utils/response_array_extractor.rb, line 36
def response_has_array?
  !@response_hash[@key].nil? && @response_hash[@key].class != Hash
end
response_has_collection?() click to toggle source
# File lib/cb/utils/response_array_extractor.rb, line 40
def response_has_collection?
  !@response_hash[@key].nil? && !@response_hash[@key][@singular_key].nil?
end