Class: Subjoin::Document

Inherits:
Object
  • Object
show all
Includes:
Linkable, Metable
Defined in:
lib/subjoin/document.rb

Overview

A JSON-API top level document

Instance Attribute Summary collapse

Attributes included from Linkable

#links

Attributes included from Metable

#meta

Instance Method Summary collapse

Methods included from Linkable

#has_links?, #load_links

Methods included from Metable

#has_meta?, #load_meta

Constructor Details

#initialize(*args) ⇒ Document

Create a document. Parameters can take several forms:

  1. A URI object: Document will be created from the URI

  2. A Hash: The Hash is assumed to be a parsed JSON response and the Document will be created from that

  3. One string: Assumed to be a JSON-API object type. An attempt will be made to map this type to a class that inherits from InheritableResource and to load the create the Document from a URL provided by that class. There is also the assumption that this URL returns all objects of that type.

  4. Two strings: Assumed to be a JSON-API object type and id. The same mapping is attempted as before, and the second parameter is added to the URL

Parameters:

  • args (Array)


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/subjoin/document.rb', line 30

def initialize(*args)
  if args.count < 1
    raise ArgumentError.new
  end

  contents = load_by_type(args[0], args[1..-1])

  @meta = load_meta(contents['meta'])
  @links = load_links(contents['links'])
  @included = load_included(contents)
  @data = load_data(contents)
  @jsonapi = load_jsonapi(contents)
end

Instance Attribute Details

#dataObject (readonly)

The document’s primary data



8
9
10
# File 'lib/subjoin/document.rb', line 8

def data
  @data
end

#includedObject (readonly)

Resources included in a compound document“



11
12
13
# File 'lib/subjoin/document.rb', line 11

def included
  @included
end

#jsonapiObject (readonly)

JSON-API version information



14
15
16
# File 'lib/subjoin/document.rb', line 14

def jsonapi
  @jsonapi
end

Instance Method Details

#has_data?Boolean

Returns true if there is primary data.

Returns:

  • (Boolean)

    true if there is primary data



45
46
47
# File 'lib/subjoin/document.rb', line 45

def has_data?
  return ! @data.nil?
end

#has_included?Boolean

Returns true if there are included resources.

Returns:

  • (Boolean)

    true if there are included resources



50
51
52
# File 'lib/subjoin/document.rb', line 50

def has_included?
  return ! @included.nil?
end

#has_jsonapi?Boolean

Returns true if there is version information.

Returns:

  • (Boolean)

    true if there is version information



55
56
57
# File 'lib/subjoin/document.rb', line 55

def has_jsonapi?
  return ! @jsonapi.nil?
end