Yes, there is a better way to do it. Here's how I would go about it:
class ApiOutputsHandler
attr_accessor :status, :type, :message, :api_code, :http_code, :template
ERR_TYPES = {
:permanent_quota_limit => { :type => 'PermanentLimitException', :message => 'Quota limit reached for this action', :api_code => 700, :http_code => 401 }
}
def initialize(data)
# added it here so that you can pass symbol error code to initializer
data = ERR_TYPES[data] if data.is_a?(Symbol)
data.each do |name, value|
send("#{name}=", value)
end
end
def error
self.status = 'error'
self.template= 'api/v1/api_outputs_handler/output'
self
end
end
This way, you can just pass the symbol error code to the initializer, like this:
handler = ApiOutputsHandler.new(:permanent_quota_limit)
You also can change how your objects looks in console, you just need to redefine the #inspect
method. In your case, it might look like this:
def inspect
"#<#{self.class.name} type: #{type}, message: #{message}>" # etc
end