Class: URL
- Inherits:
-
Object
- Object
- URL
- Extended by:
- Forwardable
- Defined in:
- lib/url.rb,
lib/url/version.rb,
lib/url/endpoint.rb,
lib/url/handlers.rb,
lib/url/response.rb,
lib/url/helper_classes.rb,
lib/url/accepts_endpoint.rb,
lib/url/endpoint_builder.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, Service, TyHandler, YajlHandler
Constant Summary collapse
- VERSION =
"0.3.2"
Instance Attribute Summary collapse
-
#domain ⇒ Object
Attributes of the URL which are editable.
-
#format ⇒ Object
Attributes of the URL which are editable.
-
#hash ⇒ Object
Attributes of the URL which are editable.
-
#params ⇒ Object
The params for the request.
-
#path ⇒ Object
The path for the request.
-
#port ⇒ Object
Attributes of the URL which are editable.
-
#scheme ⇒ Object
Attributes of the URL which are editable.
-
#string ⇒ Object
readonly
Returns the value of attribute string.
-
#subdomain ⇒ Object
(also: #subdomains)
Returns array of subdomains.
Class Method Summary collapse
- .json_handler ⇒ Object
- .json_handler=(r) ⇒ Object
-
.req_handler ⇒ RequstHandler
Define the request handler to use.
-
.req_handler=(r) ⇒ RequstHandler
Define the request handler to use.
- .Service(url) ⇒ Object
Instance Method Summary collapse
- #=~(reg) ⇒ Object
- #add_to_path(val) ⇒ Object
-
#delete(*args) ⇒ URL::Response
Performs a delete request for the current URL.
- #dup ⇒ Object
-
#get(*args) ⇒ URL::Response
Performs a get request for the current URL.
-
#host ⇒ Object
The full hostname (not including port) for the URL.
-
#host_with_port ⇒ Object
Messed up host/hostname issue :(.
-
#initialize(str) ⇒ URL
constructor
Creates a new URL object.
- #inspect ⇒ Object
-
#post(*args) ⇒ URL::Response
Performs a post request for the current URL.
-
#put(*args) ⇒ URL::Response
Performs a put request for the current URL.
-
#req_handler ⇒ Handler
The request handler for this.
-
#req_handler=(r) ⇒ RequstHandler
Sets the handler to use for this request.
-
#to_s(ops = {}) ⇒ String
Outputs the full current url.
-
#to_uri ⇒ URI
Returns the parsed URI object for the string.
Constructor Details
#initialize(str) ⇒ URL
Creates a new URL object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# 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 |
Instance Attribute Details
#domain ⇒ Object
Attributes of the URL which are editable
47 48 49 |
# File 'lib/url.rb', line 47 def domain @domain end |
#format ⇒ Object
Attributes of the URL which are editable
47 48 49 |
# File 'lib/url.rb', line 47 def format @format end |
#hash ⇒ Object
Attributes of the URL which are editable
47 48 49 |
# File 'lib/url.rb', line 47 def hash @hash end |
#params ⇒ Object
The params for the request
35 36 37 |
# File 'lib/url.rb', line 35 def params @params end |
#path ⇒ Object
The path for the request
51 52 53 |
# File 'lib/url.rb', line 51 def path @path end |
#port ⇒ Object
Attributes of the URL which are editable
47 48 49 |
# File 'lib/url.rb', line 47 def port @port end |
#scheme ⇒ Object
Attributes of the URL which are editable
47 48 49 |
# File 'lib/url.rb', line 47 def scheme @scheme end |
#string ⇒ Object (readonly)
Returns the value of attribute string.
31 32 33 |
# File 'lib/url.rb', line 31 def string @string end |
#subdomain ⇒ Object Also known as: subdomains
Returns array of subdomains
73 74 75 |
# File 'lib/url.rb', line 73 def subdomain @subdomain end |
Class Method Details
.json_handler ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 |
# 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) ⇒ Object
196 197 198 199 |
# 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 |
.req_handler ⇒ RequstHandler
Define the request handler to use. If Typhoeus is setup it will use TyHandler otherwise will default back to Net::HTTP with NetHandler
166 167 168 169 170 171 172 173 174 |
# 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) ⇒ RequstHandler
Define the request handler to use. If Typhoeus is setup it will use TyHandler otherwise will default back to Net::HTTP with NetHandler
179 180 181 182 |
# 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 |
Instance Method Details
#=~(reg) ⇒ Object
240 241 242 |
# File 'lib/url.rb', line 240 def =~ reg to_s =~ reg end |
#add_to_path(val) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/url.rb', line 62 def add_to_path val unless @path[-1] == 47 # '/' @path << '/' end @path << val.sub(/^\//,'') @path end |
#delete(*args) ⇒ URL::Response
Performs a delete request for the current URL
216 217 218 |
# File 'lib/url.rb', line 216 def delete(*args) req_handler.delete(*args) end |
#get(*args) ⇒ URL::Response
Performs a get request for the current URL
204 205 206 |
# File 'lib/url.rb', line 204 def get(*args) req_handler.get(*args) end |
#host ⇒ Object
The full hostname (not including port) for the URL
129 130 131 |
# File 'lib/url.rb', line 129 def host [@subdomain,@domain].flatten.compact.join('.') end |
#host_with_port ⇒ Object
Messed up host/hostname issue :(
134 135 136 |
# File 'lib/url.rb', line 134 def host_with_port host<<':'<<port.to_s end |
#inspect ⇒ Object
226 227 228 |
# File 'lib/url.rb', line 226 def inspect "#<#{self.class} #{to_s}>" end |
#post(*args) ⇒ URL::Response
Performs a post request for the current URL
210 211 212 |
# File 'lib/url.rb', line 210 def post(*args) req_handler.post(*args) end |
#put(*args) ⇒ URL::Response
Performs a put request for the current URL
222 223 224 |
# File 'lib/url.rb', line 222 def put(*args) req_handler.delete(*args) end |
#req_handler ⇒ Handler
The request handler for this
236 237 238 |
# File 'lib/url.rb', line 236 def req_handler (@req_handler||self.class.req_handler).new(self) end |
#req_handler=(r) ⇒ RequstHandler
Sets the handler to use for this request
247 248 249 250 |
# 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 |
#to_s(ops = {}) ⇒ String
Outputs the full current url
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# 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 ⇒ URI
Returns the parsed URI object for the string
159 160 161 |
# File 'lib/url.rb', line 159 def to_uri URI.parse(to_s) end |