Module: Spaceship::ConnectAPI::Models
- Defined in:
- spaceship/lib/spaceship/connect_api/model.rb
Class Attribute Summary collapse
-
.types ⇒ Object
Returns the value of attribute types.
-
.types_cache ⇒ Object
Returns the value of attribute types_cache.
Class Method Summary collapse
- .find_class(model_data) ⇒ Object
- .inflate_model(model_data, included) ⇒ Object
- .inflate_model_relationships(type_instance, relationships, included) ⇒ Object
- .parse(json) ⇒ Object
Class Attribute Details
.types ⇒ Object
Returns the value of attribute types.
76 77 78 |
# File 'spaceship/lib/spaceship/connect_api/model.rb', line 76 def types @types end |
.types_cache ⇒ Object
Returns the value of attribute types_cache.
77 78 79 |
# File 'spaceship/lib/spaceship/connect_api/model.rb', line 77 def types_cache @types_cache end |
Class Method Details
.find_class(model_data) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'spaceship/lib/spaceship/connect_api/model.rb', line 97 def self.find_class(model_data) # Initialize cache @types_cache ||= {} # Find class in cache type_string = model_data["type"] type_class = @types_cache[type_string] return type_class if type_class # Find class in array type_class = @types.find do |type| type.type == type_string end # Cache and return class @types_cache[type_string] = type_class return type_class end |
.inflate_model(model_data, included) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'spaceship/lib/spaceship/connect_api/model.rb', line 116 def self.inflate_model(model_data, included) # Find class type_class = find_class(model_data) raise "No type class found for #{model_data['type']}" unless type_class # Get id and attributes needed for inflating id = model_data["id"] attributes = model_data["attributes"] # Instantiate object and inflate relationships relationships = model_data["relationships"] || [] type_instance = type_class.new(id, attributes) type_instance = inflate_model_relationships(type_instance, relationships, included) return type_instance end |
.inflate_model_relationships(type_instance, relationships, included) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'spaceship/lib/spaceship/connect_api/model.rb', line 133 def self.inflate_model_relationships(type_instance, relationships, included) # Relationship attributes to set attributes = {} # 1. Iterate over relationships # 2. Find id and type # 3. Find matching id and type in included # 4. Inflate matching data and set in attributes relationships.each do |key, value| # Validate data exists value_data_or_datas = value["data"] next unless value_data_or_datas # Map an included data object map_data = lambda do |value_data| id = value_data["id"] type = value_data["type"] relationship_data = included.find do |included_data| id == included_data["id"] && type == included_data["type"] end inflate_model(relationship_data, included) if relationship_data end # Map a hash or an array of data if value_data_or_datas.kind_of?(Hash) attributes[key] = map_data.call(value_data_or_datas) elsif value_data_or_datas.kind_of?(Array) attributes[key] = value_data_or_datas.map(&map_data) end end type_instance.update_attributes(attributes) return type_instance end |
.parse(json) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'spaceship/lib/spaceship/connect_api/model.rb', line 80 def self.parse(json) data = json["data"] raise "No data" unless data included = json["included"] || [] if data.kind_of?(Hash) inflate_model(data, included) elsif data.kind_of?(Array) return data.map do |model_data| inflate_model(model_data, included) end else raise "'data' is neither a hash nor an array" end end |