class ImpURI

ImpURI/VERSION.rb ImpURI::VERSION

Constants

VERSION

Attributes

strict[RW]

Public Class Methods

all_but_scheme(uri) click to toggle source
# File lib/ImpURI.rb, line 222
def all_but_scheme(uri)
  uri.split('://').last
end
all_but_userinfo(uri) click to toggle source
# File lib/ImpURI.rb, line 226
def all_but_userinfo(uri)
  if has_userinfo?(uri)
    "#{scheme_with_separator(uri)}#{hostname_and_path(uri)}"
  else
    hostname_and_path(uri)
  end
end
has_ampersand_parameter_separator?(uri) click to toggle source
# File lib/ImpURI.rb, line 206
def has_ampersand_parameter_separator?(uri)
  uri.match(/&/) ? true : false
end
has_colon_path_separator?(uri) click to toggle source
# File lib/ImpURI.rb, line 186
def has_colon_path_separator?(uri)
  hostname_and_path(uri).match(/:/) && !has_port_number?(uri) ? true : false
end
has_parameters?(uri) click to toggle source
# File lib/ImpURI.rb, line 196
def has_parameters?(uri)
  uri.match(/\?/) ? true : false
end
has_port_number?(uri) click to toggle source
# File lib/ImpURI.rb, line 191
def has_port_number?(uri)
  hostname_and_path(uri).match(/:\d+/) ? true : false
