Class: Onsi::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/onsi/resource.rb

Overview

The wrapper for generating a object

Author:

  • Maddie Schipper

Since:

  • 1.0.0

Defined Under Namespace

Classes: InvalidResourceError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, version = nil, includes: nil) ⇒ Onsi::Resource

Note:

The object MUST be a single object that includes Onsi::Model

Note:

The includes MUST be an array of Onsi::Include objects.

Create a new resouce.

Parameters:

  • object (Any)

    The resource backing object.

  • version (Symbol) (defaults to: nil)

    The version to render. Can be nil. If nil is passed the DEFAULT_API_VERSION will be used.

Since:

  • 1.0.0



146
147
148
149
150
151
# File 'lib/onsi/resource.rb', line 146

def initialize(object, version = nil, includes: nil)
  @object  = object
  @version = version || Model::DEFAULT_API_VERSION
  @includes = includes
  validate!
end

Instance Attribute Details

#includesArray<Onsi::Includes> (readonly)

The includes for the object.

Returns:

Since:

  • 1.0.0



131
132
133
# File 'lib/onsi/resource.rb', line 131

def includes
  @includes
end

#objectAny (readonly)

Note:

MUST include Onsi::Model

The backing object.

Returns:

  • (Any)

    The object to be rendered by the resource.

Since:

  • 1.0.0



119
120
121
# File 'lib/onsi/resource.rb', line 119

def object
  @object
end

#versionSymbol (readonly)

The version to render.

Returns:

  • (Symbol)

Since:

  • 1.0.0



125
126
127
# File 'lib/onsi/resource.rb', line 125

def version
  @version
end

Class Method Details

.as_resource(resource, version) ⇒ Onsi::Resource+

Convert an object into a Onsi::Resource

Parameters:

  • resource (Onsi::Resource, Enumerable, ActiveRecord::Base)

    The object to be converted.

    • If a Onsi::Resource is passed it will be directly returned.

    • If an Enumerable is passed #map will be called and .as_resource will be recursivly called for each object.

    • If any other object is passed it will be wrapped in a Onsi::Resource

  • version (Symbol)

    The version of the resource. ‘:v1`

Returns:

Since:

  • 1.0.0



70
71
72
73
74
75
76
77
78
79
# File 'lib/onsi/resource.rb', line 70

def as_resource(resource, version)
  case resource
  when Onsi::Resource
    resource
  when Enumerable
    resource.map { |res| as_resource(res, version) }
  else
    Onsi::Resource.new(resource, version)
  end
end

.render(resource, version) ⇒ Hash

Render a resource to JSON

Parameters:

  • version (Symbol)

    The version to render as. ‘:v1`

  • resource (Onsi::Resource, Enumerable, ActiveRecord::Base)

    The object to be converted.

    • If a Onsi::Resource is passed it will be directly returned.

    • If an Enumerable is passed #map will be called and .as_resource will be recursivly called for each object.

    • If any other object is passed it will be wrapped in a Onsi::Resource

Returns:

  • (Hash)

    The rendered resource as a hash ready to be converted to JSON.

Since:

  • 1.0.0



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/onsi/resource.rb', line 90

def render(resource, version)
  resources = as_resource(resource, version)
  {}.tap do |root|
    root[DATA_KEY] = resources.as_json
    included = all_included(resources)
    if included.any?
      root[INCLUDED_KEY] = included
    end
    root[META_KEY] = {}.tap do |meta|
      meta[:count] = resources.count if resources.respond_to?(:count)
    end
  end
end

Instance Method Details

#as_json(_opts = {}) ⇒ Hash

Creates a raw JSON object.

Returns:

  • (Hash)

Since:

  • 1.0.0



157
158
159
160
161
162
163
164
165
166
# File 'lib/onsi/resource.rb', line 157

def as_json(_opts = {})
  {}.tap do |root|
    root[TYPE_KEY] = type
    root[ID_KEY]   = object_identifier
    root[ATTRIBUTES_KEY] = generate_attributes
    append_relationships(root)
    append_meta(root)
    append_includes(root)
  end
end