Class: JSONAPIonify::Api::Action
- Inherits:
-
Object
- Object
- JSONAPIonify::Api::Action
- Extended by:
- Dummy, JSONAPIonify::Autoload
- Includes:
- Documentation
- Defined in:
- lib/jsonapionify/api/action.rb
Defined Under Namespace
Modules: Documentation, Dummy
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#cacheable ⇒ Object
readonly
Returns the value of attribute cacheable.
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#only_associated ⇒ Object
readonly
Returns the value of attribute only_associated.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#prepend ⇒ Object
readonly
Returns the value of attribute prepend.
-
#request_method ⇒ Object
readonly
Returns the value of attribute request_method.
-
#responses ⇒ Object
readonly
Returns the value of attribute responses.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #build_path(base, name, include_path) ⇒ Object
- #call(resource, request, callbacks: self.callbacks, **opts) ⇒ Object
-
#initialize(name, request_method, path = nil, example_input: nil, content_type: nil, prepend: nil, only_associated: false, cacheable: false, callbacks: true, &block) ⇒ Action
constructor
A new instance of Action.
- #initialize_copy(new_instance) ⇒ Object
- #path_regex(base, name, include_path) ⇒ Object
- #response(**options, &block) ⇒ Object
- #supports?(request, base, name, include_path) ⇒ Boolean
- #supports_content_type?(request) ⇒ Boolean
- #supports_path?(request, base, name, include_path) ⇒ Boolean
- #supports_request_method?(request) ⇒ Boolean
Methods included from JSONAPIonify::Autoload
autoload_all, eager_load!, unloaded
Methods included from Dummy
Methods included from Documentation
#build_sample_resource_indentifier, #documentation_object, #example_input, #example_requests
Constructor Details
#initialize(name, request_method, path = nil, example_input: nil, content_type: nil, prepend: nil, only_associated: false, cacheable: false, callbacks: true, &block) ⇒ Action
Returns a new instance of Action.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/jsonapionify/api/action.rb', line 14 def initialize(name, request_method, path = nil, example_input: nil, content_type: nil, prepend: nil, only_associated: false, cacheable: false, callbacks: true, &block) @request_method = request_method @path = path || '' @prepend = prepend @only_associated = only_associated @name = name @example_input = example_input @content_type = content_type || 'application/vnd.api+json' @block = block || proc {} @responses = [] @cacheable = cacheable @callbacks = callbacks end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
10 11 12 |
# File 'lib/jsonapionify/api/action.rb', line 10 def block @block end |
#cacheable ⇒ Object (readonly)
Returns the value of attribute cacheable.
10 11 12 |
# File 'lib/jsonapionify/api/action.rb', line 10 def cacheable @cacheable end |
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
10 11 12 |
# File 'lib/jsonapionify/api/action.rb', line 10 def callbacks @callbacks end |
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
10 11 12 |
# File 'lib/jsonapionify/api/action.rb', line 10 def content_type @content_type end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/jsonapionify/api/action.rb', line 10 def name @name end |
#only_associated ⇒ Object (readonly)
Returns the value of attribute only_associated.
10 11 12 |
# File 'lib/jsonapionify/api/action.rb', line 10 def only_associated @only_associated end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
10 11 12 |
# File 'lib/jsonapionify/api/action.rb', line 10 def path @path end |
#prepend ⇒ Object (readonly)
Returns the value of attribute prepend.
10 11 12 |
# File 'lib/jsonapionify/api/action.rb', line 10 def prepend @prepend end |
#request_method ⇒ Object (readonly)
Returns the value of attribute request_method.
10 11 12 |
# File 'lib/jsonapionify/api/action.rb', line 10 def request_method @request_method end |
#responses ⇒ Object (readonly)
Returns the value of attribute responses.
10 11 12 |
# File 'lib/jsonapionify/api/action.rb', line 10 def responses @responses end |
Instance Method Details
#==(other) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/jsonapionify/api/action.rb', line 65 def ==(other) self.class == other.class && %i{@request_method @path @content_type @prepend}.all? do |ivar| instance_variable_get(ivar) == other.instance_variable_get(ivar) end end |
#build_path(base, name, include_path) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/jsonapionify/api/action.rb', line 45 def build_path(base, name, include_path) File.join(*[base].tap do |parts| parts << prepend if prepend parts << name parts << path if path.present? && include_path end) end |
#call(resource, request, callbacks: self.callbacks, **opts) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/jsonapionify/api/action.rb', line 97 def call(resource, request, callbacks: self.callbacks, **opts) resource.new( request: request, callbacks: callbacks, cacheable: cacheable, action: self, **opts ).call end |
#initialize_copy(new_instance) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/jsonapionify/api/action.rb', line 35 def initialize_copy(new_instance) super %i{@responses}.each do |ivar| value = instance_variable_get(ivar) new_instance.instance_variable_set( ivar, value.frozen? ? value : value.dup ) end end |
#path_regex(base, name, include_path) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/jsonapionify/api/action.rb', line 53 def path_regex(base, name, include_path) raw_reqexp = build_path( base, name, include_path ).gsub( ':id', '(?<id>[^\/]+)' ).gsub( '/*', '/?[^\/]*' ) Regexp.new('^' + raw_reqexp + '(\.[A-Za-z_-]+)?$') end |
#response(**options, &block) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/jsonapionify/api/action.rb', line 90 def response(**, &block) new_response = Response.new(self, **, &block) @responses.delete new_response @responses.push new_response self end |
#supports?(request, base, name, include_path) ⇒ Boolean
84 85 86 87 88 |
# File 'lib/jsonapionify/api/action.rb', line 84 def supports?(request, base, name, include_path) supports_path?(request, base, name, include_path) && supports_request_method?(request) && supports_content_type?(request) end |
#supports_content_type?(request) ⇒ Boolean
76 77 78 |
# File 'lib/jsonapionify/api/action.rb', line 76 def supports_content_type?(request) @content_type == request.content_type || !request.has_body? end |
#supports_path?(request, base, name, include_path) ⇒ Boolean
72 73 74 |
# File 'lib/jsonapionify/api/action.rb', line 72 def supports_path?(request, base, name, include_path) request.path_info.match(path_regex(base, name, include_path)) end |
#supports_request_method?(request) ⇒ Boolean
80 81 82 |
# File 'lib/jsonapionify/api/action.rb', line 80 def supports_request_method?(request) request.request_method == @request_method end |