Class: Koine::Url

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

Overview

Url builder

Constant Summary collapse

ArgumentError =

Exception for when the argument is invalid

Class.new(::ArgumentError)

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Url

Returns a new instance of Url.



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

def initialize(url)
  @url = validate(url).to_s.freeze
  freeze
end

Instance Method Details

#==(other) ⇒ Object



56
57
58
# File 'lib/koine/url.rb', line 56

def ==(other)
  other.class == self.class && other.to_s == to_s
end

#query_paramsObject



34
35
36
# File 'lib/koine/url.rb', line 34

def query_params
  parsed.query_values || {}
end

#to_s(unescape: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/koine/url.rb', line 42

def to_s(unescape: nil)
  unless unescape
    return @url
  end

  url = @url.to_s.dup

  unescape.to_s.each_char do |char|
    url = url.gsub(CGI.escape(char), char)
  end

  url
end

#uriObject



38
39
40
# File 'lib/koine/url.rb', line 38

def uri
  parsed.request_uri
end

#with_query_param(param_name, value) ⇒ Object



30
31
32
# File 'lib/koine/url.rb', line 30

def with_query_param(param_name, value)
  with_query_params(query_params.merge(param_name.to_s => value))
end

#with_query_params(params) ⇒ Object

:reek:UncommunicativeVariableName :reek:DuplicateMethodCall



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/koine/url.rb', line 18

def with_query_params(params)
  if params.empty?
    return self.class.new(to_s.split('?').first)
  end

  url = parsed.tap do |p|
    p.query_values = params
  end

  self.class.new(url.to_s)
end