Class: PathRewriter::UrlCodec

Inherits:
Object
  • Object
show all
Defined in:
lib/path_rewriter/url_codec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_pathObject



8
9
10
# File 'lib/path_rewriter/url_codec.rb', line 8

def base_path
  @base_path ||= ""
end

#root_pathObject



12
13
14
# File 'lib/path_rewriter/url_codec.rb', line 12

def root_path
  @root_path ||= ""
end

Instance Method Details

#base_path_without_slashObject



20
21
22
# File 'lib/path_rewriter/url_codec.rb', line 20

def base_path_without_slash
  self.base_path.sub(/^\//, "")
end

#decode(uri) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/path_rewriter/url_codec.rb', line 24

def decode(uri)
  return uri if self.base_path.blank?
  uri = Addressable::URI.parse(uri)
  
  segments = uri.path.sub(self.root_path, '').split("/")
  segments.delete("")
  segments.unshift(self.base_path_without_slash)
  segments.unshift(self.root_path_without_slash) unless root_path.blank?
  uri.path = "/" + segments.join("/")
  
  uri.to_s
end

#encode(uri) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/path_rewriter/url_codec.rb', line 37

def encode(uri)
  return uri if self.base_path.blank?
  return uri unless uri.start_with?(self.root_path)
  uri = Addressable::URI.parse(uri)

  uri.path.sub!(self.root_path, '')
  uri.path.sub!(self.base_path, '')
  
  segments = uri.path.split("/")
  segments.unshift(self.root_path_without_slash)
  segments.delete("")
  uri.path = "/" + segments.join("/")
  
  uri.to_s
end

#root_path_without_slashObject



16
17
18
# File 'lib/path_rewriter/url_codec.rb', line 16

def root_path_without_slash
  self.root_path.sub(/^\//, "")
end