Module: Merb::Threshold::Helpers

Defined in:
lib/merb_threshold/helpers/wait_helper.rb,
lib/merb_threshold/helpers/recaptcha_helper.rb

Instance Method Summary collapse

Instance Method Details

#captcha(threshold_name = nil, opts = {}) ⇒ String

Display a captcha if the threshold has been exceeded

Parameters:

  • threshold_name (~Symbol) (defaults to: nil)

    key to look up

Returns:

  • (String)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/merb_threshold/helpers/recaptcha_helper.rb', line 19

def captcha(threshold_name = nil, opts={})
  if threshold_name.is_a?(Hash)
    opts = threshold_name
    threshold_name = action_name
  end

  curr_threshold_key = threshold_key(threshold_name)
  
  # Has the thresholded resource been accessed during this request
  # if so 
  #   if it was relaxed
  #     dont show partial
  #   else
  #     show partial
  # else #resource wasn't access
  #   if permit_another?
  #     dont show partial
  #   else 
  #     show partial
  #       
  @show_captcha = if @relaxed_thresholds && @relaxed_thresholds.key?(curr_threshold_key)
    if @relaxed_thresholds[curr_threshold_key]
      false #dont show partial, it was relaxed
    else 
      true #show partial, threshold exceeded
    end
  else
    !will_permit_another?(threshold_name)
  end

  # if it won't permit another, show the captcha
  if @show_captcha
    _src_uri        = opts.delete(:ssl) ? RecaptchaClient::API_SSL_SERVER : RecaptchaClient::API_SERVER
    
    _encoded_key    = escape_html(RecaptchaClient.public_key)
  
    _recaptcha_partial = (opts.delete(:partial) || Merb::Plugins.config[:merb_threshold][:captcha_partial])
    _partial_opts   = opts.delete(:partial_opts) || {}
  
    _partial_opts.merge!({
      :src_uri            => _src_uri, 
      :encoded_key        => _encoded_key, 
      :threshold_options  => opts,
      :captcha_error      => escape_html(@captcha_error.to_s)
    })
  
    partial(_recaptcha_partial, _partial_opts)
  end
end

#wait(threshold_name = nil, opts = {}) ⇒ String

Display a wait message if the threshold has been exceeded

Parameters:

  • threshold_name (~Symbol) (defaults to: nil)

    key to look up

Returns:

  • (String)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/merb_threshold/helpers/wait_helper.rb', line 13

def wait(threshold_name = nil,opts={})
  if threshold_name.is_a?(Hash)
    opts = threshold_name
    threshold_name = action_name
  end

  curr_threshold_key = threshold_key(threshold_name)
  
  # Has the thresholded resource been accessed during this request
  # if so 
  #   if it was relaxed
  #     dont show partial
  #   else
  #     show partial
  # else #resource wasn't access
  #   if permit_another?
  #     dont show partial
  #   else 
  #     show partial
  #       
  @show_wait = if @relaxed_thresholds && @relaxed_thresholds.key?(curr_threshold_key)
    if @relaxed_thresholds[curr_threshold_key]
      false #dont show partial, it was relaxed
    else 
      true #show partial, threshold exceeded
    end
  else #wasn't accessed, will it permit another?
    !will_permit_another?(threshold_name)
  end

  # if it wont permit another show wait
  if @show_wait
    _wait_partial = opts.delete(:partial) || Merb::Plugins.config[:merb_threshold][:wait_partial]
    _partial_opts = opts.delete(:partial_opts) || {}
    _partial_opts.merge!({
      :seconds_to_wait => (waiting_period[threshold_key(threshold_name)] || 0)
    })
    partial(_wait_partial,_partial_opts)
  end
end