Class: WebPipe::Conn
- Inherits:
-
Dry::Struct
- Object
- Dry::Struct
- WebPipe::Conn
- Includes:
- WebPipe::ConnSupport::Types, Container, Cookies, DrySchema, DryView, Flash, NotFound, Params, Rails, Redirect, Session, Url
- Defined in:
- lib/web_pipe/conn.rb
Overview
Struct and methods about web request and response data.
It is meant to contain all the data coming from a web request along with all the data needed to build a web response. It can be built with WebPipe::ConnSupport::Builder.
Besides data fetching methods and #rack_response, any other method returns a fresh new instance of it, so it is thought to be used in an immutable way and to allow chaining of method calls.
There are two subclasses (two types) for this: Ongoing and Halted. WebPipe::ConnSupport::Builder constructs a Ongoing struct, while #halt copies the data to a Halted instance. The intention of this is to halt operations on the web request/response cycle one a Halted instance is detected.
Defined Under Namespace
Instance Attribute Summary collapse
-
#bag ⇒ Bag[]
readonly
Hash where anything can be stored.
-
#config ⇒ Bag[]
readonly
Instance level configuration.
-
#env ⇒ Env[]
readonly
Rack env hash.
-
#host ⇒ Host[]
readonly
Host being requested.
-
#ip ⇒ IP[]
readonly
IP being requested.
-
#path_info ⇒ PathInfo[]
readonly
Besides #script_name, the remainder path of the URL or the empty string if none.
-
#port ⇒ Port[]
readonly
Port in which the request is made.
-
#query_string ⇒ QueryString[]
readonly
Query String of the URL (everything after
?
, or the empty string if none). -
#request ⇒ Request[]
readonly
Rack request.
-
#request_body ⇒ RequestBody[]
readonly
Body sent by the request.
-
#request_headers ⇒ Headers[]
readonly
Hash of request headers.
-
#request_method ⇒ Method[]
readonly
Method of the request.
-
#response_body ⇒ ResponseBody[]
readonly
Body sent by the response.
-
#response_headers ⇒ Headers[]
readonly
Response headers.
-
#scheme ⇒ Scheme[]
readonly
Scheme of the request.
-
#script_name ⇒ ScriptName[]
readonly
Script name in the URL, or the empty string if none.
-
#status ⇒ Status[]
readonly
Status sent by the response.
Instance Method Summary collapse
-
#add(key, value) ⇒ Conn
Writes an item to the #bag.
-
#add_config(key, value) ⇒ Conn
Writes an item to #config.
-
#add_flash(key, value) ⇒ Object
included
from Flash
Adds an item to the flash bag to be consumed by next request.
-
#add_flash_now(key, value) ⇒ Object
included
from Flash
Adds an item to the flash bag to be consumed by the same request in process.
-
#add_response_header(key, value) ⇒ Conn
Adds given pair to response headers.
-
#add_session(key, value) ⇒ Conn
included
from Session
Adds given key/value pair to the session.
-
#base_url ⇒ String
included
from Url
Base part of the URL.
-
#clear_session ⇒ Conn
included
from Session
Deletes everything from the session.
-
#container ⇒ Any
included
from Container
Returns #config
:container
value. - #delete_cookie(key, opts = Types::EMPTY_HASH) ⇒ Object included from Cookies
-
#delete_response_header(key) ⇒ Conn
Deletes pair with given key from response headers.
-
#delete_session(key) ⇒ Conn
included
from Session
Deletes given key form the session.
-
#fetch(key, default = Types::Undefined) ⇒ Object
Reads an item from #bag.
-
#fetch_config(key, default = Types::Undefined) ⇒ Object
Reads an item from #config.
-
#fetch_session(*args, &block) ⇒ Any
included
from Session
Fetches given key from the session.
-
#flash ⇒ Rack::Flash::FlashHash
included
from Flash
Returns the flash bag.
-
#full_path ⇒ String
included
from Url
URL full path.
-
#halt ⇒ Halted
Copies all the data to a Halted instance and returns it.
-
#halted? ⇒ Bool
Returns whether the instance is Halted.
- #helpers ⇒ Object included from Rails
-
#not_found ⇒ WebPipe::Conn::Halted
included
from NotFound
Generates the not-found response.
- #params(transformation_specs = Types::Undefined) ⇒ Any included from Params
-
#path ⇒ String
included
from Url
URL path.
-
#rack_response ⇒ Object
private
Builds response in the way rack expects.
- #redirect(location, code = 302) ⇒ Object included from Redirect
- #render(*args) ⇒ Object included from Rails
- #request_cookies ⇒ Hash included from Cookies
- #sanitized_params ⇒ Object included from DrySchema
-
#session ⇒ Rack::Session::Abstract::SessionHash
included
from Session
Returns Rack::Session's hash.
- #set_cookie(key, value, opts = Types::EMPTY_HASH) ⇒ Object included from Cookies
-
#set_response_body(content) ⇒ Conn
Sets response body.
-
#set_response_headers(headers) ⇒ Conn
Sets response headers.
-
#set_status(code) ⇒ Conn
Sets response status code.
-
#url ⇒ String
included
from Url
Request URL.
- #url_helpers ⇒ Object included from Rails
-
#view(view_spec, **kwargs) ⇒ Object
included
from DryView
Sets string output of a view as response body.
Instance Attribute Details
#bag ⇒ Bag[] (readonly)
Hash where anything can be stored. Keys must be symbols.
This can be used to store anything that is needed to be consumed downstream in a pipe of operations action on and returning WebPipe::Conn.
212 |
# File 'lib/web_pipe/conn.rb', line 212 attribute :bag, Bag |
#config ⇒ Bag[] (readonly)
Instance level configuration.
It is a hash where anything can be stored. Keys must be symbols.
The idea of it is for extensions to have somewhere to store user provided values they may need at some point.
224 |
# File 'lib/web_pipe/conn.rb', line 224 attribute :config, Bag |
#env ⇒ Env[] (readonly)
Rack env hash.
43 |
# File 'lib/web_pipe/conn.rb', line 43 attribute :env, Env |
#host ⇒ Host[] (readonly)
Host being requested.
85 |
# File 'lib/web_pipe/conn.rb', line 85 attribute :host, Host |
#ip ⇒ IP[] (readonly)
IP being requested.
95 |
# File 'lib/web_pipe/conn.rb', line 95 attribute :ip, Ip |
#path_info ⇒ PathInfo[] (readonly)
Besides #script_name, the remainder path of the URL or the
empty string if none. It is, at least, /
when #script_name
is empty.
This doesn't include the #query_string.
129 |
# File 'lib/web_pipe/conn.rb', line 129 attribute :path_info, PathInfo |
#port ⇒ Port[] (readonly)
Port in which the request is made.
105 |
# File 'lib/web_pipe/conn.rb', line 105 attribute :port, Port |
#query_string ⇒ QueryString[] (readonly)
Query String of the URL (everything after ?
, or the empty
string if none).
140 |
# File 'lib/web_pipe/conn.rb', line 140 attribute :query_string, QueryString |
#request ⇒ Request[] (readonly)
Rack request.
52 |
# File 'lib/web_pipe/conn.rb', line 52 attribute :request, Request |
#request_body ⇒ RequestBody[] (readonly)
Body sent by the request.
150 |
# File 'lib/web_pipe/conn.rb', line 150 attribute :request_body, RequestBody |
#request_headers ⇒ Headers[] (readonly)
Hash of request headers.
As per RFC2616, headers names are case insensitive. Here, they are normalized to PascalCase acting on dashes ('-').
Notice that when a rack server maps headers to CGI-like
variables, both dashes and underscores (_
) are treated as
dashes. Here, they always remain as dashes.
169 |
# File 'lib/web_pipe/conn.rb', line 169 attribute :request_headers, Headers |
#request_method ⇒ Method[] (readonly)
Method of the request.
It is not called :method
in order not to collide with
Object#method.
75 |
# File 'lib/web_pipe/conn.rb', line 75 attribute :request_method, Method |
#response_body ⇒ ResponseBody[] (readonly)
Returns Body sent by the response.
187 |
# File 'lib/web_pipe/conn.rb', line 187 attribute :response_body, ResponseBody |
#response_headers ⇒ Headers[] (readonly)
Response headers.
200 |
# File 'lib/web_pipe/conn.rb', line 200 attribute :response_headers, Headers |
#scheme ⇒ Scheme[] (readonly)
Scheme of the request.
62 |
# File 'lib/web_pipe/conn.rb', line 62 attribute :scheme, Scheme |
#script_name ⇒ ScriptName[] (readonly)
Script name in the URL, or the empty string if none.
115 |
# File 'lib/web_pipe/conn.rb', line 115 attribute :script_name, ScriptName |
Instance Method Details
#add(key, value) ⇒ Conn
Writes an item to the #bag.
If it already exists, it is overwritten.
330 331 332 333 334 |
# File 'lib/web_pipe/conn.rb', line 330 def add(key, value) new( bag: bag.merge(key => value) ) end |
#add_config(key, value) ⇒ Conn
Writes an item to #config.
If it already exists, it is overwritten.
358 359 360 361 362 |
# File 'lib/web_pipe/conn.rb', line 358 def add_config(key, value) new( config: config.merge(key => value) ) end |
#add_flash(key, value) ⇒ Object Originally defined in module Flash
Adds an item to the flash bag to be consumed by next request.
#add_flash_now(key, value) ⇒ Object Originally defined in module Flash
Adds an item to the flash bag to be consumed by the same request in process.
#add_response_header(key, value) ⇒ Conn
Adds given pair to response headers.
key
is normalized.
283 284 285 286 287 288 289 |
# File 'lib/web_pipe/conn.rb', line 283 def add_response_header(key, value) new( response_headers: ConnSupport::Headers.add( response_headers, key, value ) ) end |
#add_session(key, value) ⇒ Conn Originally defined in module Session
Adds given key/value pair to the session.
#base_url ⇒ String Originally defined in module Url
Base part of the URL.
This is #scheme and #host, adding #port unless it is the default one for the scheme.
#container ⇒ Any Originally defined in module Container
Returns WebPipe::Conn#config :container
value
#delete_cookie(key, opts = Types::EMPTY_HASH) ⇒ Object Originally defined in module Cookies
#delete_response_header(key) ⇒ Conn
Deletes pair with given key from response headers.
It accepts a non normalized key.
300 301 302 303 304 305 306 |
# File 'lib/web_pipe/conn.rb', line 300 def delete_response_header(key) new( response_headers: ConnSupport::Headers.delete( response_headers, key ) ) end |
#delete_session(key) ⇒ Conn Originally defined in module Session
Deletes given key form the session.
#fetch(key, default = Types::Undefined) ⇒ Object
Reads an item from #bag.
registered in the bag.
316 317 318 319 320 |
# File 'lib/web_pipe/conn.rb', line 316 def fetch(key, default = Types::Undefined) return bag.fetch(key, default) unless default == Types::Undefined bag.fetch(key) { raise ConnSupport::KeyNotFoundInBagError, key } end |
#fetch_config(key, default = Types::Undefined) ⇒ Object
344 345 346 347 348 |
# File 'lib/web_pipe/conn.rb', line 344 def fetch_config(key, default = Types::Undefined) return config.fetch(key, default) unless default == Types::Undefined config.fetch(key) { raise ConnSupport::KeyNotFoundInConfigError, key } end |
#fetch_session(*args, &block) ⇒ Any Originally defined in module Session
Fetches given key from the session.
#flash ⇒ Rack::Flash::FlashHash Originally defined in module Flash
Returns the flash bag.
is not being used as middleware
#full_path ⇒ String Originally defined in module Url
URL full path.
This is #path with #query_string if present.
#halt ⇒ Halted
Copies all the data to a Halted instance and returns it.
387 388 389 |
# File 'lib/web_pipe/conn.rb', line 387 def halt Halted.new(attributes) end |
#halted? ⇒ Bool
Returns whether the instance is Halted.
394 395 396 |
# File 'lib/web_pipe/conn.rb', line 394 def halted? is_a?(Halted) end |
#helpers ⇒ Object Originally defined in module Rails
#not_found ⇒ WebPipe::Conn::Halted Originally defined in module NotFound
Generates the not-found response
#params(transformation_specs = Types::Undefined) ⇒ Any Originally defined in module Params
#path ⇒ String Originally defined in module Url
URL path.
This is #script_name and #path_info.
#rack_response ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds response in the way rack expects.
It is useful to finish a rack application built with a WebPipe::Conn. After every desired operation has been done, this method has to be called before giving control back to rack.
375 376 377 378 379 380 381 |
# File 'lib/web_pipe/conn.rb', line 375 def rack_response [ status, response_headers, response_body ] end |
#redirect(location, code = 302) ⇒ Object Originally defined in module Redirect
#render(*args) ⇒ Object Originally defined in module Rails
#request_cookies ⇒ Hash Originally defined in module Cookies
#sanitized_params ⇒ Object Originally defined in module DrySchema
#session ⇒ Rack::Session::Abstract::SessionHash Originally defined in module Session
Returns Rack::Session's hash
#set_cookie(key, value, opts = Types::EMPTY_HASH) ⇒ Object Originally defined in module Cookies
#set_response_body(content) ⇒ Conn
Sets response body.
As per rack specification, the response body must respond to
#each
. Here, when given content
responds to :each
it is
set as it is as the new response body. Otherwise, what is set
is a one item array of it.
249 250 251 252 253 |
# File 'lib/web_pipe/conn.rb', line 249 def set_response_body(content) new( response_body: content.respond_to?(:each) ? content : [content] ) end |
#set_response_headers(headers) ⇒ Conn
Sets response headers.
Substitues everything that was present as response headers by the new given hash.
Headers keys are normalized.
267 268 269 270 271 |
# File 'lib/web_pipe/conn.rb', line 267 def set_response_headers(headers) new( response_headers: ConnSupport::Headers.normalize(headers) ) end |
#set_status(code) ⇒ Conn
Sets response status code.
231 232 233 234 235 |
# File 'lib/web_pipe/conn.rb', line 231 def set_status(code) new( status: code ) end |
#url ⇒ String Originally defined in module Url
Request URL.
This is the same as #base_url plus #full_path.
#url_helpers ⇒ Object Originally defined in module Rails
#view(view_spec, **kwargs) ⇒ Object Originally defined in module DryView
Sets string output of a view as response body.
If the view is not a Hanami::View instance, it is resolved from the configured container.
kwargs
is used as the input for the view (the arguments that
Hanami::View#call receives). If they doesn't contain an explicit
context:
key, it can be added through the injection of the
result of a lambda present in context's :view_context
.(see
Hanami::View::Context#with).