Class: RapidApi::Services::BaseService

Inherits:
Object
  • Object
show all
Defined in:
lib/rapid_api/services/base_service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ BaseService

Returns a new instance of BaseService.



30
31
32
33
34
# File 'lib/rapid_api/services/base_service.rb', line 30

def initialize(params={})
  @params   = params
  @response = ServiceResponse.new
  after_initialize
end

Instance Attribute Details

#bang_modeObject

Returns the value of attribute bang_mode.



5
6
7
# File 'lib/rapid_api/services/base_service.rb', line 5

def bang_mode
  @bang_mode
end

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/rapid_api/services/base_service.rb', line 5

def params
  @params
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/rapid_api/services/base_service.rb', line 5

def response
  @response
end

Class Method Details

.method_missing(meth, *args, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rapid_api/services/base_service.rb', line 9

def self.method_missing(meth, *args, &block)
  if meth[-1] == '!'
    meth = meth.to_s.split('!')[0].to_sym
    bang_mode = true
  else
    bang_mode = false
  end
  if instance_methods.include? meth
    service = self.new(*args)
    service.bang_mode = bang_mode
    service.send(meth)
    if bang_mode
      return service.response.value
    else
      return service.response
    end
  else
    super
  end
end

Instance Method Details

#add_error(key, message) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/rapid_api/services/base_service.rb', line 39

def add_error(key, message)
  raise "#{key}: #{message}" if bang_mode

  if response.errors.key? key
    response.errors[key] << message
  else
    response.errors[key] = []
    response.errors[key] << message
  end
end

#after_initializeObject



36
37
# File 'lib/rapid_api/services/base_service.rb', line 36

def after_initialize
end

#return_value(value) ⇒ Object



50
51
52
# File 'lib/rapid_api/services/base_service.rb', line 50

def return_value(value)
  response.value = value
end