Class: JSON::Api::Vanilla::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/json-api-vanilla/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, links: {}, rel_links: {}, meta: {}, keys: {}, objects: {}, errors: [], container: Module.new, superclass: Class.new) ⇒ Document

Returns a new instance of Document.



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/json-api-vanilla/parser.rb', line 219

def initialize(data, links: {}, rel_links: {}, meta: {},
               keys: {}, objects: {}, errors: [],
               container: Module.new, superclass: Class.new)
  @data = data
  @links = links
  @rel_links = rel_links
  @meta = meta
  @keys = keys
  @objects = objects
  @errors = errors
  @container = container
  @superclass = superclass
end

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



217
218
219
# File 'lib/json-api-vanilla/parser.rb', line 217

def container
  @container
end

#dataObject+ (readonly)

Returns the content of the JSON API data.

Returns:

  • (Object, Array<Object>)

    the content of the JSON API data.



203
204
205
# File 'lib/json-api-vanilla/parser.rb', line 203

def data
  @data
end

#errorsArray (readonly)

Returns a list of errors, if any, otherwise nil.

Returns:

  • (Array)

    a list of errors, if any, otherwise nil.



213
214
215
# File 'lib/json-api-vanilla/parser.rb', line 213

def errors
  @errors
end

#keysHash (readonly)

Returns a map from objects to a Hash from their original field names (non-snake_case’d) to the corresponding object.

Returns:

  • (Hash)

    a map from objects to a Hash from their original field names (non-snake_case’d) to the corresponding object.



216
217
218
# File 'lib/json-api-vanilla/parser.rb', line 216

def keys
  @keys
end

Returns a map from objects (obtained from .data) to their links, as a Hash.

Returns:

  • (Hash)

    a map from objects (obtained from .data) to their links, as a Hash.



206
207
208
# File 'lib/json-api-vanilla/parser.rb', line 206

def links
  @links
end

#metaHash (readonly)

Returns a map from objects to their meta information (a Hash).

Returns:

  • (Hash)

    a map from objects to their meta information (a Hash).



211
212
213
# File 'lib/json-api-vanilla/parser.rb', line 211

def meta
  @meta
end

Returns a map from objects’ relationships (obtained from .data) to the links defined in that relationship, as a Hash.

Returns:

  • (Hash)

    a map from objects’ relationships (obtained from .data) to the links defined in that relationship, as a Hash.



209
210
211
# File 'lib/json-api-vanilla/parser.rb', line 209

def rel_links
  @rel_links
end

#superclassObject (readonly)

Returns the value of attribute superclass.



218
219
220
# File 'lib/json-api-vanilla/parser.rb', line 218

def superclass
  @superclass
end

Instance Method Details

#find(type, id) ⇒ Object

Get a JSON API object.

Parameters:

  • type (String)

    the type of the object we want returned.

  • id (String)

    its id.

Returns:

  • (Object)

    the object with that type and id.



238
239
240
# File 'lib/json-api-vanilla/parser.rb', line 238

def find(type, id)
  @objects[[type, id]]
end

#find_all(type) ⇒ Array<Object>

Get all JSON API objects of a given type.

Parameters:

  • type (String)

    the type of the objects we want returned.

Returns:

  • (Array<Object>)

    the list of objects with that type.



246
247
248
# File 'lib/json-api-vanilla/parser.rb', line 246

def find_all(type)
  @objects.values.select { |obj| obj.type == type }
end