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.
- #http_body ⇒ Object
- #http_headers ⇒ Object
- #http_method ⇒ Object
- #initialize(options) ⇒ Object
-
#perform ⇒ Object
Entry method.
-
#perform_request ⇒ Object
Method called to make the actual request.
- #request_options ⇒ Object
-
#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] = @options[attribute] end end.tap { |avs| avs[:options] = @options } end |
#http_body ⇒ Object
142 143 144 |
# File 'lib/conduit/core/action.rb', line 142 def http_body view end |
#http_headers ⇒ Object
150 151 152 |
# File 'lib/conduit/core/action.rb', line 150 def http_headers {} end |
#http_method ⇒ Object
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 = validate!() end |
#perform ⇒ Object
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_request ⇒ Object
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() parser_instance = parser_class.new(response.body) Conduit::ApiResponse.new(raw_response: response, parser: parser_instance) end |
#request_options ⇒ Object
138 139 140 |
# File 'lib/conduit/core/action.rb', line 138 def { body: http_body, method: http_method, headers: http_headers } 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 |