Module: Stapler::Utils

Defined in:
lib/stapler/utils.rb

Defined Under Namespace

Classes: BadString

Class Method Summary collapse

Class Method Details

.b64_decode(string) ⇒ Object



27
28
29
30
# File 'lib/stapler/utils.rb', line 27

def b64_decode(string)
  padding_length = string.length % 4
  Base64.decode64(string + '=' * padding_length)
end

.b64_encode(string) ⇒ Object

Base64 encoding for URLS ####



23
24
25
# File 'lib/stapler/utils.rb', line 23

def b64_encode(string)
  Base64.encode64(string).tr("\n=",'')
end

.bundle_path(assets) ⇒ Object

Convert a list of assets into an URL Note: Rails helpers adds the appropriate extension



49
50
51
# File 'lib/stapler/utils.rb', line 49

def bundle_path(assets)
  File.join('/stapler/bundle/', Bundle::Key.to_key(assets))
end

.groom_path(path) ⇒ Object



42
43
44
45
# File 'lib/stapler/utils.rb', line 42

def groom_path(path)
  # Remove leading slashes and query params
  path.gsub(/^\//, '').gsub(/\?.*$/, '')
end

.response_body(response) ⇒ Object



17
18
19
20
# File 'lib/stapler/utils.rb', line 17

def response_body(response)
  body = response[2]
  body.is_a?(Rack::File) ? File.read(body.path) : body
end

.url_decode(string) ⇒ Object



36
37
38
39
40
# File 'lib/stapler/utils.rb', line 36

def url_decode(string)
  Marshal.load(b64_decode(CGI.unescape(string)))
rescue TypeError, ArgumentError => e
  raise BadString, "couldn't decode #{string} - got #{e}"
end

.url_encode(object) ⇒ Object



32
33
34
# File 'lib/stapler/utils.rb', line 32

def url_encode(object)
  CGI.escape(b64_encode(Marshal.dump(object)))
end