Module: Eipiai::Resource

Included in:
ApiResource
Defined in:
lib/eipiai/webmachine/resources/base.rb

Overview

Resource

The base resource which can be included in regular Webmachine::Resource objects. It provides sensible defaults for a full-features REST API endpoint.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Includes the correct resource into the class, depending on its name.

If the resource is called ‘ItemResource`, the `Item` part is considered singular, and thus the `SingularResource` is included into the class.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/eipiai/webmachine/resources/base.rb', line 20

def self.included(base)
  subject_class_name = base.name.demodulize.chomp('Resource')

  if subject_class_name == subject_class_name.singularize
    base.send(:include, SingularResource)
  else
    base.send(:include, CollectionResource)
  end

  base.extend(ClassMethods)
end

Instance Method Details

#content_types_acceptedObject



80
81
82
# File 'lib/eipiai/webmachine/resources/base.rb', line 80

def content_types_accepted
  [['application/json', :from_json]]
end

#content_types_providedObject



76
77
78
# File 'lib/eipiai/webmachine/resources/base.rb', line 76

def content_types_provided
  [['application/hal+json', :to_json]]
end

#malformed_request?true, false

malformed_request?

return ‘true` if content_type is of type `application/json`, and the JSON request body cannot be parsed.

Examples:

valid JSON payload

post('/items', '{ "uid": "hello" }', 'Content-Type': 'application/json')
resource.malformed_request? # => false

invalid JSON payload

post('/items', '{ invalid! }', 'Content-Type': 'application/json')
resource.malformed_request? # => true

Returns:

  • (true, false)


47
48
49
50
51
52
53
# File 'lib/eipiai/webmachine/resources/base.rb', line 47

def malformed_request?
  return false unless request.json? && request.body.to_s.present?

  JSON.parse(request.body.to_s) && false
rescue JSON::ParserError
  json_error_body(:invalid_json)
end

#new_objectClass?

new_object

Return the instantiated object, based on the representer provided by ‘singular_representer_class` and the parameters provided by `params`.

See ‘Object#new_object` for the version without using representers.

Examples:

resource.new_object.class # => Item

Returns:

  • (Class, nil)

    instantiated representer, or nil if not found



96
97
98
99
100
# File 'lib/eipiai/webmachine/resources/base.rb', line 96

def new_object
  return if singular_representer_class.nil? || object_class.nil?

  @new_object ||= singular_representer_class.new(object_class.new).from_hash(params)
end

#params(body = request.body.to_s) ⇒ Hash

params

Given a string in JSON format, returns the hash representation of that object.

If the input is invalid JSON, an empty hash is returned.

If no argument is given, ‘request.body` is used as the JSON input.

Examples:

Parse valid JSON request

resource.params('{ "hello": "world" }') # => { 'hello' => 'world' }

Parse invalid JSON request

resource.params('invalid') #=> {}

Parameters:

  • body (String) (defaults to: request.body.to_s)

    JSON provided as a string

Returns:

  • (Hash)

    JSON string, converted to a hash



120
121
122
123
124
# File 'lib/eipiai/webmachine/resources/base.rb', line 120

def params(body = request.body.to_s)
  @params ||= JSON.parse(body)
rescue JSON::ParserError
  {}
end

#to_hash(o = represented) ⇒ Hash

to_hash

Given an object, calls ‘#to_hash` on that object,

If the object’s ‘to_hash` implementation accepts any arguments, the hash `{ request: request }` is sent as its first argument.

In practice, this method is used without any parameters, causing the method to call ‘represented`, which represents a Roar representer. This in turn converts the represented object to a HAL/JSON compatible hash representation.

Examples:

item = Item.new(uid: 'hello')
resource.to_hash(item) # => { uid: 'hello' }

Parameters:

  • o (Object) (defaults to: represented)

    represented to call #to_hash on

Returns:

  • (Hash)

    hash representation of the object



145
146
147
# File 'lib/eipiai/webmachine/resources/base.rb', line 145

def to_hash(o = represented)
  o.method(:to_hash).arity.zero? ? o.to_hash : o.to_hash(request: request)
end

#to_jsonObject



149
150
151
# File 'lib/eipiai/webmachine/resources/base.rb', line 149

def to_json
  to_hash.to_json
end

#unprocessable_entity?true, false

unprocessable_entity?

return ‘true` if content_type is of type `application/json`, and the newly generated object reports back as “invalid”.

Examples:

invalid object

post('/users', '{}', 'Content-Type': 'application/json')
resource.unprocessable_entity? # => true

valid object

post('/users', '{ "uid": "bartsimpson" }', 'Content-Type': 'application/json')
resource.unprocessable_entity? # => false

Returns:

  • (true, false)


70
71
72
73
74
# File 'lib/eipiai/webmachine/resources/base.rb', line 70

def unprocessable_entity?
  return false unless request.json? && new_object.respond_to?(:invalid?)

  new_object.invalid? && json_error_body(*new_object.errors)
end