Class: Deas::Url

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path, options = nil) ⇒ Url

Returns a new instance of Url.



15
16
17
18
19
# File 'lib/deas/url.rb', line 15

def initialize(name, path, options = nil)
  options ||= {}
  @name, @path = name, path
  @escape_query_value_proc = options[:escape_query_value]
end

Instance Attribute Details

#escape_query_value_procObject (readonly)

Returns the value of attribute escape_query_value_proc.



13
14
15
# File 'lib/deas/url.rb', line 13

def escape_query_value_proc
  @escape_query_value_proc
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.http_query(hash, &escape_value_proc) ⇒ Object



5
6
7
8
9
10
# File 'lib/deas/url.rb', line 5

def self.http_query(hash, &escape_value_proc)
  escape_value_proc ||= proc{ |v| v.to_s }
  hash.map do |(key, value)|
    "#{key}=#{escape_value_proc.call(value)}"
  end.sort.join('&')
end

Instance Method Details

#path_for(params = {}) ⇒ Object

Raises:

  • (NonHashParamsError)


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/deas/url.rb', line 21

def path_for(params = {})
  raise NonHashParamsError if !params.kind_of?(::Hash)

  h = params.dup # don't alter the given params
  c = h.delete(:captures) || h.delete('captures') || []
  s = h.delete(:splat)    || h.delete('splat')    || nil
  a = h.delete(:'#')      || h.delete('#')        || nil

  # ignore captures when setting params
  # remove duplicate forward slashes
  set_anchor(set_extra(set_named(set_splat(@path, s), h).gsub(/\/\/+/, '/'), h), a)
end