Class: Softlayer::Model::Response
- Inherits:
-
Object
- Object
- Softlayer::Model::Response
- Defined in:
- lib/softlayer/model/response.rb
Constant Summary collapse
- HASH_TYPES =
[ :'@soap_enc:array_type', :'@xsi:type', :@href ]
- RUBY_TYPES =
[ String, TrueClass, FalseClass, Nori::StringWithAttributes ]
Instance Method Summary collapse
- #build_collection(element) ⇒ Object
- #build_collection_single(element) ⇒ Object
- #build_model(element) ⇒ Object
- #build_model_object(element) ⇒ Object
- #build_reference(element) ⇒ Object
- #build_references ⇒ Object
- #convert_item_type(type) ⇒ Object
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
- #initialize_hash(hash) ⇒ Object
- #is_collection_single_element?(v) ⇒ Boolean
- #map_item_to_ruby_type(response) ⇒ Object
- #map_references ⇒ Object
- #model_build_relationships(model, element) ⇒ Object
- #process(return_object) ⇒ Object
- #process_array ⇒ Object
- #process_object ⇒ Object
Constructor Details
#initialize(response) ⇒ Response
Returns a new instance of Response.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# 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 |
Instance Method Details
#build_collection(element) ⇒ Object
88 89 90 91 92 93 94 95 |
# 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) ⇒ Object
97 98 99 100 |
# File 'lib/softlayer/model/response.rb', line 97 def build_collection_single(element) return [] if element.nil? [build_model(element)] end |
#build_model(element) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# 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) ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# 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) ⇒ Object
84 85 86 |
# File 'lib/softlayer/model/response.rb', line 84 def build_reference(element) build_model_object(element) end |
#build_references ⇒ Object
75 76 77 78 79 80 81 82 |
# 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) ⇒ Object
60 61 62 63 |
# 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) ⇒ Object
30 31 32 33 |
# 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) ⇒ Boolean
141 142 143 |
# 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) ⇒ Object
23 24 25 26 27 28 |
# 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 ⇒ Object
65 66 67 68 69 70 71 72 73 |
# 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) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 |
# 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) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# 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 ⇒ Object
47 48 49 50 51 52 53 54 |
# 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 ⇒ Object
56 57 58 |
# File 'lib/softlayer/model/response.rb', line 56 def process_object build_model(@item) end |