Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move URL redundants methods to an extension #24

Merged
merged 1 commit into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/web_pipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ def self.call(*args)
register_extension :session do
require 'web_pipe/extensions/session/session'
end

register_extension :url do
require 'web_pipe/extensions/url/url'
end
end
82 changes: 0 additions & 82 deletions lib/web_pipe/conn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ module WebPipe
class Conn < Dry::Struct
include ConnSupport::Types

# Env's key used to retrieve params set by the router.
#
# @see #router_params
ROUTER_PARAMS_KEY = 'router.params'

# @!attribute [r] env
#
# Rack env hash.
Expand Down Expand Up @@ -215,83 +210,6 @@ class Conn < Dry::Struct
# @return [Bag[]]
attribute :bag, Bag

# Base part of the URL.
#
# This is {#scheme} and {#host}, adding {#port} unless it is the
# default one for the scheme.
#
# @return [BaseUrl]
#
# @example
# 'https://example.org'
# 'http://example.org:8000'
def base_url
request.base_url
end

# URL path.
#
# This is {#script_name} and {#path_info}.
#
# @return [Path]
#
# @example
# 'index.rb/users'
def path
request.path
end

# URL full path.
#
# This is {#path} with {#query_string} if present.
#
# @return [FullPath]
#
# @example
# '/users?id=1'
def full_path
request.fullpath
end

# Request URL.
#
# This is the same as {#base_url} plus {#full_path}.
#
# @return [Url]
#
# @example
# 'http://www.example.org:8000/users?id=1'
def url
request.url
end

# *Params* in rack env's 'router.params' key.
#
# Routers used to map routes to applications build with
# {WebPipe} have the option to introduce extra params through
# setting env's 'router.params' key. These parameters will be
# merged with GET and POST ones when calling {#params}.
#
# This kind of functionality is usually implemented from the
# router side allowing the addition of variables in the route
# definition, e.g.:
#
# @example
# /user/:id/update
def router_params
env.fetch(ROUTER_PARAMS_KEY, Types::EMPTY_HASH)
end

# GET, POST and {#router_params} merged in a hash.
#
# @return [Params]
#
# @example
# { 'id' => '1', 'name' => 'Joe' }
def params
request.params.merge(router_params)
end

# Sets response status code.
#
# @param code [StatusCode]
Expand Down
6 changes: 0 additions & 6 deletions lib/web_pipe/conn_support/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ module Types
QueryString = Strict::String
RequestBody = Interface(:gets, :each, :read, :rewind)

BaseUrl = Strict::String
Path = Strict::String
FullPath = Strict::String
Url = Strict::String
Params = Strict::Hash

Status = Strict::Integer.
default(200).
constrained(gteq: 100, lteq: 599)
Expand Down
95 changes: 95 additions & 0 deletions lib/web_pipe/extensions/url/url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
module WebPipe
# Adds helper methods related to the request URL.
#
# This methods are in fact redundant with the information already
# present in {Conn} struct but, of course, they are very useful.
module Url
# Env's key used to retrieve params set by the router.
#
# @see #router_params
ROUTER_PARAMS_KEY = 'router.params'

# Base part of the URL.
#
# This is {#scheme} and {#host}, adding {#port} unless it is the
# default one for the scheme.
#
# @return [String]
#
# @example
# 'https://example.org'
# 'http://example.org:8000'
def base_url
request.base_url
end

# URL path.
#
# This is {#script_name} and {#path_info}.
#
# @return [String]
#
# @example
# 'index.rb/users'
def path
request.path
end

# URL full path.
#
# This is {#path} with {#query_string} if present.
#
# @return [String]
#
# @example
# '/users?id=1'
def full_path
request.fullpath
end

# Request URL.
#
# This is the same as {#base_url} plus {#full_path}.
#
# @return [String]
#
# @example
# 'http://www.example.org:8000/users?id=1'
def url
request.url
end

# *Params* in rack env's 'router.params' key.
#
# Routers used to map routes to applications build with
# {WebPipe} have the option to introduce extra params through
# setting env's 'router.params' key. These parameters will be
# merged with GET and POST ones when calling {#params}.
#
# This kind of functionality is usually implemented from the
# router side allowing the addition of variables in the route
# definition, e.g.:
#
# @example
# /user/:id/update
#
# @return [Hash]
def router_params
env.fetch(ROUTER_PARAMS_KEY, Types::EMPTY_HASH)
end

# GET, POST and {#router_params} merged in a hash.
#
# @return [Params]
#
# @example
# { 'id' => '1', 'name' => 'Joe' }
#
# @return Hash
def params
request.params.merge(router_params)
end
end

Conn.include(Url)
end
Loading