Class: Glimmer::Util::UrlBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/glimmer/util/url_builder.rb

Constant Summary collapse

DEFAULT_SCHEME =
'https'
REGEXP_URL =
/^(https?)?(:\/\/)?(([^\/\:]+):?(\d+)?)?(\/[^?#]*)?(\?[^#]*)?(\#.+)?$/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUrlBuilder

Returns a new instance of UrlBuilder.



18
19
20
21
# File 'lib/glimmer/util/url_builder.rb', line 18

def initialize
  @scheme = DEFAULT_SCHEME
  @params = {}
end

Class Method Details

.currentObject



13
14
15
# File 'lib/glimmer/util/url_builder.rb', line 13

def current
  new.url($$.document.location.href) if RUBY_ENGINE == 'opal'
end

Instance Method Details

#compute_queryObject



96
97
98
# File 'lib/glimmer/util/url_builder.rb', line 96

def compute_query
  UrlQueryStringBuilder.new.with_prefix.params(@params).to_s
end

#fragment(value) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/glimmer/util/url_builder.rb', line 74

def fragment(value)
  return self if value.nil?
  
  @fragment = value
  @fragment = "##{@fragment}" unless @fragment.start_with?('#')
  self
end

#host(value) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/glimmer/util/url_builder.rb', line 30

def host(value)
  return self if value.nil?
  
  @host = value
  @host.gsub('/', '')
  self
end

#param(name, value) ⇒ Object



53
54
55
56
# File 'lib/glimmer/util/url_builder.rb', line 53

def param(name, value)
  @params[name.to_s] = value
  self
end

#params(params_hash) ⇒ Object



58
59
60
61
# File 'lib/glimmer/util/url_builder.rb', line 58

def params(params_hash)
  @params = @params.merge(params_hash)
  self
end

#path(value) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/glimmer/util/url_builder.rb', line 45

def path(value)
  return self if value.nil?
  
  @path = value
  @path = "/#{@path}" unless @path.start_with?('/')
  self
end

#port(value) ⇒ Object



38
39
40
41
42
43
# File 'lib/glimmer/util/url_builder.rb', line 38

def port(value)
  return self if value.nil?
  
  @port = value
  self
end

#query(value) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/glimmer/util/url_builder.rb', line 63

def query(value)
  return self if value.nil?
  
  value = value.sub('?', '') if value.start_with?('?')
  value.split('&').each do |param_pair|
    name, value = param_pair.split('=')
    @params[name.to_s] = value
  end
  self
end

#scheme(value) ⇒ Object Also known as: protocol



23
24
25
26
27
# File 'lib/glimmer/util/url_builder.rb', line 23

def scheme(value)
  @scheme = value || DEFAULT_SCHEME
  @scheme = @scheme.gsub(/[:\/]/, '')
  self
end

#to_urlObject Also known as: to_s, build



87
88
89
90
91
92
# File 'lib/glimmer/util/url_builder.rb', line 87

def to_url
  output = "#{@scheme}://#{@host}"
  output += ":#{@port}" if @port
  output += "#{@path&.strip}#{compute_query}#{@fragment&.strip}"
  output
end

#url(value) ⇒ Object



82
83
84
85
# File 'lib/glimmer/util/url_builder.rb', line 82

def url(value)
  url_scheme, url_slashes, url_host_port, url_host, url_port, url_path, url_query, url_fragment = REGEXP_URL.match(value).to_a.drop(1)
  scheme(url_scheme).host(url_host).port(url_port).path(url_path).query(url_query).fragment(url_fragment)
end