Module: TestServer::WebHelper

Defined in:
lib/test_server/web_helper.rb

Instance Method Summary collapse

Instance Method Details

#base_urlObject



4
5
6
7
8
9
10
# File 'lib/test_server/web_helper.rb', line 4

def base_url
  mutex = Mutex.new

  mutex.synchronize do
    @base_url ||= "#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}"
  end
end

#configure_caching(params) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/test_server/web_helper.rb', line 39

def configure_caching(params)
  options = []

  if params.key? 'expires'
    options << :must_revalidate
    options << :no_cache
    options << { max_age: params[:expires] }
  else
    options << :must_revalidate              if params.key? 'must_revalidate'
    options << :no_cache                     if params.key? 'no_cache'
    options << { max_age: params[:max_age] } if params.key? 'max_age'
  end

  cache_control(*options)
end

#encode(&block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/test_server/web_helper.rb', line 55

def encode(&block)
  encoders = []
  encoders << Encoders::Base64.new
  encoders << Encoders::Base64Strict.new
  encoders << Encoders::Gzip.new

  encoders_to_run = params.keys.map(&:to_sym) & encoders.collect { |e| e.name }

  if encoders_to_run.blank?
    real_encoders = [Encoders::Null.new]
  else
    real_encoders = encoders_to_run.collect { |er| encoders.find { |e| e.name? er } }.compact
  end

  real_encoders.reduce(block.call) do |memo, e| 
    e.encode(memo)
  end
end

#generate_eicarObject



30
31
32
33
34
35
36
37
# File 'lib/test_server/web_helper.rb', line 30

def generate_eicar
  [ 'X', '5', 'O', '!', 'P', '%', '@', 'A', 'P', '[', '4', "\\", 'P',
    'Z', 'X', '5', '4', '(', 'P', '^', ')', '7', 'C', 'C', ')', '7',
    '}', '$', 'E', 'I', 'C', 'A', 'R', '-', 'S', 'T', 'A', 'N', 'D',
    'A', 'R', 'D', '-', 'A', 'N', 'T', 'I', 'V', 'I', 'R', 'U', 'S',
    '-', 'T', 'E', 'S', 'T', '-', 'F', 'I', 'L', 'E', '!', '$', 'H',
    '+', 'H', '*' ]
end

#generate_random_string(count) ⇒ Object



24
25
26
27
28
# File 'lib/test_server/web_helper.rb', line 24

def generate_random_string(count)
  o = [('a'..'z'), ('A'..'Z'), (1..9), %w{° ^ ! " § $ % & / ( ) = ? * + ~ ` ´ ' # > < | @ ł   ŧ    ŋ đ ð ſ ð đ ŋ ħ   ¢ « | » « ¢    µ ·   ĸ ¹ ² ³ ¼ ½ ¬ \{ [ ] \}}].map { |i| i.to_a }.flatten

  (0...count).map { o[rand(o.length)] }.join
end

#generate_string(count, string = "Plain Data\n") ⇒ Object



20
21
22
# File 'lib/test_server/web_helper.rb', line 20

def generate_string(count, string = "Plain Data\n")
  string * count
end

#h(text) ⇒ Object



12
13
14
# File 'lib/test_server/web_helper.rb', line 12

def h(text)
  Rack::Utils.escape_html(text)
end

#t(*args) ⇒ Object



16
17
18
# File 'lib/test_server/web_helper.rb', line 16

def t(*args)
  I18n.t(*args)
end