Class: Locomotive::Steam::RecaptchaService

Inherits:
Object
  • Object
show all
Defined in:
lib/locomotive/steam/services/recaptcha_service.rb

Overview

This service supports Google Recaptcha or any API compatible with Google

Constant Summary collapse

GOOGLE_API_URL =
'https://www.google.com/recaptcha/api/siteverify'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(site, request) ⇒ RecaptchaService

Returns a new instance of RecaptchaService.



11
12
13
14
15
16
17
# File 'lib/locomotive/steam/services/recaptcha_service.rb', line 11

def initialize(site, request)
  attributes = site.metafields.values.reduce({}, :merge).with_indifferent_access

  @api      = attributes[:recaptcha_api_url] || GOOGLE_API_URL
  @secret   = attributes[:recaptcha_secret]
  @ip       = request.ip
end

Instance Method Details

#verify(response_code) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/locomotive/steam/services/recaptcha_service.rb', line 19

def verify(response_code)
  # save a HTTP query if there is no code
  return false if response_code.blank?

  _response = HTTParty.get(@api, { query: {
    secret:   @secret,
    response: response_code,
    remoteip: @ip
  }})

  _response.parsed_response['success']
end