Class: Booth::Request

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/booth/request.rb

Overview

Convenience wrapper for Rack::Request.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request:, scope:) ⇒ Request

Initializes a new Booth::Request instance.

Parameters:

  • :request - The request object (ActionDispatch::Request, Rack::Request, or

    +Booth::Request+).
    
  • scope - The controller scope identifier (Symbol).



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/booth/request.rb', line 15

def initialize(request:, scope:)
  # Sometimes we pass the request instance from one `Calls` object to another.
  # In that case, unwrap the underlying request and take that as basis.
  # if request.is_a?(::Booth::Request)
  #   return request if request.scope == scope
  #   #request = request.send(:request)
  # end

  # The underlying `request` is an `ActionDispatch::Request` or a `Rack::Request`.
  # For consistency, we always convert to the more rich interface provided by Rails.
  request = ActionDispatch::Request.new(request.env) if request.is_a?(::Rack::Request)

  @request = request
  @scope = ::Booth::Syntaxes::Scope.call(scope).normalized_scope
end

Instance Attribute Details

#scopeObject (readonly)

A Controller that logs the user in, needs to know in which scope to login to. The scope is an authoritative way for the Rails developer to specify a dedicated area.

For example:

“‘ruby Booth::Userland.new_login(scope: :admin, …) Booth::Userland.new_login(scope: :guest, …) “`

The implementation could then check if a user is already logged in in that scope, while ignoring other scopes.

For convenience, we store the authoritative controller scope here, in the request wrapper.



45
46
47
# File 'lib/booth/request.rb', line 45

def scope
  @scope
end

Instance Method Details

#agentObject



53
54
55
# File 'lib/booth/request.rb', line 53

def agent
  ::Booth::Requests::Agent.call(request:)
end

#authenticationObject



65
66
67
# File 'lib/booth/request.rb', line 65

def authentication
  ::Booth::Requests::Authentication.new(scope:, warden: request.env['warden'])
end

#hostObject



49
50
51
# File 'lib/booth/request.rb', line 49

def host
  ::Booth::Syntaxes::Domain.call(request.host).valid_domain
end

#ipObject



57
58
59
# File 'lib/booth/request.rb', line 57

def ip
  ::Booth::Requests::Ip.call(request:)
end

#locationObject



61
62
63
# File 'lib/booth/request.rb', line 61

def location
  ::Booth::Core::Geolocation.lookup(ip)
end

#must_be_delete!Object



119
120
121
# File 'lib/booth/request.rb', line 119

def must_be_delete!
  request.delete? || raise(::Booth::Errors::MustBeDelete)
end

#must_be_get!Object



107
108
109
# File 'lib/booth/request.rb', line 107

def must_be_get!
  request.get? || raise(::Booth::Errors::MustBeGet)
end

#must_be_html!Object



99
100
101
# File 'lib/booth/request.rb', line 99

def must_be_html!
  request.format.html? || raise(::Booth::Errors::MustBeHtml)
end

#must_be_json!Object



103
104
105
# File 'lib/booth/request.rb', line 103

def must_be_json!
  request.format.json? || raise(::Booth::Errors::MustBeJson)
end

#must_be_logged_in!Object



92
93
94
95
96
97
# File 'lib/booth/request.rb', line 92

def must_be_logged_in!
  return if authentication.logged_in?

  log { "Expected someone to be logged in in scope #{scope.inspect}" }
  raise ::Booth::Errors::NotAuthenticated
end

#must_be_patch!Object



115
116
117
# File 'lib/booth/request.rb', line 115

def must_be_patch!
  request.patch? || raise(::Booth::Errors::MustBePatch)
end

#must_be_post!Object



111
112
113
# File 'lib/booth/request.rb', line 111

def must_be_post!
  request.post? || raise(::Booth::Errors::MustBePost)
end

#paramsObject



81
82
83
84
85
86
# File 'lib/booth/request.rb', line 81

def params
  # Huh, I thought the following line was needed:
  # return request.param if request.params.is_a?(::ActionController::Parameters)

  @params ||= ::ActionController::Parameters.new(request.params)
end

#return_pathObject



88
89
90
# File 'lib/booth/request.rb', line 88

def return_path
  ::Booth::Requests::ReturnPath.call(params:)
end

#session(namespace:) ⇒ Object



69
70
71
# File 'lib/booth/request.rb', line 69

def session(namespace:)
  ::Booth::Requests::Session.new(scope:, namespace:, session: request.session)
end

#storageObject



77
78
79
# File 'lib/booth/request.rb', line 77

def storage
  ::Booth::Requests::Storage.new(scope:, request: self)
end

#sudoObject



73
74
75
# File 'lib/booth/request.rb', line 73

def sudo
  ::Booth::Requests::Sudo.new(scope:, request: self)
end