class FaradayMiddleware::RaiseHttpException

@private

Public Class Methods

new(app) click to toggle source
Calls superclass method
# File lib/gbifrb/faraday.rb, line 27
def initialize(app)
  super app
  @parser = nil
end

Public Instance Methods

call(env) click to toggle source
# File lib/gbifrb/faraday.rb, line 8
def call(env)
  @app.call(env).on_complete do |response|
    case response[:status].to_i
    when 400
      raise Gbif::BadRequest, error_message_400(response)
    when 404
      raise Gbif::NotFound, error_message_400(response)
    when 500
      raise Gbif::InternalServerError, error_message_500(response, "Something is technically wrong.")
    when 502
      raise Gbif::BadGateway, error_message_500(response, "The server returned an invalid or incomplete response.")
    when 503
      raise Gbif::ServiceUnavailable, error_message_500(response, "Crossref is rate limiting your requests.")
    when 504
      raise Gbif::GatewayTimeout, error_message_500(response, "504 Gateway Time-out")
    end
  end
end

Private Instance Methods

error_body(body) click to toggle source
# File lib/gbifrb/faraday.rb, line 38
def error_body(body)
  if not body.nil? and not body.empty? and body.kind_of?(String)
    if is_json?(body)
      body = ::MultiJson.load(body)
      if body['message'].nil?
        body = nil
      elseif body['message'].length == 1
        body = body['message']
      else
        body = body['message'].collect { |x| x['message'] }.join('; ')
      end
    end
  end

  if body.nil?
    nil
  else
    ": #{body}"
  end
end
error_message_400(response) click to toggle source
# File lib/gbifrb/faraday.rb, line 34
def error_message_400(response)
  "\n   #{response[:method].to_s.upcase} #{response[:url].to_s}\n   Status #{response[:status]}#{error_body(response[:body])}"
end
error_message_500(response, body=nil) click to toggle source
# File lib/gbifrb/faraday.rb, line 59
def error_message_500(response, body=nil)
  "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s + ':', body].compact.join(' ')}"
end
is_json?(string) click to toggle source
# File lib/gbifrb/faraday.rb, line 63
def is_json?(string)
  MultiJson.load(string)
  return true
rescue MultiJson::ParseError
  return false
end