Class: Springboard::Client::URI

Inherits:
Object
  • Object
show all
Defined in:
lib/springboard/client/uri.rb

Overview

A wrapper around URI

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ URI

Creates a new URI object from an Addressable::URI


21
22
23
# File 'lib/springboard/client/uri.rb', line 21

def initialize(uri)
  @uri = uri
end

Class Method Details

.parse(value) ⇒ URI

Returns a URI object based on the parsed string.


12
13
14
15
# File 'lib/springboard/client/uri.rb', line 12

def self.parse(value)
  return value.dup if value.is_a?(self)
  new(::URI.parse(value.to_s))
end

Instance Method Details

#==(other_uri) ⇒ boolean

Checks if supplied URI matches current URI


57
58
59
60
# File 'lib/springboard/client/uri.rb', line 57

def ==(other_uri)
  return false unless other_uri.is_a?(self.class)
  uri == other_uri.__send__(:uri)
end

#dupURI

Clones the URI object


29
30
31
# File 'lib/springboard/client/uri.rb', line 29

def dup
  self.class.new(@uri.dup)
end

#merge_query_values!(values) ⇒ Object

Merges the given hash of query string parameters and values with the URI's existing query string parameters (if any).


48
49
50
51
# File 'lib/springboard/client/uri.rb', line 48

def merge_query_values!(values)
  old_query_values = self.query_values || {}
  self.query_values = old_query_values.merge(normalize_query_hash(values))
end

#query_valueshash

Returns a hash of query string parameters and values


72
73
74
75
76
77
78
79
80
81
82
# File 'lib/springboard/client/uri.rb', line 72

def query_values
  return nil if query.nil?
  ::URI.decode_www_form(query).each_with_object({}) do |(k, v), hash|
    if k.end_with?('[]')
      k.gsub!(/\[\]$/, '')
      hash[k] = Array(hash[k]) + [v]
    else
      hash[k] = v
    end
  end
end

#query_values=(values) ⇒ Object

Overwrites the query using the supplied query values


64
65
66
# File 'lib/springboard/client/uri.rb', line 64

def query_values=(values)
  self.query = ::URI.encode_www_form(normalize_query_hash(values).sort)
end

#subpath(subpath) ⇒ URI

Returns a new URI with the given subpath appended to it. Ensures a single forward slash between the URI's path and the given subpath.


38
39
40
41
42
43
# File 'lib/springboard/client/uri.rb', line 38

def subpath(subpath)
  uri = dup
  uri.path = "#{path}/" unless path.end_with?('/')
  uri.path = uri.path + ::URI.encode(subpath.to_s.gsub(/^\//, ''))
  uri
end