Class: ApiAuth::RequestDrivers::NetHttpRequest

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/api_auth/request_drivers/net_http.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods included from Helpers

#b64_encode, #capitalize_keys, #md5_base64digest

Constructor Details

#initialize(request) ⇒ NetHttpRequest

Returns a new instance of NetHttpRequest.



6
7
8
9
10
# File 'lib/api_auth/request_drivers/net_http.rb', line 6

def initialize(request)
  @request = request
  @headers = fetch_headers
  true
end

Instance Method Details

#authorization_headerObject



74
75
76
# File 'lib/api_auth/request_drivers/net_http.rb', line 74

def authorization_header
  find_header %w(Authorization AUTHORIZATION HTTP_AUTHORIZATION)
end

#calculated_md5Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/api_auth/request_drivers/net_http.rb', line 18

def calculated_md5
  if @request.respond_to?(:body_stream) && @request.body_stream
    body = @request.body_stream.read
    @request.body_stream.rewind
  else
    body = @request.body
  end

  md5_base64digest(body || '')
end

#content_md5Object



56
57
58
59
# File 'lib/api_auth/request_drivers/net_http.rb', line 56

def content_md5
  value = find_header(%w(CONTENT-MD5 CONTENT_MD5))
  value.nil? ? '' : value
end

#content_typeObject



51
52
53
54
# File 'lib/api_auth/request_drivers/net_http.rb', line 51

def content_type
  value = find_header(%w(CONTENT-TYPE CONTENT_TYPE HTTP_CONTENT_TYPE))
  value.nil? ? '' : value
end

#fetch_headersObject



43
44
45
# File 'lib/api_auth/request_drivers/net_http.rb', line 43

def fetch_headers
  @request
end

#http_methodObject



47
48
49
# File 'lib/api_auth/request_drivers/net_http.rb', line 47

def http_method
  @request.method.upcase
end

#md5_mismatch?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
# File 'lib/api_auth/request_drivers/net_http.rb', line 35

def md5_mismatch?
  if @request.class::REQUEST_HAS_BODY
    calculated_md5 != content_md5
  else
    false
  end
end

#populate_content_md5Object



29
30
31
32
33
# File 'lib/api_auth/request_drivers/net_http.rb', line 29

def populate_content_md5
  if @request.class::REQUEST_HAS_BODY
    @request['Content-MD5'] = calculated_md5
  end
end

#request_uriObject



61
62
63
# File 'lib/api_auth/request_drivers/net_http.rb', line 61

def request_uri
  @request.path
end

#set_auth_header(header) ⇒ Object



12
13
14
15
16
# File 'lib/api_auth/request_drivers/net_http.rb', line 12

def set_auth_header(header)
  @request['Authorization'] = header
  @headers = fetch_headers
  @request
end

#set_dateObject



65
66
67
# File 'lib/api_auth/request_drivers/net_http.rb', line 65

def set_date
  @request['DATE'] = Time.now.utc.httpdate
end

#timestampObject



69
70
71
72
# File 'lib/api_auth/request_drivers/net_http.rb', line 69

def timestamp
  value = find_header(%w(DATE HTTP_DATE))
  value.nil? ? '' : value
end