matthutchinson/acts_as_textcaptcha

lib/acts_as_textcaptcha/textcaptcha_api.rb
A

Do not suppress exceptions.
Open

      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

Useless private access modifier.
Open

    private

This cop checks for redundant access modifiers, including those with no code, those which are repeated, and leading public modifiers in a class or module body. Conditionally-defined methods are considered as always being defined, and thus access modifiers guarding such methods are not redundant.

Example:

class Foo
  public # this is redundant (default access is public)

  def method
  end

  private # this is not redundant (a method is defined)
  def method2
  end

  private # this is redundant (no following methods are defined)
end

Example:

class Foo
  # The following is not redundant (conditionally defined methods are
  # considered as always defining a method)
  private

  if condition?
    def method
    end
  end

  protected # this is not redundant (method is defined)

  define_method(:method2) do
  end

  protected # this is redundant (repeated from previous modifier)

  [1,2,3].each do |i|
    define_method("foo#{i}") do
    end
  end

  # The following is redundant (methods defined on the class'
  # singleton class are not affected by the public modifier)
  public

  def self.method3
  end
end

Example:

# Lint/UselessAccessModifier:
#   ContextCreatingMethods:
#     - concerning
require 'active_support/concern'
class Foo
  concerning :Bar do
    def some_public_method
    end

    private

    def some_private_method
    end
  end

  # this is not redundant because `concerning` created its own context
  private

  def some_other_private_method
  end
end

There are no issues that match your filters.

Category
Status
# 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

Size

Lines of code
61