Module: Coercive::URI
- Defined in:
- lib/coercive/uri.rb
Class Method Summary collapse
-
.coerce_fn(string_coerce_fn, schema_fn: nil, require_path: false, require_port: false, require_user: false, require_password: false) ⇒ Object
Public DSL: Return a coercion function to coerce input to a URI.
Class Method Details
.coerce_fn(string_coerce_fn, schema_fn: nil, require_path: false, require_port: false, require_user: false, require_password: false) ⇒ Object
Public DSL: Return a coercion function to coerce input to a URI. Used when declaring an attribute. See documentation for attr_coerce_fns.
string_coerce_fn - the string coerce function used to coerce the URI schema_fn - the optional function used to coerce the schema require_path - set true to make the URI path a required element require_port - set true to make the URI port a required element require_user - set true to make the URI user a required element require_password - set true to make the URI password a required element
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/coercive/uri.rb', line 15 def self.coerce_fn(string_coerce_fn, schema_fn: nil, require_path: false, require_port: false, require_user: false, require_password: false) ->(input) do uri = begin ::URI.parse(string_coerce_fn.call(input)) rescue ::URI::InvalidURIError fail Coercive::Error.new("not_valid") end fail Coercive::Error.new("no_host") unless uri.host fail Coercive::Error.new("no_path") if require_path && uri.path.empty? fail Coercive::Error.new("no_port") if require_port && !uri.port fail Coercive::Error.new("no_user") if require_user && !uri.user fail Coercive::Error.new("no_password") if require_password && !uri.password if schema_fn begin schema_fn.call(uri.scheme) rescue Coercive::Error fail Coercive::Error.new("unsupported_schema") end end uri.to_s end end |