class URL

Main class for managing urls

url = URL.new('https://mail.google.com/mail/?shva=1#mbox')
url.params # => {:shva => '1'}
url.scheme # => 'https'
url.host   # => 'mail.google.com'
url.domain # => 'google.com'
url.subdomain # => ['mail']
url.path   # => '/mail/'
url.hash   # => 'mbox'

url.subdomain = ['my','mail']
url.params[:foo] = 'bar'
url.to_s   # => 'https://my.mail.google.com/mail/?foo=bar&shva=1#mbox'

Constants

VERSION

Attributes

domain[RW]

Attributes of the URL which are editable @returns [String]

format[RW]

Attributes of the URL which are editable @returns [String]

hash[RW]

Attributes of the URL which are editable @returns [String]

params[R]

The params for the request @returns [URL::ParamsHash]

path[R]

The path for the request @returns [URL::Path]

port[RW]

Attributes of the URL which are editable @returns [String]

scheme[RW]

Attributes of the URL which are editable @returns [String]

string[R]
subdomain[R]

Returns array of subdomains @returns [URL::Subdomain]

subdomains[R]

Returns array of subdomains @returns [URL::Subdomain]

Public Class Methods

Service(url) click to toggle source
# File lib/url/service.rb, line 51
def Service url
  Class.new(URL::Service).set_url(url)
end
json_handler() click to toggle source
# File lib/url.rb, line 184
def json_handler
  return @json_handler if @json_handler

  if defined?(Yajl)
    URL.json_handler = URL::YajlHandler
  elsif defined?(JSON)
    URL.json_handler = URL::BaseJSONHandler
  elsif defined?(ActiveSupport::JSON)
    URL.json_handler = URL::ASJSONHandler
  end
end
json_handler=(r) click to toggle source
# File lib/url.rb, line 196
def json_handler=r
  raise ArgumentError, 'Must be a subclass of URL::JSONHandler' unless r.nil? || r < JSONHandler
  @json_handler = r
end
new(str) click to toggle source

Creates a new URL object @param [String] URL the starting url to work with

# File lib/url.rb, line 88
def initialize str
  @string = str
  sp = URI.split(@string)
  @scheme = sp[0]
  @port = sp[3]
  self.path = sp[5]
  @format = @path.gsub(/(.+\.)/,'')
  @hash = sp[8]

  if sp[2]
    host_parts = sp[2].split('.')
    # Detect internationl urls and treat as tld (eg .co.uk)
    if host_parts[-2] == 'co' and host_parts.length > 2
      @domain = host_parts[-3,3].join('.')
      @subdomain = host_parts.first(host_parts.length-3)
    else
      begin
        @domain = host_parts[-2,2].join('.')
        @subdomain = host_parts.first(host_parts.length-2)
      rescue # if there arent at least 2 parts eg: localhost
        @domain = host_parts.join('.')
      end
    end
  else
    @domain = nil
    @subdomain = nil
  end

  @params = ParamsHash.new
  if sp[7]
    sp[7].gsub('?','').split('&').each do |myp|
      key,value = myp.split('=')
      value = CGI.unescape(value) if value
      @params[key.to_sym] = value if key
    end
  end
end
req_handler() click to toggle source

Define the request handler to use. If Typhoeus is setup it will use {TyHandler} otherwise will default back to Net::HTTP with {NetHandler} @return [RequstHandler]

# File lib/url.rb, line 166
def req_handler
  return @req_handler if @req_handler

  if defined?(Typhoeus)
    URL.req_handler = URL::TyHandler
  else
    URL.req_handler = URL::NetHandler
  end
end
req_handler=(r) click to toggle source

Define the request handler to use. If Typhoeus is setup it will use {TyHandler} otherwise will default back to Net::HTTP with {NetHandler} @param [RequstHandler] @return [RequstHandler]

# File lib/url.rb, line 179
def req_handler=r
  raise ArgumentError, 'Must be a subclass of URL::RequestHandler' unless r.nil? || r < RequestHandler
  @req_handler = r