end
has_scheme?(uri) click to toggle source
# File lib/ImpURI.rb, line 181
def has_scheme?(uri)
  uri.match(/^[a-z]*?:\/\//) ? true : false
end
has_semicolon_parameter_separator?(uri) click to toggle source
# File lib/ImpURI.rb, line 201
def has_semicolon_parameter_separator?(uri)
  uri.match(/;/) ? true : false
end
has_userinfo?(uri) click to toggle source
# File lib/ImpURI.rb, line 158
def has_userinfo?(uri)
  if has_scheme?(uri)
    partial_decomposition = uri.split('//').all_but_first.join.split('/')
    if partial_decomposition.size > 1
      next_decomposition = partial_decomposition.all_but_last
    else
      next_decomposition = [partial_decomposition.first]
    end
    next_decomposition.join('/').match(/@/) ? true : false
  else
    uri.match(/.+:.+@/) ? true : false
  end
end
has_username?(uri) click to toggle source
# File lib/ImpURI.rb, line 173
def has_username?(uri)
  if has_scheme?(uri)
    all_but_scheme(uri).scan('@').first ? true : false
  else
    uri.scan('@').first ? true : false
  end
end
hostname(uri) click to toggle source
# File lib/ImpURI.rb, line 77
def hostname(uri)
  hostname_and_port_number(uri).split(':').first
end
hostname_and_path(uri) click to toggle source
# File lib/ImpURI.rb, line 136
def hostname_and_path(uri)
  if has_userinfo?(uri) || has_username?(uri)
    if all_but_scheme(uri).split('@').size > 1
      all_but_scheme(uri).split('@').all_but_first.join('@')
    else
      all_but_scheme(uri).split('@').first
    end
  else
    all_but_scheme(uri)
  end
end
hostname_and_port_number(uri) click to toggle source
# File lib/ImpURI.rb, line 149
def hostname_and_port_number(uri)
  if has_colon_path_separator?(uri)
    hostname_and_path(uri).split(':').all_but_last.join(':')
  else
    hostname_and_path(uri).split('/').first
  end
end
hostname_optionally_with_port_number(uri) click to toggle source
# File lib/ImpURI.rb, line 254
def hostname_optionally_with_port_number(uri)
  if port_number(uri).blank?
    "#{hostname(uri)}"
  else
    "#{hostname(uri)}:#{port_number(uri)}"
  end
end
is_ssh?(uri) click to toggle source
# File lib/ImpURI.rb, line 263
def is_ssh?(uri)
  !has_scheme?(uri) && has_colon_path_separator?(uri) ? true : false
end
new(uri, *args) click to toggle source
# File lib/ImpURI.rb, line 269
def initialize(uri, *args)
  options = args.extract_options!
  if options[:strict]
    raise SchemeMissingError if !ImpURI.has_scheme?(uri)
    raise ColonPathSeparatorsNotAllowedError if ImpURI.has_colon_path_separator?(uri)
  end
  @uri = uri
end
parameter_separator(uri) click to toggle source
# File lib/ImpURI.rb, line 211
def parameter_separator(uri)
  if has_ampersand_parameter_separator?(uri)
    '&'
  elsif has_semicolon_parameter_separator?(uri)
    ';'
  else
    nil
  end
end
parameter_string(uri) click to toggle source
# File lib/ImpURI.rb, line 110
def parameter_string(uri)
  parameter_string = has_parameters?(uri) ? request_uri(uri).split('?').last : nil
  parameter_string.blank? ? nil : parameter_string
end
parameters(uri) click to toggle source
# File lib/ImpURI.rb, line 116
def parameters(uri)
  if non_nil_parameter_string = parameter_string(uri)
    parameter_parts = (
      case parameter_separator(uri)
      when '&'; non_nil_parameter_string.split('&')
      when ';'; non_nil_parameter_string.split(';')
      else [non_nil_parameter_string]
      end
    )
    parameter_parts.inject({}) do |h,pairs|
      a = pairs.split('=')
      h[a[0]] = a[1]
      h
    end
  else
    nil
  end
end
parse(uri, *args) click to toggle source
# File lib/ImpURI.rb, line 29
def parse(uri, *args)
  ImpURI.new(uri, *args)
end
password(uri) click to toggle source
# File lib/ImpURI.rb, line 68
def password(uri)
  if has_userinfo?(uri)
    userinfo(uri).split(':')[1]
  else
    nil
  end
end
path(uri) click to toggle source
# File lib/ImpURI.rb, line 101
def path(uri)
  if non_nil_request_uri = request_uri(uri)
    request_uri = non_nil_request_uri.split('?').first
    request_uri.blank? ? nil : request_uri
  else
    nil
  end
end
port_number(uri) click to toggle source
# File lib/ImpURI.rb, line 82
def port_number(uri)
  if has_port_number?(uri)
    hostname_and_port_number(uri).split(':').last
  else
    nil
  end
end
request_uri(uri) click to toggle source

For the instance method providing compatibility to Ruby's URI.

# File lib/ImpURI.rb, line 92
def request_uri(uri)
  if has_colon_path_separator?(uri)
    path = hostname_and_path(uri).split(':').all_but_first.join('/')
  else
    path = hostname_and_path(uri).split('/').all_but_first.join('/')
    path.blank? ? nil : '/' + path
  end
end
scheme(uri) click to toggle source
# File lib/ImpURI.rb, line 33
def scheme(uri)
  if has_scheme?(uri)
    uri.split('://').first
  else
    nil
  end
end
scheme_with_separator(uri) click to toggle source
# File lib/ImpURI.rb, line 234
def scheme_with_separator(uri)
  if scheme(uri).blank?
    ''
  else
    "#{scheme(uri)}://"
  end
end
userinfo(uri) click to toggle source
# File lib/ImpURI.rb, line 42
def userinfo(uri)
  if has_userinfo?(uri)
    if all_but_scheme(uri).split('@').size > 1
      all_but_scheme(uri).split('@').all_but_last.join('@')
    else
      all_but_scheme(uri).split('@').first
    end
  elsif has_username?(uri)
    uri.split('@').first
  else
    nil
  end
end
userinfo_with_separator(uri) click to toggle source
# File lib/ImpURI.rb, line 243
def userinfo_with_separator(uri)
  if username(uri).blank?
    ''
  elsif password(uri).blank?
    "#{username(uri)}@"
  else
    "#{username(uri)}:#{password(uri)}@"
  end
end
username(uri) click to toggle source
# File lib/ImpURI.rb, line 57
def username(uri)
  if has_userinfo?(uri)
    userinfo(uri).split(':')[0]
  elsif has_username?(uri)
    uri.split('@').first
  else
    nil
  end
end

Public Instance Methods

all_but_scheme() click to toggle source
# File lib/ImpURI.rb, line 426
def all_but_scheme
  if has_colon_path_separator?
    "#{userinfo_with_separator}#{hostname_optionally_with_port_number}:#{path}"
  else
    "#{userinfo_with_separator}#{hostname_optionally_with_port_number}#{path}"
  end
end
has_ampersand_parameter_separator?() click to toggle source
# File lib/ImpURI.rb, line 507
def has_ampersand_parameter_separator?
  ImpURI.has_ampersand_parameter_separator?(@uri)
end
has_colon_path_separator?() click to toggle source
# File lib/ImpURI.rb, line 495
def has_colon_path_separator?
  ImpURI.has_colon_path_separator?(@uri)
end
has_hostname?() click to toggle source
# File lib/ImpURI.rb, line 463
def has_hostname?
  hostname.nil? ? false : true
end
has_parameter_string?() click to toggle source
# File lib/ImpURI.rb, line 478
def has_parameter_string?
  parameter_string.nil? ? false : true
end
has_parameters?() click to toggle source
# File lib/ImpURI.rb, line 483
def has_parameters?
  parameters.nil? ? false : true
end
has_password?() click to toggle source
# File lib/ImpURI.rb, line 458
def has_password?
  password.nil? ? false : true
end
has_path?() click to toggle source
# File lib/ImpURI.rb, line 473
def has_path?
  path.nil? ? false : true
end
has_port_number?() click to toggle source
# File lib/ImpURI.rb, line 468
def has_port_number?
  port_number.nil? ? false : true
end
has_scheme?() click to toggle source

attribute boolean methods

# File lib/ImpURI.rb, line 448
def has_scheme?
  scheme.nil? ? false : true
end
has_semicolon_parameter_separator?() click to toggle source
# File lib/ImpURI.rb, line 500
def has_semicolon_parameter_separator?
  ImpURI.has_semicolon_parameter_separator?(@uri)
end
has_userinfo?() click to toggle source

derived attribute boolean methods

# File lib/ImpURI.rb, line 490
def has_userinfo?
  ImpURI.has_userinfo?(@uri)
end
has_username?() click to toggle source
# File lib/ImpURI.rb, line 453
def has_username?
  username.nil? ? false : true
end
hostname() click to toggle source
# File lib/ImpURI.rb, line 313
def hostname
  @hostname ||= ImpURI.hostname(@uri)
end
hostname=(new_hostname) click to toggle source
# File lib/ImpURI.rb, line 318
def hostname=(new_hostname)
  @hostname = new_hostname
  @uri = to_s
end
hostname_and_path() click to toggle source
# File lib/ImpURI.rb, line 410
def hostname_and_path
  if has_colon_path_separator?
    "#{hostname}:#{path}"
  else
    "#{hostname}#{path}"
  end
end
hostname_and_port_number() click to toggle source
# File lib/ImpURI.rb, line 418
def hostname_and_port_number
  if has_colon_path_separator?
    hostname_and_path.split(':').all_but_last.join(':')
  else
    hostname_and_path.split('/').first
  end
end
hostname_optionally_with_port_number() click to toggle source
# File lib/ImpURI.rb, line 401
def hostname_optionally_with_port_number
  if has_port_number?
    "#{hostname}:#{port_number}"
  else
    "#{hostname}"
  end
end
is_ssh?() click to toggle source
# File lib/ImpURI.rb, line 512
def is_ssh?
  ImpURI.is_ssh?(to_s)
end
parameter_string() click to toggle source
# File lib/ImpURI.rb, line 344
def parameter_string
  @parameter_string ||= ImpURI.parameter_string(@uri)
end
parameter_string=(new_parameter_string) click to toggle source
# File lib/ImpURI.rb, line 348
def parameter_string=(new_parameter_string)
  @parameter_string = new_parameter_string
  @uri = to_s
  @parameters = ImpURI.parameters(@uri)
end
parameters() click to toggle source
# File lib/ImpURI.rb, line 354
def parameters
  @parameters ||= ImpURI.parameters(@uri)
end
parameters=(new_parameters) click to toggle source
# File lib/ImpURI.rb, line 358
def parameters=(new_parameters)
  @parameters = new_parameters
  @parameter_string = @parameters.x_www_form_urlencode
  @uri = to_s
end
password() click to toggle source
# File lib/ImpURI.rb, line 302
def password
  @password ||= ImpURI.password(@uri)
end
password=(new_password) click to toggle source
# File lib/ImpURI.rb, line 307
def password=(new_password)
  @password = new_password
  @uri = to_s
end
path() click to toggle source
# File lib/ImpURI.rb, line 335
def path
  @path ||= ImpURI.path(@uri)
end
path=(new_path) click to toggle source
# File lib/ImpURI.rb, line 339
def path=(new_path)
  @path = new_path
  @uri = to_s
end
port_number() click to toggle source
# File lib/ImpURI.rb, line 324
def port_number
  @port_number ||= ImpURI.port_number(@uri)
end
port_number=(new_port_number) click to toggle source
# File lib/ImpURI.rb, line 329
def port_number=(new_port_number)
  @port_number = new_port_number
  @uri = to_s
end
request_uri() click to toggle source

For compatability with Ruby's URI. Not quite a drop-in replacement for most aspects as yet however. Perhaps it should be in a compatability module and included.

# File lib/ImpURI.rb, line 366
def request_uri
  @request_uri ||= ImpURI.request_uri(@uri)
end
scheme() click to toggle source

attributes

# File lib/ImpURI.rb, line 280
def scheme
  @scheme ||= ImpURI.scheme(@uri)
end
scheme=(new_scheme) click to toggle source
# File lib/ImpURI.rb, line 285
def scheme=(new_scheme)
  @scheme = new_scheme
  @uri = to_s
end
scheme_with_separator() click to toggle source

derived attributes

# File lib/ImpURI.rb, line 372
def scheme_with_separator
  if scheme.blank?
    ''
  else
    "#{scheme}://"
  end
end
to_h() click to toggle source
# File lib/ImpURI.rb, line 442
def to_h
  {scheme: scheme, username: username, password: password, hostname: hostname, port_number: port_number, path: path, parameter_string: parameter_string}
end
to_s() click to toggle source
# File lib/ImpURI.rb, line 434
def to_s
  if has_parameter_string?
    "#{scheme_with_separator}#{all_but_scheme}?#{parameter_string}"
  else
    "#{scheme_with_separator}#{all_but_scheme}"
  end
end
userinfo() click to toggle source
# File lib/ImpURI.rb, line 381
def userinfo
  if password
    "#{username}:#{password}"
  else
    "#{username}"
  end
end
userinfo_with_separator() click to toggle source
# File lib/ImpURI.rb, line 390
def userinfo_with_separator
  if username.blank?
    ''
  elsif password.blank?
    "#{username}@"
  else
    "#{username}:#{password}@"
  end
end
username() click to toggle source
# File lib/ImpURI.rb, line 291
def username
  @username ||= ImpURI.username(@uri)
end
username=(new_username) click to toggle source
# File lib/ImpURI.rb, line 296
def username=(new_username)
  @username = new_username
  @uri = to_s
end