Sha256: 17917a262ca0b30dc89e778fd4590a77f6101cba63d0a59568fb749b0184fb35

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 KB

Contents

module GeoNamesAPI
  class Error < StandardError
    # See http://www.geonames.org/export/webservice-exception.html
    def self.from_status(status)
      error_code = status['value'].to_i
      error_class = for_error_code(error_code) || self
      raise error_class, "#{status['message']} (#{error_code})"
    end

    def self.for_error_code(error_code)
      @lookup ||= subclasses.reduce({}) do |h, subclass|
        subclass::ERROR_CODES.each { |ea| h[ea] = subclass }
        h
      end
      @lookup[error_code]
    end
  end

  class AuthorizationException < Error
    ERROR_CODES = [10]
  end
  class RecordDoesNotExist < Error
    ERROR_CODES = [11]
  end
  class Timeout < Error
    # 12 is "other error", but is normally "Cannot get a connection, pool error Timeout waiting for idle object"
    ERROR_CODES = [12, 13, 22]
  end
  class InvalidParameter < Error
    ERROR_CODES = [14]
  end
  class NoResultFound < Error
    ERROR_CODES = [15]
  end
  class DuplicateException < Error
    ERROR_CODES = [16]
  end
  class PostalCodeNotFound < Error
    ERROR_CODES = [17]
  end
  class LimitExceeded < Error
    ERROR_CODES = []
  end
  class DailyLimitExceeded < LimitExceeded
    ERROR_CODES = [18]
  end
  class HourlyLimitExceeded < LimitExceeded
    ERROR_CODES = [19]
  end
  class WeeklyLimitExceeded < LimitExceeded
    ERROR_CODES = [20]
  end
  class ServiceNotImplemented < Error
    ERROR_CODES = [23]
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
geonames_api-0.1.6 lib/geonames_api/error.rb
geonames_api-0.1.5 lib/geonames_api/error.rb
geonames_api-0.1.4 lib/geonames_api/error.rb
geonames_api-0.1.3 lib/geonames_api/error.rb