Class: URLSignature::URL

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

Constant Summary collapse

SEQUENCIAL_PARAMS =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ URL

Returns a new instance of URL.



11
12
13
14
15
16
17
18
19
20
# File 'lib/url_signature/url.rb', line 11

def initialize(url)
  @uri = parse_url(url)
  @scheme = uri.scheme
  @host = uri.host
  @user = uri.user
  @password = uri.password
  @path = uri.path.empty? ? "/" : uri.path
  @params = parse_query(uri.query)
  @fragment = uri.fragment
end

Instance Attribute Details

#fragmentObject (readonly)

Returns the value of attribute fragment.



5
6
7
# File 'lib/url_signature/url.rb', line 5

def fragment
  @fragment
end

#hostObject (readonly)

Returns the value of attribute host.



5
6
7
# File 'lib/url_signature/url.rb', line 5

def host
  @host
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/url_signature/url.rb', line 5

def params
  @params
end

#passwordObject (readonly)

Returns the value of attribute password.



5
6
7
# File 'lib/url_signature/url.rb', line 5

def password
  @password
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/url_signature/url.rb', line 5

def path
  @path
end

#schemeObject (readonly)

Returns the value of attribute scheme.



5
6
7
# File 'lib/url_signature/url.rb', line 5

def scheme
  @scheme
end

#userObject (readonly)

Returns the value of attribute user.



5
6
7
# File 'lib/url_signature/url.rb', line 5

def user
  @user
end

Instance Method Details

#add_query(key, value, replace: true) ⇒ Object



29
30
31
32
33
# File 'lib/url_signature/url.rb', line 29

def add_query(key, value, replace: true)
  params[key] ||= []
  params[key] = [] if replace
  params[key] += [value].flatten.map(&:to_s)
end

#clear_query!Object



55
56
57
# File 'lib/url_signature/url.rb', line 55

def clear_query!
  @params = {}
end

#portObject



22
23
24
25
26
27
# File 'lib/url_signature/url.rb', line 22

def port
  return if uri.port == 80 && @scheme == "http"
  return if uri.port == 443 && @scheme == "https"

  uri.port
end

#queryObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/url_signature/url.rb', line 39

def query
  return if params.empty?

  query = params.each_with_object([]) do |(param, value), buffer|
    param = param.to_s

    if param.include?("[")
      value.each {|v| buffer << "#{encode(param)}=#{encode(v)}" }
    else
      buffer << "#{encode(param)}=#{encode(value.last)}"
    end
  end

  query.sort.join("&")
end

#remove_query(key) ⇒ Object



35
36
37
# File 'lib/url_signature/url.rb', line 35

def remove_query(key)
  params.delete(key) || []
end

#to_sObject



59
60
61
62
63
64
65
66
67
# File 'lib/url_signature/url.rb', line 59

def to_s
  [
    "#{scheme}://#{host}",
    port ? ":#{port}" : nil,
    path,
    query ? "?#{query}" : nil,
    fragment ? "##{fragment}" : nil
  ].compact.join
end