Class: MessageMediaLookups::BaseController

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

Overview

Base controller.

Direct Known Subclasses

LookupsController

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.



7
8
9
10
11
12
13
14
15
16
# File 'lib/message_media_lookups/controllers/base_controller.rb', line 7

def initialize(http_client: nil, http_call_back: nil)
  @http_client = http_client || FaradayClient.new
  @http_call_back = http_call_back

  @global_headers = {
    'user-agent' => 'messagemedia-lookups-ruby-sdk-1.0.0'
  }
  @logger = Logging.logger[self]
  @logger.info("Instantiated controller class.")
end

Instance Attribute Details

#http_call_backObject

Returns the value of attribute http_call_back.



5
6
7
# File 'lib/message_media_lookups/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/message_media_lookups/controllers/base_controller.rb', line 5

def http_client
  @http_client
end

Instance Method Details

#apply_authentication(request, url, body = nil) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/message_media_lookups/controllers/base_controller.rb', line 26

def apply_authentication(request, url, body=nil)
  if Configuration.hmac_auth_user_name == nil or Configuration.hmac_auth_password == nil
    BasicAuth.apply(request)
  else
    HmacAuth.apply(request, url, body)
  end
end

#execute_request(request, binary: false, name: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/message_media_lookups/controllers/base_controller.rb', line 34

def execute_request(request, binary: false, name: nil)
  @logger.info("Calling the on_before_request method of http_call_back for #{name}.") if @http_call_back
  @http_call_back.on_before_request(request) if @http_call_back

  @logger.info("Merging global headers with endpoint headers for #{name}.")
  APIHelper.clean_hash(request.headers)
  request.headers = @global_headers.clone.merge(request.headers)

  @logger.debug("Raw request for #{name} is: #{request.inspect}")
  response = if binary
               @http_client.execute_as_binary(request)
             else
               @http_client.execute_as_string(request)
             end
  @logger.debug("Raw response for #{name} is: #{response.inspect}")
  @logger.info("Wrapping request and response in a context object for #{name}.")
  context = HttpContext.new(request, response)

  @logger.info("Calling on_after_response method of http_call_back for #{name}.") if @http_call_back
  @http_call_back.on_after_response(context) if @http_call_back

  context
end

#validate_parameters(args) ⇒ Object



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

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

#validate_response(context) ⇒ Object



58
59
60
61
# File 'lib/message_media_lookups/controllers/base_controller.rb', line 58

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