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_args ⇒ 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 31 32 |
# 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 if opts[:url] @url = opts[:url] else @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.
47 48 49 |
# File 'lib/acfs/operation.rb', line 47 def full_params (id ? params.merge(id: id) : params).merge location_args 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.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/acfs/operation.rb', line 73 def handle_failure(response) case response.code when 400 raise ::Acfs::BadRequest.new response: response when 401 raise ::Acfs::.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.
42 43 44 45 |
# File 'lib/acfs/operation.rb', line 42 def id # TODO @id ||= params.delete(:id) || data[:id] end |
#location_args ⇒ 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.
51 52 53 |
# File 'lib/acfs/operation.rb', line 51 def location_args location ? location.args : {} 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.
55 56 57 |
# File 'lib/acfs/operation.rb', line 55 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.
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/acfs/operation.rb', line 59 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.
34 35 36 |
# File 'lib/acfs/operation.rb', line 34 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.
38 39 40 |
# File 'lib/acfs/operation.rb', line 38 def synchronous? i[update delete create].include? action end |