Module: Conduit::Core::Action::InstanceMethods

Extended by:
Forwardable
Defined in:
lib/conduit/core/action.rb

Instance Method Summary collapse

Instance Method Details

#attributes_with_valuesObject

Returns a hash of all the defined attributes and their values.

If an attribute’s value is not passed as an option it will default to nil.



109
110
111
112
113
114
115
# File 'lib/conduit/core/action.rb', line 109

def attributes_with_values
  attributes.inject({}) do |hash, attribute|
    hash.tap do |h|
      h[attribute] = @options[attribute]
    end
  end
end

#initialize(**options) ⇒ Object



83
84
85
86
# File 'lib/conduit/core/action.rb', line 83

def initialize(**options)
  @options = options
  validate!(options)
end

#performObject

Entry method. Calls either the mocker or the perform_request method.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/conduit/core/action.rb', line 147

def perform
  # When testing we can mock the request. The mocker used
  # will depend on the action's name. For example:
  # `Conduit::MyDriver::RequestMocker::Activate` will be responsible for
  # mocking the `activate` action.
  #
  # * To mock success pass `mock_status: 'success'` as an option.
  # * To mock failure pass `mock_status: 'failure'` as an option.
  if mock_mode?
    mocker = request_mocker.new(self, @options)
    mocker.with_mocking { perform_request }
  else
    perform_request
  end
end

#perform_requestObject

Method called to make the actual request.

Override to customize.



137
138
139
140
141
142
# File 'lib/conduit/core/action.rb', line 137

def perform_request
  response = request(body: view, method: :post)
  parser_instance = parser.new(response.body)

  Conduit::ApiResponse.new(raw_response: response, parser: parser_instance)
end

#viewObject

Return the rendered view



127
128
129
130
131
# File 'lib/conduit/core/action.rb', line 127

def view
  tpl = self.class.name.demodulize
    .underscore.downcase
  render(tpl)
end

#view_contextObject

The view’s context will be the action’s decorator. The decorator name depends on the current action’s name.

For example Conduit::MyDriver::Decorators::ActivateDecorator will be the view’s context for the activate action.



97
98
99
100
101
# File 'lib/conduit/core/action.rb', line 97

def view_context
  view_decorator.new(
    OpenStruct.new(attributes_with_values)
  )
end

#view_pathObject

Location where the view files can be found Default to lib/conduit/drivers/<drivername>/views Can be overriden per class.



121
122
123
# File 'lib/conduit/core/action.rb', line 121

def view_path
  File.join(File.dirname(action_path), 'views')
end