Class: ApiService::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/api_service/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, initial_state = {}) ⇒ Base

Returns a new instance of Base.



20
21
22
23
24
# File 'lib/api_service/base.rb', line 20

def initialize(params, initial_state = {})
  @response = nil
  @params = params
  @state = ActionState.new(initial_state)
end

Class Method Details

.actionsObject



7
8
9
# File 'lib/api_service/base.rb', line 7

def actions
  @actions ||= []
end

.add_action(name, options = {}, &action_body) ⇒ Object



15
16
17
# File 'lib/api_service/base.rb', line 15

def add_action(name, options = {}, &action_body)
  actions << Action.new(name, options, action_body)
end

.add_shared_action(name, shared_action, options = {}) ⇒ Object



11
12
13
# File 'lib/api_service/base.rb', line 11

def add_shared_action(name, shared_action, options = {})
  actions << shared_action.new(name, options)
end

Instance Method Details

#performObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/api_service/base.rb', line 26

def perform
  actions_pool = self.class.actions
  actions_pool.each_with_index do |action, index|
    @state.add_current_action(action, index == actions_pool.size - 1)

    action.perform(@params, @state)

    if !@state.valid? && action.abort_on_fail?
      @state.build_failed_response
      break
    end
  end

  @state.response
end