class DesignerNews::Response::Error
Public Class Methods
from_response(response)
click to toggle source
# File lib/designer_news/response/error.rb, line 5 def self.from_response(response) status = response[:status].to_i body = response[:body].to_s headers = response[:response_headers] if klass = case status when 400 then DesignerNews::Response::BadRequest when 401 then DesignerNews::Response::Unauthorized when 403 then DesignerNews::Response::Forbidden when 404 then DesignerNews::Response::NotFound when 406 then DesignerNews::Response::NotAcceptable when 400..499 then DesignerNews::Response::ClientError when 500 then DesignerNews::Response::InternalServerError when 501 then DesignerNews::Response::NotImplemented when 502 then DesignerNews::Response::BadGateway when 503 then DesignerNews::Response::ServiceUnavailable when 500..599 then DesignerNews::Response::ServerError end klass.new(response) end end
new(response=nil)
click to toggle source
Calls superclass method
# File lib/designer_news/response/error.rb, line 27 def initialize(response=nil) @response = response super(build_error_message) end
Public Instance Methods
errors()
click to toggle source
# File lib/designer_news/response/error.rb, line 32 def errors if data && data.is_a?(Hash) data[:errors] || [] else [] end end
Private Instance Methods
build_error_message()
click to toggle source
# File lib/designer_news/response/error.rb, line 42 def build_error_message return nil if @response.nil? message = "#{@response[:method].to_s.upcase} " message << "#{@response[:url].to_s}: " message << "#{@response[:status]} - " message << "#{response_message}" unless response_message.nil? message << "#{response_error}" unless response_error.nil? message << "#{response_error_summary}" unless response_error_summary.nil? message end
data()
click to toggle source
# File lib/designer_news/response/error.rb, line 78 def data @data ||= if (body = @response[:body]) && !body.empty? if body.is_a?(String) && @response[:response_headers] && @response[:response_headers][:content_type] =~ /json/ MultiJson.load(body) else body end else nil end end
response_error()
click to toggle source
# File lib/designer_news/response/error.rb, line 63 def response_error "Error: #{data[:error]}" if data.is_a?(Hash) && data[:error] end
response_error_summary()
click to toggle source
# File lib/designer_news/response/error.rb, line 67 def response_error_summary return nil unless data.is_a?(Hash) && !Array(data[:errors]).empty? summary = "\nError summary:\n" summary << data[:errors].map do |hash| hash.map { |k,v| " #{k}: #{v}" } end.join("\n") summary end
response_message()
click to toggle source
# File lib/designer_news/response/error.rb, line 54 def response_message case data when Hash data[:message] when String data end end