Sha256: a5de605b8ea47ab2c7f960277b0eb7abd4e515d9a12ccdbc3f4500e8c8fd0295

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require 'json'

module ActsAsTextcaptcha
  class TextcaptchaApi

    BASE_URL = 'http://textcaptcha.com'

    def initialize(api_key: nil, api_endpoint: nil, raise_errors: false)
      if api_endpoint
        self.uri = URI(api_endpoint)
      else
        self.uri = URI("#{BASE_URL}/#{api_key}.json")
      end
      self.raise_errors = raise_errors || false
    rescue URI::InvalidURIError => exception
      raise ApiKeyError.new(api_key, exception)
    end

    def fetch
      parse(get.to_s)
    end

    private

      attr_accessor :uri, :raise_errors

      def get
        response = Net::HTTP.new(uri.host, uri.port).get(uri.path)
        if response.code == '200'
          response.body
        else
          handle_error ResponseError.new(uri, "status: #{response.code}")
        end
      rescue SocketError, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
        Errno::EHOSTUNREACH, EOFError, Errno::ECONNREFUSED, Errno::ETIMEDOUT,
        Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
        Net::ProtocolError => exception
        handle_error ResponseError.new(uri, exception)
      end

      def parse(response)
        JSON.parse(response) unless response.empty?
      rescue JSON::ParserError
        handle_error ParseError.new(uri)
      end

      def handle_error(error)
        if raise_errors
          raise error
        else
          Rails.logger.error("#{error.class} #{error.message}")
          nil
        end
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acts_as_textcaptcha-4.4.1 lib/acts_as_textcaptcha/textcaptcha_api.rb