Module: Recaptcha::ClientHelper

Defined in:
lib/recaptcha/client_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.recaptcha_components(options = {}) ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/recaptcha/client_helper.rb', line 69

def self.recaptcha_components(options = {})
  html = +''
  attributes = {}
  fallback_uri = +''

  # Since leftover options get passed directly through as tag
  # attributes, we must unconditionally delete all our options
  options = options.dup
  env = options.delete(:env)
  class_attribute = options.delete(:class)
  site_key = options.delete(:site_key)
  hl = options.delete(:hl)
  onload = options.delete(:onload)
  render = options.delete(:render)
  nonce = options.delete(:nonce)
  skip_script = (options.delete(:script) == false)
  ui = options.delete(:ui)

  data_attribute_keys = [:badge, :theme, :type, :callback, :expired_callback, :error_callback, :size]
  data_attribute_keys << :tabindex unless ui == :button
  data_attributes = {}
  data_attribute_keys.each do |data_attribute|
    value = options.delete(data_attribute)
    data_attributes["data-#{data_attribute.to_s.tr('_', '-')}"] = value if value
  end

  unless Recaptcha::Verify.skip?(env)
    site_key ||= Recaptcha.configuration.site_key!
    script_url = Recaptcha.configuration.api_server_url
    query_params = hash_to_query(
      hl: hl,
      onload: onload,
      render: render
    )
    script_url += "?#{query_params}" unless query_params.empty?
    nonce_attr = " nonce='#{nonce}'" if nonce
    html << %(<script src="#{script_url}" async defer#{nonce_attr}></script>\n) unless skip_script
    fallback_uri = %(#{script_url.chomp(".js")}/fallback?k=#{site_key})
    attributes["data-sitekey"] = site_key
    attributes.merge! data_attributes
  end

  # Append whatever that's left of options to be attributes on the tag.
  attributes["class"] = "g-recaptcha #{class_attribute}"
  tag_attributes = attributes.merge(options).map { |k, v| %(#{k}="#{v}") }.join(" ")

  [html, tag_attributes, fallback_uri]
end

Instance Method Details

#invisible_recaptcha_tags(options = {}) ⇒ Object

Invisible reCAPTCHA implementation


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/recaptcha/client_helper.rb', line 51

def invisible_recaptcha_tags(options = {})
  options = {callback: 'invisibleRecaptchaSubmit', ui: :button}.merge options
  text = options.delete(:text)
  html, tag_attributes = Recaptcha::ClientHelper.recaptcha_components(options)
  html << recaptcha_default_callback(options) if recaptcha_default_callback_required?(options)
  case options[:ui]
  when :button
    html << %(<button type="submit" #{tag_attributes}>#{text}</button>\n)
  when :invisible
    html << %(<div data-size="invisible" #{tag_attributes}></div>\n)
  when :input
    html << %(<input type="submit" #{tag_attributes} value="#{text}"/>\n)
  else
    raise(RecaptchaError, "ReCAPTCHA ui `#{options[:ui]}` is not valid.")
  end
  html.respond_to?(:html_safe) ? html.html_safe : html
end

#recaptcha_tags(options = {}) ⇒ Object

Your public API can be specified in the options hash or preferably using the Configuration.


7
8
9
10
11
12
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
# File 'lib/recaptcha/client_helper.rb', line 7

def recaptcha_tags(options = {})
  if options.key?(:stoken)
    raise(RecaptchaError, "Secure Token is deprecated. Please remove 'stoken' from your calls to recaptcha_tags.")
  end
  if options.key?(:ssl)
    raise(RecaptchaError, "SSL is now always true. Please remove 'ssl' from your calls to recaptcha_tags.")
  end

  noscript = options.delete(:noscript)

  html, tag_attributes, fallback_uri = Recaptcha::ClientHelper.recaptcha_components(options)
  html << %(<div #{tag_attributes}></div>\n)

  if noscript != false
    html << "<noscript>\n<div>\n<div style=\"width: 302px; height: 422px; position: relative;\">\n<div style=\"width: 302px; height: 422px; position: absolute;\">\n<iframe\nsrc=\"\#{fallback_uri}\"\nname=\"ReCAPTCHA\"\nstyle=\"width: 302px; height: 422px; border-style: none; border: 0; overflow: hidden;\">\n</iframe>\n</div>\n</div>\n<div style=\"width: 300px; height: 60px; border-style: none;\nbottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px;\nbackground: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px;\">\n<textarea id=\"g-recaptcha-response\" name=\"g-recaptcha-response\"\nclass=\"g-recaptcha-response\"\nstyle=\"width: 250px; height: 40px; border: 1px solid #c1c1c1;\nmargin: 10px 25px; padding: 0px; resize: none;\">\n</textarea>\n</div>\n</div>\n</noscript>\n"
  end

  html.respond_to?(:html_safe) ? html.html_safe : html
end