Class: ApiAuth::RequestDrivers::RestClientRequest

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

Overview

:nodoc:

Instance Method Summary collapse

Methods included from Helpers

#b64_encode, #capitalize_keys

Constructor Details

#initialize(request) ⇒ RestClientRequest

Returns a new instance of RestClientRequest.



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

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

Instance Method Details

#authorization_headerObject



76
77
78
# File 'lib/api_auth/request_drivers/rest_client.rb', line 76

def authorization_header
  find_header %w(Authorization AUTHORIZATION HTTP_AUTHORIZATION)
end

#calculated_md5Object



25
26
27
28
29
30
31
32
33
# File 'lib/api_auth/request_drivers/rest_client.rb', line 25

def calculated_md5
  if @request.payload
    body = @request.payload.read
    @request.payload.instance_variable_get(:@stream).seek(0)
  else
    body = ''
  end
  Digest::MD5.base64digest(body)
end

#content_md5Object



58
59
60
61
# File 'lib/api_auth/request_drivers/rest_client.rb', line 58

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

#content_typeObject



53
54
55
56
# File 'lib/api_auth/request_drivers/rest_client.rb', line 53

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

#fetch_headersObject



49
50
51
# File 'lib/api_auth/request_drivers/rest_client.rb', line 49

def fetch_headers
  capitalize_keys @request.headers
end

#md5_mismatch?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
# File 'lib/api_auth/request_drivers/rest_client.rb', line 41

def md5_mismatch?
  if [:post, :put].include?(@request.method)
    calculated_md5 != content_md5
  else
    false
  end
end

#populate_content_md5Object



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

def populate_content_md5
  if [:post, :put].include?(@request.method)
    @request.headers["Content-MD5"] = calculated_md5
  end
end

#request_uriObject



63
64
65
# File 'lib/api_auth/request_drivers/rest_client.rb', line 63

def request_uri
  @request.url
end

#set_auth_header(header) ⇒ Object



18
19
20
21
22
23
# File 'lib/api_auth/request_drivers/rest_client.rb', line 18

def set_auth_header(header)
  @request.headers.merge!({ "Authorization" => header })
  @headers = fetch_headers
  save_headers # enforce update of processed_headers based on last updated headers
  @request
end

#set_dateObject



67
68
69
# File 'lib/api_auth/request_drivers/rest_client.rb', line 67

def set_date
  @request.headers.merge!({ "DATE" => Time.now.utc.httpdate })
end

#timestampObject



71
72
73
74
# File 'lib/api_auth/request_drivers/rest_client.rb', line 71

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