class Yelp::Fusion::Error::ResponseValidator

Class to handle error responses

Public Instance Methods

validate(response) click to toggle source

If the request is not successful, raise an appropriate Yelp::Error exception with the error text from the request response. @param response from the Yelp API

# File lib/yelp/fusion/error.rb, line 33
def validate(response)
  return if successful_response?(response)

  raise error_from_response(response)
end

Private Instance Methods

error_classes() click to toggle source

Maps from API Error id's to Yelp::Error exception classes.

# File lib/yelp/fusion/error.rb, line 55
def error_classes
  @error_classes ||= Hash.new do |hash, key|
    class_name = key.split('_').map(&:capitalize).join('')
    hash[key] = Yelp::Fusion::Error.const_get(class_name)
  end
end
error_from_response(response) click to toggle source

Create an initialized exception from the response @return [Yelp::Error::Base] exception corresponding to API error

# File lib/yelp/fusion/error.rb, line 48
def error_from_response(response)
  body = JSON.parse(response.body)
  klass = error_classes[body['error']['code']]
  klass.new(body['error']['description'], body['error'])
end
successful_response?(response) click to toggle source
# File lib/yelp/fusion/error.rb, line 41
def successful_response?(response)
  # check if the status is in the range of non-error status codes
  (200..399).cover?(response.status)
end