Module: Spaceship::ConnectAPI::Models

Defined in:
spaceship/lib/spaceship/connect_api/model.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.typesObject

Returns the value of attribute types


56
57
58
# File 'spaceship/lib/spaceship/connect_api/model.rb', line 56

def types
  @types
end

.types_cacheObject

Returns the value of attribute types_cache


57
58
59
# File 'spaceship/lib/spaceship/connect_api/model.rb', line 57

def types_cache
  @types_cache
end

Class Method Details

.find_class(model_data) ⇒ Object


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'spaceship/lib/spaceship/connect_api/model.rb', line 77

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


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'spaceship/lib/spaceship/connect_api/model.rb', line 96

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


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'spaceship/lib/spaceship/connect_api/model.rb', line 113

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)
    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


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'spaceship/lib/spaceship/connect_api/model.rb', line 60

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