Class: URL

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/url.rb,
lib/url/handlers.rb,
lib/url/response.rb,
lib/url/helper_classes.rb,
lib/url/handlers/ty_handler.rb,
lib/url/handlers/net_handler.rb,
lib/url/handlers/yajl_handler.rb,
lib/url/handlers/as_json_handler.rb,
lib/url/handlers/base_json_handler.rb

Overview

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'

Defined Under Namespace

Modules: Classer Classes: ASJSONHandler, BaseJSONHandler, JSONHandler, Mash, NetHandler, ParamsHash, RequestHandler, Response, TyHandler, YajlHandler

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ URL

Creates a new URL object

Parameters:

  • URL (String)

    the starting url to work with



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/url.rb', line 67

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('.')
    if host_parts[-2] == 'co'
      @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

Instance Attribute Details

#domainObject

Attributes of the URL which are editable



35
36
37
# File 'lib/url.rb', line 35

def domain
  @domain
end

#formatObject

Attributes of the URL which are editable



35
36
37
# File 'lib/url.rb', line 35

def format
  @format
end

#hashObject

Attributes of the URL which are editable



35
36
37
# File 'lib/url.rb', line 35

def hash
  @hash
end

#paramsObject (readonly)

The params for the request



31
32
33
# File 'lib/url.rb', line 31

def params
  @params
end

#pathObject

The path for the request



39
40
41
# File 'lib/url.rb', line 39

def path
  @path
end

#portObject

Attributes of the URL which are editable



35
36
37
# File 'lib/url.rb', line 35

def port
  @port
end

#schemeObject

Attributes of the URL which are editable



35
36
37
# File 'lib/url.rb', line 35

def scheme
  @scheme
end

#stringObject (readonly)

Returns the value of attribute string.



27
28
29
# File 'lib/url.rb', line 27

def string
  @string
end

#subdomainObject Also known as: subdomains

Returns array of subdomains



52
53
54
# File 'lib/url.rb', line 52

def subdomain
  @subdomain
end

Class Method Details

.json_handlerObject



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/url.rb', line 157

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) ⇒ Object

Raises:

  • (ArgumentError)


169
170
171
172
# File 'lib/url.rb', line 169

def json_handler=r
  raise ArgumentError, 'Must be a subclass of URL::JSONHandler' unless r.nil? || r < JSONHandler
  @json_handler = r
end

.req_handlerRequstHandler

Define the request handler to use. If Typhoeus is setup it will use TyHandler otherwise will default back to Net::HTTP with NetHandler

Returns:

  • (RequstHandler)


139
140
141
142
143
144
145
146
147
# File 'lib/url.rb', line 139

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) ⇒ RequstHandler

Define the request handler to use. If Typhoeus is setup it will use TyHandler otherwise will default back to Net::HTTP with NetHandler

Parameters:

  • (RequstHandler)

Returns:

  • (RequstHandler)

Raises:

  • (ArgumentError)


152
153
154
155
# File 'lib/url.rb', line 152

def req_handler=r
  raise ArgumentError, 'Must be a subclass of URL::RequestHandler' unless r.nil? || r < RequestHandler
  @req_handler = r
end

Instance Method Details

#=~(reg) ⇒ Object



207
208
209
# File 'lib/url.rb', line 207

def =~ reg
  to_s =~ reg
end

#delete(*args) ⇒ URL::Response

Performs a delete request for the current URL

Returns:

  • (URL::Response)

    A subclass of string which also repsonds to a few added mthods storing more information



189
190
191
# File 'lib/url.rb', line 189

def delete(*args)
  req_handler.delete(*args)
end

#dupObject



197
198
199
# File 'lib/url.rb', line 197

def dup
  URL.new(to_s)
end

#get(*args) ⇒ URL::Response

Performs a get request for the current URL

Returns:

  • (URL::Response)

    A subclass of string which also repsonds to a few added mthods storing more information



177
178
179
# File 'lib/url.rb', line 177

def get(*args)
  req_handler.get(*args)
end

#hostObject

The full hostname (not including port) for the URL



107
108
109
# File 'lib/url.rb', line 107

def host
  [@subdomain,@domain].flatten.compact.join('.')
end

#inspectObject



193
194
195
# File 'lib/url.rb', line 193

def inspect
  "#<#{self.class} #{to_s}>"
end

#post(*args) ⇒ URL::Response

Performs a post request for the current URL

Returns:

  • (URL::Response)

    A subclass of string which also repsonds to a few added mthods storing more information



183
184
185
# File 'lib/url.rb', line 183

def post(*args)
  req_handler.post(*args)
end

#req_handlerHandler

The request handler for this

Returns:

  • (Handler)


203
204
205
# File 'lib/url.rb', line 203

def req_handler
  (@req_handler||self.class.req_handler).new(self)
end

#req_handler=(r) ⇒ RequstHandler

Sets the handler to use for this request

Parameters:

  • (RequstHandler)

Returns:

  • (RequstHandler)

Raises:

  • (ArgumentError)


214
215
216
217
# File 'lib/url.rb', line 214

def req_handler=r
  raise ArgumentError, 'Must be a subclass of URL::Handler' unless r < RequestHandler
  @req_handler = r
end

#to_s(ops = {}) ⇒ String

Outputs the full current url

Parameters:

  • ops (Hash) (defaults to: {})

    Prevent certain parts of the object from being shown by setting :scheme,:port,:path,:params, or :hash to false

Returns:

  • (String)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/url.rb', line 114

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_uriURI

Returns the parsed URI object for the string

Returns:

  • (URI)


132
133
134
# File 'lib/url.rb', line 132

def to_uri
  URI.parse(to_s)
end