Class: Drillbit::Requests::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/drillbit/requests/base.rb

Direct Known Subclasses

Rack, Rails

Constant Summary collapse

BASE64_PATTERN =
%r{[A-Za-z0-9_/\+\=\-\.]}
BASE64_TOKEN_PARAM_NAME =
'token_b64'
JSON_WEB_TOKEN_PARAM_NAME =
'token_jwt'
JSON_WEB_TOKEN_PATTERN =
/(#{BASE64_PATTERN}+?\.){4}#{BASE64_PATTERN}+?/
BASE64_TOKEN_HEADER_PATTERN =
/\A(?:Basic|Bearer)\s+(.*)\z/
JSON_WEB_TOKEN_HEADER_PATTERN =
/\AToken\s+(.*)\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token_private_key: Drillbit.configuration.token_private_key, request:) ⇒ Base

Returns a new instance of Base.



19
20
21
22
23
24
# File 'lib/drillbit/requests/base.rb', line 19

def initialize(token_private_key: Drillbit.configuration.token_private_key,
               request:)

  self.token_private_key = token_private_key
  self.request           = request
end

Instance Attribute Details

#requestObject

Returns the value of attribute request.



16
17
18
# File 'lib/drillbit/requests/base.rb', line 16

def request
  @request
end

#token_private_keyObject

Returns the value of attribute token_private_key.



16
17
18
# File 'lib/drillbit/requests/base.rb', line 16

def token_private_key
  @token_private_key
end

Class Method Details

.rack_request_classObject



77
78
79
80
81
# File 'lib/drillbit/requests/base.rb', line 77

def self.rack_request_class
  require 'drillbit/requests/rack'

  Object.const_get('Drillbit::Requests::Rack')
end

.rails_request_classObject



71
72
73
74
75
# File 'lib/drillbit/requests/base.rb', line 71

def self.rails_request_class
  require 'drillbit/requests/rails'

  Object.const_get('Drillbit::Requests::Rails')
end

.resolve(original_request) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/drillbit/requests/base.rb', line 61

def self.resolve(original_request)
  if original_request.is_a? self
    original_request
  elsif original_request.respond_to? :headers
    rails_request_class.new(request: original_request)
  else
    rack_request_class.new(request: original_request)
  end
end

Instance Method Details

#accept_headerObject



26
27
28
29
30
31
32
33
34
# File 'lib/drillbit/requests/base.rb', line 26

def accept_header
  if accept_header_from_header.valid? ||
     accept_header_from_params.invalid?

    accept_header_from_header
  else
    accept_header_from_params
  end
end

#application_nameObject



53
54
55
# File 'lib/drillbit/requests/base.rb', line 53

def application_name
  raw_request_application_name || Drillbit.configuration.application_name
end

#authorization_tokenObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/drillbit/requests/base.rb', line 36

def authorization_token
  if (
       !authorization_token_from_header.blank? &&
       authorization_token_from_header.valid?
     ) \
     ||
     (
       authorization_token_from_params.blank? ||
       !authorization_token_from_params.valid?
     )

    authorization_token_from_header
  else
    authorization_token_from_params
  end
end

#subdomainObject



57
58
59
# File 'lib/drillbit/requests/base.rb', line 57

def subdomain
  @subdomain ||= raw_host[/\A([a-z\-]+)/i, 1]
end