Class: Url

Inherits:
Object show all
Defined in:
lib/common/url.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Url

Returns a new instance of Url.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/common/url.rb', line 56

def initialize url
  url, qs_part = url.split('?', 2)

  @qs = qs_part.to_s.split('&').inject({}) do |qs, el|
    parts = el.split('=', 2)
    qs[parts[0]] = Url.unescape parts[1]
    qs
  end

  if url =~ %r{://}
    @proto, _, @domain, @path = url.split '/', 4

    @proto = @proto.sub(':', '')

    @domain, @port = @domain.split(':', 2)
    @port = nil if @port == '80' || @port.blank?

    @domain_parts = @domain.split('.').reverse.map(&:downcase)
  else
    @path = url.to_s.sub(%r{^/}, '')
  end

  @path = '' if @path.blank?
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



12
13
14
# File 'lib/common/url.rb', line 12

def host
  @host
end

#portObject

Returns the value of attribute port.



12
13
14
# File 'lib/common/url.rb', line 12

def port
  @port
end

#protoObject

Returns the value of attribute proto.



12
13
14
# File 'lib/common/url.rb', line 12

def proto
  @proto
end

Class Method Details

.currentObject



15
16
17
# File 'lib/common/url.rb', line 15

def current
  new Lux.current.request.url
end

.escape(str = nil) ⇒ Object



45
46
47
# File 'lib/common/url.rb', line 45

def escape str=nil
  CGI::escape(str.to_s)
end

.force_locale(loc) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/common/url.rb', line 19

def force_locale loc
  # u = current
  # u.locale loc
  # u.relative

  '/' + loc.to_s + Lux.current.nav.full
end

.prepare_qs(name) ⇒ Object

for search Url.prepare_qs(:q) -> /foo?bar=1&q=



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

def prepare_qs name
  url = current.delete(name).relative
  url += url.index('?') ? '&' : '?'
  "#{url}#{name}="
end

.qs(name, value) ⇒ Object



33
34
35
# File 'lib/common/url.rb', line 33

def qs name, value
  current.qs(name, value).relative
end

.subdomain(name, in_path = nil) ⇒ Object



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

def subdomain name,  in_path=nil
  b = current.subdomain(name)
  b.path in_path if in_path
  b.url
end

.unescape(str = nil) ⇒ Object



49
50
51
# File 'lib/common/url.rb', line 49

def unescape str=nil
  CGI::unescape(str.to_s)
end

Instance Method Details

#[](key) ⇒ Object



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

def [] key
  @qs[key.to_s]
end

#[]=(key, value) ⇒ Object



173
174
175
# File 'lib/common/url.rb', line 173

def []= key, value
  @qs[key.to_s] = value
end

#delete(*keys) ⇒ Object



120
121
122
123
# File 'lib/common/url.rb', line 120

def delete *keys
  keys.map{ |key| @qs.delete(key.to_s) }
  self
end

#domain(what = nil) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/common/url.rb', line 81

def domain what=nil
  if what
    @host = what
    return self
  end

  @domain_parts.slice(0,2).reverse.join '.'
end

#hash(val) ⇒ Object



125
126
127
# File 'lib/common/url.rb', line 125

def hash val
  @hash = "##{val}"
end

#locale(what) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/common/url.rb', line 143

def locale what
  elms = @path.split('/')

  if elms[0] && Locale.all.index(elms[0].to_s)
    elms[0] = what
  else
    elms.unshift what
  end

  @path = elms.join('/')

  self
end

#namespace(data) ⇒ Object



138
139
140
141
# File 'lib/common/url.rb', line 138

def namespace data
  @namespace = data.to_s
  self
end

#path(val = nil) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/common/url.rb', line 111

def path val=nil
  if val
    @path = val.sub /^\//, ''
    return self
  else
    '/' + @path
  end
end

#qs(name, value = :_nil) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/common/url.rb', line 129

def qs name, value=:_nil
  if value != :_nil
    @qs[name.to_s] = value
    self
  elsif name
    @qs[name.to_s]
  end
end

#queryObject



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

def query
  @query
end

#relativeObject



161
162
163
# File 'lib/common/url.rb', line 161

def relative
  [path, qs_val, @hash].join('').sub('//','/')
end

#subdomain(name = nil) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/common/url.rb', line 90

def subdomain name=nil
  if name
    @domain_parts[2] = name
    return self
  end

  @domain_parts.drop(2).reverse.join('.').or nil
end

#subdomain=(name) ⇒ Object



99
100
101
# File 'lib/common/url.rb', line 99

def subdomain= name
  @domain_parts[2] = name
end

#to_sObject



165
166
167
# File 'lib/common/url.rb', line 165

def to_s
  @domain ? url : relative
end

#urlObject



157
158
159
# File 'lib/common/url.rb', line 157

def url
  [host_with_port, path, qs_val, @hash].join('')
end