Sha256: 1d1d9579098112eb93ddb219eb8b21bd9c8ec4210429494dfb29ac4b0f0e5e70

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

# simple wrapper for the textcaptcha.com API service
# loads and parses captcha question and answers

require 'rexml/document'

module ActsAsTextcaptcha

  # raised if an empty response is returned
  class EmptyResponseError < StandardError; end;

  class TextcaptchaApi

    ENDPOINT = 'http://textcaptcha.com/api/'

    def self.fetch(api_key, options = {})
      begin
        url = uri_parser.parse("#{ENDPOINT}#{api_key}")
        http = Net::HTTP.new(url.host, url.port)
        if options[:http_open_timeout]
          http.open_timeout = options[:http_open_timeout]
        end
        if options[:http_read_timeout]
          http.read_timeout = options[:http_read_timeout]
        end

        response = http.get(url.path)
        if response.body.to_s.empty?
          raise ActsAsTextcaptcha::EmptyResponseError
        else
          return parse(response.body)
        end
      rescue SocketError, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
        Errno::EHOSTUNREACH, EOFError, Errno::ECONNREFUSED, Errno::ETIMEDOUT,
        Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError,
        URI::InvalidURIError, ActsAsTextcaptcha::EmptyResponseError,
        REXML::ParseException
        # rescue from these errors and continue
      end
    end

    def self.parse(xml)
      parsed_xml = ActiveSupport::XmlMini.parse(xml)['captcha']
      question = parsed_xml['question']['__content__']
      if parsed_xml['answer'].is_a?(Array)
        answers = parsed_xml['answer'].collect { |a| a['__content__'] }
      else
        answers = [parsed_xml['answer']['__content__']]
      end

      [question, answers]
    end


    private

    def self.uri_parser
      # URI.parse is deprecated in 1.9.2
      URI.const_defined?(:Parser) ? URI::Parser.new : URI
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
acts_as_textcaptcha-4.3.0 lib/acts_as_textcaptcha/textcaptcha_api.rb
acts_as_textcaptcha-4.2.0 lib/acts_as_textcaptcha/textcaptcha_api.rb
acts_as_textcaptcha-4.1.3 lib/acts_as_textcaptcha/textcaptcha_api.rb