Class: Gonebusy::BaseController

Inherits:
Object
  • Object
show all
Defined in:
lib/gonebusy/controllers/base_controller.rb

Constant Summary collapse

@@http_client =
FaradayClient.new
@@global_headers =
{
  'user-agent' => 'APIMATIC 2.0'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_client: nil, http_call_back: nil) ⇒ BaseController

Returns a new instance of BaseController.



13
14
15
16
# File 'lib/gonebusy/controllers/base_controller.rb', line 13

def initialize(http_client: nil, http_call_back: nil)
  @http_client = http_client ||= @@http_client
  @http_call_back = http_call_back
end

Instance Attribute Details

#http_call_backObject

Returns the value of attribute http_call_back.



5
6
7
# File 'lib/gonebusy/controllers/base_controller.rb', line 5

def http_call_back
  @http_call_back
end

#http_clientObject

Returns the value of attribute http_client.



5
6
7
# File 'lib/gonebusy/controllers/base_controller.rb', line 5

def http_client
  @http_client
end

Instance Method Details

#execute_request(request, binary: false) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gonebusy/controllers/base_controller.rb', line 26

def execute_request(request, binary: false)
  @http_call_back.on_before_request(request) if @http_call_back

  APIHelper.clean_hash(request.headers)
  request.headers = @@global_headers.clone.merge(request.headers)

  response = binary ? @http_client.execute_as_binary(request) : @http_client.execute_as_string(request)
  context = HttpContext.new(request, response)

  @http_call_back.on_after_response(context) if @http_call_back

  return context
end

#validate_parameters(args) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/gonebusy/controllers/base_controller.rb', line 18

def validate_parameters(args)
  args.each do |_name, value|
    if value.nil?
      raise ArgumentError.new 'Required parameter #{name} cannot be nil.'
    end
  end
end

#validate_response(context) ⇒ Object



40
41
42
43
44
# File 'lib/gonebusy/controllers/base_controller.rb', line 40

def validate_response(context)
  if !context.response.status_code.between?(200, 208) #[200,208] = HTTP OK
    raise APIException.new 'HTTP Response Not OK', context
  end
end