Class: URL

Inherits:
Struct
  • Object
show all
Defined in:
lib/activesp/url.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fragmentObject

Returns the value of attribute fragment

Returns:

  • (Object)

    the current value of fragment



44
45
46
# File 'lib/activesp/url.rb', line 44

def fragment
  @fragment
end

#hostObject

Returns the value of attribute host

Returns:

  • (Object)

    the current value of host



44
45
46
# File 'lib/activesp/url.rb', line 44

def host
  @host
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



44
45
46
# File 'lib/activesp/url.rb', line 44

def path
  @path
end

#portObject

Returns the value of attribute port

Returns:

  • (Object)

    the current value of port



44
45
46
# File 'lib/activesp/url.rb', line 44

def port
  @port
end

#protocolObject

Returns the value of attribute protocol

Returns:

  • (Object)

    the current value of protocol



44
45
46
# File 'lib/activesp/url.rb', line 44

def protocol
  @protocol
end

#queryObject

Returns the value of attribute query

Returns:

  • (Object)

    the current value of query



44
45
46
# File 'lib/activesp/url.rb', line 44

def query
  @query
end

Class Method Details

.construct_query(hash) ⇒ Object



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

def self.construct_query(hash)
  hash.map { |k, v| "%s=%s" % [k, escape(v)] }.join('&')
end

.escape(s) ⇒ Object



109
110
111
112
113
# File 'lib/activesp/url.rb', line 109

def self.escape(s)
  s.to_s.gsub(/([^a-zA-Z0-9_.-]+)/n) do
    '%' + $1.unpack('H2' * $1.size).join('%').upcase
  end
end

.parse(url) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/activesp/url.rb', line 46

def self.parse(url)
  if /^(?:([^:\/?#]+):)?(?:\/\/([^\/?#:]*)(?::(\d+))?)?([^?#]*)(?:\?([^#]*))?(?:#(.*))?$/ === url.strip
    new($1 ? $1.downcase : nil, $2 ? $2.downcase : nil, $3 ? $3.to_i : nil, $4, $5, $6)
  else
    nil
  end
end

.parse_query(qs, d = '&;') ⇒ Object



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

def self.parse_query(qs, d = '&;')
  params = {}
  (qs || '').split(/[&;] */n).inject(params) do |h, p|
    k, v = unescape(p).split('=', 2)
    if cur = params[k]
      if Array === cur
        params[k] << v
      else
        params[k] = [cur, v]
      end
    else
      params[k] = v
    end
  end
  params
end

.unescape(s) ⇒ Object



103
104
105
106
107
# File 'lib/activesp/url.rb', line 103

def self.unescape(s)
  s.gsub(/((?:%[0-9a-fA-F]{2})+)/n) do
    [$1.delete('%')].pack('H*')
  end
end

Instance Method Details

#authorityObject



58
59
60
# File 'lib/activesp/url.rb', line 58

def authority
  "%s%s" % [host, (!port || port == (protocol == "http" ? 80 : 443)) ? "" : ":#{port}"]
end

#completeObject



96
97
98
99
100
101
# File 'lib/activesp/url.rb', line 96

def complete
  self.protocol ||= "http"
  self.port ||= self.protocol == "http" ? 80 : 443
  self.path = "/" if self.path.empty?
  self
end

#full_pathObject



62
63
64
65
66
67
# File 'lib/activesp/url.rb', line 62

def full_path
  result = path.dup
  result << "?" << query if query
  result << "#" << fragment if fragment
  result
end

#join(url) ⇒ Object



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
# File 'lib/activesp/url.rb', line 69

def join(url)
  url = URL(url)
  if url
    if url.protocol == protocol
      url.protocol = nil
    end
    unless url.protocol
      url.protocol = protocol
      unless url.host
        url.host = host
        url.port = port
        if url.path.empty?
          url.path = path
          unless url.query
            url.query = query
          end
        else
          url.path = join_url_paths(url.path, path)
        end
      end
    end
    url.complete
  else
    nil
  end
end

#to_sObject



54
55
56
# File 'lib/activesp/url.rb', line 54

def to_s
  "%s://%s%s" % [protocol, authority, full_path]
end