class Softlayer::Model::Response

Constants

HASH_TYPES
RUBY_TYPES

Public Class Methods

new(response) click to toggle source
# File lib/softlayer/model/response.rb, line 6
def initialize(response)
  map_item_to_ruby_type(response)
  return if @return_item

  @xsi_type = response[:'@xsi:type']

  if @xsi_type.match(/Array\z/)
    initialize_hash(response)
  else
    @item = response
  end
  @item.extend(Hashie::Extensions::DeepFind)
  @item.extend(Hashie::Extensions::DeepLocate)
  map_references
  build_references
end

Public Instance Methods

build_collection(element) click to toggle source
# File lib/softlayer/model/response.rb, line 88
def build_collection(element)
  elements = []
  element.each do |item|
    model = build_model(item)
    elements << model unless model.nil?
  end
  elements
end
build_collection_single(element) click to toggle source
# File lib/softlayer/model/response.rb, line 97
def build_collection_single(element)
  return [] if element.nil?
  [build_model(element)]
end
build_model(element) click to toggle source
# File lib/softlayer/model/response.rb, line 102
def build_model(element)
  return nil if element.nil?
  return @references_map[element[:@href].tr('#', '')] if element.keys == [:@href]
  if element.keys.include?(:@id)
    binding.pry if @references_map.nil?
    binding.pry if element.nil?
    model = @references_map[element[:@id]]
  else
    model = build_model_object(element)
  end

  model_build_relationships(model, element)
  model
end
build_model_object(element) click to toggle source
# File lib/softlayer/model/response.rb, line 117
def build_model_object(element)
  element = element.dup
  id = element.delete(:@id)
  item_type = element.delete(:'@xsi:type')
  item_type = convert_item_type(item_type)
  attributes = element.select { |k, v| !v.is_a?(Hash) }
  attributes.deep_stringify_keys!
  return element if item_type == String
  model = (item_type.to_s+"::Representer").constantize.new(item_type.new).from_hash(attributes)
end
build_reference(element) click to toggle source
# File lib/softlayer/model/response.rb, line 84
def build_reference(element)
  build_model_object(element)
end
build_references() click to toggle source
# File lib/softlayer/model/response.rb, line 75
def build_references
  @references_map = {}
  @references.each do |ref|
    element = @item.deep_locate -> (key, value, object) { key == :@id && value == ref }
    element = element.first
    @references_map[ref] = build_reference(element)
  end
end
convert_item_type(type) click to toggle source
# File lib/softlayer/model/response.rb, line 60
def convert_item_type(type)
  name = Softlayer::Generator::Converter.type(type)
  name.sub(/Array\[/, '').sub(/\]/, '').constantize
end
initialize_hash(hash) click to toggle source
# File lib/softlayer/model/response.rb, line 30
def initialize_hash(hash)
  @item = hash[:item] if hash.has_key?(:item)
  @array_type = hash[:'@soap_enc:array_type'] if hash.has_key?(:'@soap_enc:array_type')
end
is_collection_single_element?(v) click to toggle source
# File lib/softlayer/model/response.rb, line 141
def is_collection_single_element?(v)
  v.is_a?(Hash) and v.has_key?(:item) and v[:item].is_a?(Hash) and (v.keys - [:item, :"@xsi:type", :"@soap_enc:array_type"]).empty?
end
map_item_to_ruby_type(response) click to toggle source
# File lib/softlayer/model/response.rb, line 23
def map_item_to_ruby_type(response)
  if RUBY_TYPES.include?(response.class) || response.nil?
    @return_item = true
    @item = response
  end
end
map_references() click to toggle source
# File lib/softlayer/model/response.rb, line 65
def map_references
  @references = @item.deep_find_all(:@href)
  if @references
    @references = @references.sort.uniq.reverse
  else
    @references = []
  end
  @references.map! { |x| x.tr('#', '') }
end
model_build_relationships(model, element) click to toggle source
# File lib/softlayer/model/response.rb, line 128
def model_build_relationships(model, element)
  relations = element.select { |k, v| v.is_a?(Hash) }
  relations.each_pair do |k, v|
    if v.is_a?(Hash) and v.has_key?(:item) and v[:item].is_a?(Array)
      model.send("#{k}=", build_collection(v[:item]))
    elsif is_collection_single_element?(v)
      model.send("#{k}=", build_collection_single(v[:item]))
    else
      model.send("#{k}=", build_model(v))
    end
  end
end
process(return_object) click to toggle source
# File lib/softlayer/model/response.rb, line 35
def process(return_object)
  return @item if @return_item
  # check if array is empty or not
  if (!@item.is_a?(Array) && @xsi_type.match(/Array\z/))
    processed_object = process_object
    return [] if processed_object.nil?
    return [process_object] 
  end
  return process_array if (@item.is_a?(Array) && @xsi_type.match(/Array\z/))
  process_object
end
process_array() click to toggle source
# File lib/softlayer/model/response.rb, line 47
def process_array
  items = []
  @item.each do |item|
    model = build_model(item)
    items << model unless model.nil?
  end
  items
end
process_object() click to toggle source
# File lib/softlayer/model/response.rb, line 56
def process_object
  build_model(@item)
end