Class: Acfs::Operation Private
- Inherits:
-
Object
- Object
- Acfs::Operation
- Defined in:
- lib/acfs/operation.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Describes a CRUD operation. Handle request creation and response processing as well as error handling and stubbing.
Instance Attribute Summary collapse
- #action ⇒ Object readonly private
- #callback ⇒ Object readonly private
- #data ⇒ Object readonly private
- #location ⇒ Object readonly private
- #params ⇒ Object readonly private
- #resource ⇒ Object readonly private
- #url ⇒ Object readonly private
Instance Method Summary collapse
- #full_params ⇒ Object private
- #handle_failure(response) ⇒ Object private
- #id ⇒ Object private
-
#initialize(resource, action, **opts, &block) ⇒ Operation
constructor
private
A new instance of Operation.
- #location_vars ⇒ Object private
- #method ⇒ Object private
- #request ⇒ Object private
- #single? ⇒ Boolean private
- #synchronous? ⇒ Boolean private
Constructor Details
#initialize(resource, action, **opts, &block) ⇒ Operation
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Operation.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/acfs/operation.rb', line 15 def initialize(resource, action, **opts, &block) @resource = resource @action = action.to_sym # Operations can be delayed so dup params and data to avoid # later in-place changes by modifying passed hash @params = (opts[:params] || {}).dup @data = (opts[:data] || {}).dup unless (@url = opts[:url]) @location = resource.location(action: @action).extract_from(@params, @data) @url = location.str end @callback = block end |
Instance Attribute Details
#action ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
10 11 12 |
# File 'lib/acfs/operation.rb', line 10 def action @action end |
#callback ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
10 11 12 |
# File 'lib/acfs/operation.rb', line 10 def callback @callback end |
#data ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
10 11 12 |
# File 'lib/acfs/operation.rb', line 10 def data @data end |
#location ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
10 11 12 |
# File 'lib/acfs/operation.rb', line 10 def location @location end |
#params ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
10 11 12 |
# File 'lib/acfs/operation.rb', line 10 def params @params end |
#resource ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
10 11 12 |
# File 'lib/acfs/operation.rb', line 10 def resource @resource end |
#url ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
10 11 12 |
# File 'lib/acfs/operation.rb', line 10 def url @url end |
Instance Method Details
#full_params ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
45 46 47 |
# File 'lib/acfs/operation.rb', line 45 def full_params (id ? params.merge(id: id) : params).merge(location_vars) end |
#handle_failure(response) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/acfs/operation.rb', line 71 def handle_failure(response) case response.code when 400 raise ::Acfs::BadRequest.new response: response when 401 raise ::Acfs::Unauthorized.new response: response when 403 raise ::Acfs::Forbidden.new response: response when 404 raise ::Acfs::ResourceNotFound.new response: response when 422 raise ::Acfs::InvalidResource.new response: response, errors: response.data.try(:[], 'errors') when 500 raise ::Acfs::ServerError.new response: response when 502 raise ::Acfs::BadGateway.new response: response when 503 raise ::Acfs::ServiceUnavailable.new response: response when 504 raise ::Acfs::GatewayTimeout.new response: response else raise ::Acfs::ErroneousResponse.new response: response end end |
#id ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
40 41 42 43 |
# File 'lib/acfs/operation.rb', line 40 def id # TODO @id ||= params.delete(:id) || data[:id] end |
#location_vars ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
49 50 51 |
# File 'lib/acfs/operation.rb', line 49 def location_vars location ? location.vars : {} end |
#method ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
53 54 55 |
# File 'lib/acfs/operation.rb', line 53 def method {read: :get, list: :get, update: :put, create: :post, delete: :delete}[action] end |
#request ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/acfs/operation.rb', line 57 def request request = ::Acfs::Request.new url, method: method, params: params, data: data, operation: self request.on_complete do |response| ::ActiveSupport::Notifications.instrument 'acfs.operation.complete', operation: self, response: response handle_failure response unless response.success? callback.call response.data, response end request end |
#single? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
32 33 34 |
# File 'lib/acfs/operation.rb', line 32 def single? %i[read update delete].include? action end |
#synchronous? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
36 37 38 |
# File 'lib/acfs/operation.rb', line 36 def synchronous? %i[update delete create].include? action end |