Module: Eipiai::Resource
- Included in:
- ApiResource, HealthResource
- 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
-
.included(base) ⇒ Object
Includes the correct resource into the class, depending on its name.
Instance Method Summary collapse
- #content_types_accepted ⇒ Object
- #content_types_provided ⇒ Object
-
#malformed_request? ⇒ true, false
malformed_request?.
-
#new_object ⇒ Class?
new_object.
-
#params(body = request.body.to_s) ⇒ Hash
params.
-
#to_hash(o = represented) ⇒ Hash
to_hash.
- #to_json ⇒ Object
-
#unprocessable_entity? ⇒ true, false
unprocessable_entity?.
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_accepted ⇒ Object
80 81 82 |
# File 'lib/eipiai/webmachine/resources/base.rb', line 80 def content_types_accepted [['application/json', :from_json]] end |
#content_types_provided ⇒ Object
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.
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_object ⇒ Class?
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.
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.
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.
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_json ⇒ Object
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”.
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 |