Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/magellan/extensions/string.rb

Instance Method Summary collapse

Instance Method Details

#remove_fragmentObject

Removes a fragment from a URL Example:

'/foo.html#fsajfksafd'.remove_fragment    # => /foo.html


26
27
28
# File 'lib/magellan/extensions/string.rb', line 26

def remove_fragment
  self.gsub(/#.*/,'')
end

#to_absolute_url(origin_url) ⇒ Object

Converts a relative url to a absolute url Example:

'/foo.html'.to_absolute_url('http://www.google.com/index.html?foo=b')    # => http://www.google.com/foo.html
'?foo=a'.to_absolute_url('http://www.google.com/index.html?foo=b')    # => http://www.google.com/index.html?foo=a


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/magellan/extensions/string.rb', line 9

def to_absolute_url(origin_url) # :nodoc:
  begin
    #BUG in URI.join?  URI.join('http://www.google.com/index.html?foo=b','?foo=a') # => http://www.google.com/?foo=a
    stripped = self.strip
    if stripped.starts_with?('?')
      origin_url.gsub(/\?.*/,'') + stripped
    else
      URI.join(origin_url,stripped).to_s
    end
  rescue
    self
  end
end