end

Public Instance Methods

=~(reg) click to toggle source
# File lib/url.rb, line 240
def =~ reg
  to_s =~ reg
end
add_to_path(val) click to toggle source
# File lib/url.rb, line 62
def add_to_path val
  unless @path[-1] == 47 # '/'
    @path << '/'
  end

  @path << val.sub(/^\//,'')
  @path
end
delete(*args) click to toggle source

Performs a delete request for the current URL @return [URL::Response] A subclass of string which also repsonds to a few added mthods storing more information

# File lib/url.rb, line 216
def delete(*args)
  req_handler.delete(*args)
end
dup() click to toggle source
# File lib/url.rb, line 230
def dup
  URL.new(to_s)
end
get(*args) click to toggle source

Performs a get request for the current URL @return [URL::Response] A subclass of string which also repsonds to a few added mthods storing more information

# File lib/url.rb, line 204
def get(*args)
  req_handler.get(*args)
end
host() click to toggle source

The full hostname (not including port) for the URL

# File lib/url.rb, line 129
def host
  [@subdomain,@domain].flatten.compact.join('.')
end
host_with_port() click to toggle source

Messed up host/hostname issue :(

# File lib/url.rb, line 134
def host_with_port
  host<<':'<<port.to_s
end
inspect() click to toggle source
# File lib/url.rb, line 226
def inspect
  "#<#{self.class} #{to_s}>"
end
params=(p) click to toggle source

Set the params for the request Allows for url.params |= {:foo => 'bar'} @returns [URL::ParamsHash]

# File lib/url.rb, line 40
def params= p
  raise ArgumentError, 'Params must be a URL::ParamsHash' unless p.is_a?(ParamsHash)
  @params = p
end
path=(str) click to toggle source

Set the path for the request

# File lib/url.rb, line 54
def path=str
  if str.nil? || str.empty?
    str = '/'
  end

  @path = str
end
post(*args) click to toggle source

Performs a post request for the current URL @return [URL::Response] A subclass of string which also repsonds to a few added mthods storing more information

# File lib/url.rb, line 210
def post(*args)
  req_handler.post(*args)
end
put(*args) click to toggle source

Performs a put request for the current URL @return [URL::Response] A subclass of string which also repsonds to a few added mthods storing more information

# File lib/url.rb, line 222
def put(*args)
  req_handler.delete(*args)
end
req_handler() click to toggle source

The request handler for this @return [Handler]

# File lib/url.rb, line 236
def req_handler
  (@req_handler||self.class.req_handler).new(self)
end
req_handler=(r) click to toggle source

Sets the handler to use for this request @param [RequstHandler] @return [RequstHandler]

# File lib/url.rb, line 247
def req_handler=r
  raise ArgumentError, 'Must be a subclass of URL::Handler' unless r < RequestHandler
  @req_handler = r
end
subdomain=(s) click to toggle source

@param [Array,String] subdomain An array or string for subdomain

# File lib/url.rb, line 77
def subdomain=(s)
  if s.is_a?(String)
    s = s.split('.')
  end

  @subdomain = s
end
Also aliased as: subdomains=
subdomains=(s)
Alias for: subdomain=
to_s(ops={}) click to toggle source

Outputs the full current url @param [Hash] ops Prevent certain parts of the object from being shown by setting `:scheme`,`:port`,`:path`,`:params`, or `:hash` to `false` @return [String]

# File lib/url.rb, line 141
def to_s ops={}
  ret = String.new
  ret << %{#{scheme}://} if scheme && ops[:scheme] != false
  ret << host
  ret << %{:#{port}} if port && ops[:port] != false
  if path && ops[:path] != false
    ret << path
  end

  ret << params.to_s if params && ops[:params] != false

  ret << "##{hash.to_s}" if hash && ops[:hash] != false

  ret
end
to_uri() click to toggle source

Returns the parsed URI object for the string @return [URI]

# File lib/url.rb, line 159
def to_uri
  URI.parse(to_s)
end