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.



106
107
108
109
110
111
112
# File 'lib/conduit/core/action.rb', line 106

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

#initialize(**options) ⇒ Object



80
81
82
83
# File 'lib/conduit/core/action.rb', line 80

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

#performObject

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



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

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.



134
135
136
137
138
139
# File 'lib/conduit/core/action.rb', line 134

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

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

#viewObject

Return the rendered view



124
125
126
127
128
# File 'lib/conduit/core/action.rb', line 124

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.



94
95
96
97
98
# File 'lib/conduit/core/action.rb', line 94

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.



118
119
120
# File 'lib/conduit/core/action.rb', line 118

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