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

#http_bodyObject



142
143
144
# File 'lib/conduit/core/action.rb', line 142

def http_body
  view
end

#http_headersObject



150
151
152
# File 'lib/conduit/core/action.rb', line 150

def http_headers
  {}
end

#http_methodObject



146
147
148
# File 'lib/conduit/core/action.rb', line 146

def http_method
  :post
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.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/conduit/core/action.rb', line 157

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.



132
133
134
135
136
# File 'lib/conduit/core/action.rb', line 132

def perform_request
  response = request(request_options)
  parser_instance = parser_class.new(response.body)
  Conduit::ApiResponse.new(raw_response: response, parser: parser_instance)
end

#request_optionsObject



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

def request_options
  { body: http_body, method: http_method, headers: http_headers }
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