Class: Acfs::Operation Private

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

[View source]

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

#actionObject (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

#callbackObject (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

#dataObject (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

#locationObject (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

#paramsObject (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

#resourceObject (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

#urlObject (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_paramsObject

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.

[View source]

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.

[View source]

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

#idObject

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.

[View source]

40
41
42
43
# File 'lib/acfs/operation.rb', line 40

def id
  # TODO
  @id ||= params.delete(:id) || data[:id]
end

#location_varsObject

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.

[View source]

49
50
51
# File 'lib/acfs/operation.rb', line 49

def location_vars
  location ? location.vars : {}
end

#methodObject

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.

[View source]

53
54
55
# File 'lib/acfs/operation.rb', line 53

def method
  {read: :get, list: :get, update: :put, create: :post, delete: :delete}[action]
end

#requestObject

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.

[View source]

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.

Returns:

  • (Boolean)
[View source]

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.

Returns:

  • (Boolean)
[View source]

36
37
38
# File 'lib/acfs/operation.rb', line 36

def synchronous?
  %i[update delete create].include? action
end