Module: Conduit::Core::Action::InstanceMethods
- Extended by:
- Forwardable
- Defined in:
- lib/conduit/core/action.rb
Instance Method Summary collapse
-
#attributes_with_values ⇒ Object
Returns a hash of all the defined attributes and their values.
- #initialize(**options) ⇒ Object
-
#perform ⇒ Object
Entry method.
-
#perform_request ⇒ Object
Method called to make the actual request.
-
#view ⇒ Object
Return the rendered view.
-
#view_context ⇒ Object
The view’s context will be the action’s decorator.
-
#view_path ⇒ Object
Location where the view files can be found Default to lib/conduit/drivers/<drivername>/views Can be overriden per class.
Instance Method Details
#attributes_with_values ⇒ Object
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] = [attribute] end end.tap { |avs| avs[:options] = } end |
#initialize(**options) ⇒ Object
80 81 82 83 |
# File 'lib/conduit/core/action.rb', line 80 def initialize(**) = validate!() end |
#perform ⇒ Object
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, ) mocker.with_mocking { perform_request } else perform_request end end |
#perform_request ⇒ Object
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 |
#view ⇒ Object
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_context ⇒ Object
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_path ⇒ Object
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 |