Module: Angus::Responses
- Included in:
- RequestHandler
- Defined in:
- lib/angus/responses.rb
Constant Summary collapse
- HTTP_STATUS_CODE_OK =
200- HTTP_STATUS_CODE_FORBIDDEN =
403- HTTP_STATUS_CODE_NOT_FOUND =
404- HTTP_STATUS_CODE_CONFLICT =
409- HTTP_STATUS_CODE_UNPROCESSABLE_ENTITY =
422- HTTP_STATUS_CODE_INTERNAL_SERVER_ERROR =
500
Instance Method Summary collapse
-
#build_data_response(data, attributes, messages = []) ⇒ Object
Builds a success response with the received data.
-
#build_error_response(error) ⇒ Object
Builds a service error response.
-
#build_message(key, level, *params) ⇒ ResponseMessage
Builds a ResponseMessage object.
-
#build_messages(level, messages) ⇒ Array<ResponseMessage>
Builds a list of messages with the following level.
-
#build_no_data_response(messages = []) ⇒ String
Builds a success response with no elements.
-
#build_success_response(elements = {}, messages = []) ⇒ String
Builds a service success response.
-
#build_warning_response(error) ⇒ Object
Builds a service success response.
-
#get_error_definition(error) ⇒ Hash
Returns the error definition.
-
#get_error_status_code(error) ⇒ Integer
Returns a suitable HTTP status code for the given error.
- #get_message_definition(key, level) ⇒ Object
-
#json(element) ⇒ Object
Sets the content_type to json and serialize
elementas json.
Instance Method Details
#build_data_response(data, attributes, messages = []) ⇒ Object
Builds a success response with the received data
127 128 129 130 131 132 133 |
# File 'lib/angus/responses.rb', line 127 def build_data_response(data, attributes, = []) marshalled_data = Angus::Marshalling.marshal_object(data, attributes) = (Angus::SDoc::Definitions::Message::INFO_LEVEL, ) build_success_response(marshalled_data, ) end |
#build_error_response(error) ⇒ Object
Builds a service error response
79 80 81 82 |
# File 'lib/angus/responses.rb', line 79 def build_error_response(error) = (error) build_response(:error, *) end |
#build_message(key, level, *params) ⇒ ResponseMessage
Builds a ResponseMessage object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/angus/responses.rb', line 93 def (key, level, *params) = (key, level) unless raise NameError.new("Could not found message with key: #{key}, level: #{level}") end description = if .text .text % params else .description end Angus::SDoc::Definitions::Message = Angus::SDoc::Definitions::Message.new .key = key .level = level .description = description end |
#build_messages(level, messages) ⇒ Array<ResponseMessage>
Builds a list of messages with the following level
ResponseMessage objects contained in messages param won’t be modified, this method
only creates ResponseMessage for each Symbol in array
161 162 163 164 165 166 167 168 169 |
# File 'lib/angus/responses.rb', line 161 def (level, ) ( || []).map do || if .kind_of?(Angus::SDoc::Definitions::Message) else (, level) end end end |
#build_no_data_response(messages = []) ⇒ String
Builds a success response with no elements
The response would include the following:
- status
-
144 145 146 147 148 |
# File 'lib/angus/responses.rb', line 144 def build_no_data_response( = []) = (Angus::SDoc::Definitions::Message::INFO_LEVEL, ) build_success_response({}, ) end |
#build_success_response(elements = {}, messages = []) ⇒ String
Builds a service success response
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/angus/responses.rb', line 66 def build_success_response(elements = {}, = []) elements = { :status => :success, }.merge(elements) unless .empty? elements[:messages] = end json(elements) end |
#build_warning_response(error) ⇒ Object
Builds a service success response
117 118 119 120 |
# File 'lib/angus/responses.rb', line 117 def build_warning_response(error) = (error, :warning) build_response(:success, *) end |
#get_error_definition(error) ⇒ Hash
Returns the error definition.
If the error does not responds to error_key nil will be returned, see EvolutionError.
46 47 48 49 50 |
# File 'lib/angus/responses.rb', line 46 def get_error_definition(error) error_key = error.class.name (error_key, Angus::SDoc::Definitions::Message::ERROR_LEVEL) end |
#get_error_status_code(error) ⇒ Integer
Returns a suitable HTTP status code for the given error
If error param responds to #errors, then #HTTP_STATUS_CODE_CONFLICT will be returned.
If error param responds to #error_key, then the status_code associated
with the will be returned.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/angus/responses.rb', line 25 def get_error_status_code(error) if error.respond_to?(:errors) return HTTP_STATUS_CODE_CONFLICT end = get_error_definition(error) if .status_code else HTTP_STATUS_CODE_INTERNAL_SERVER_ERROR end end |
#get_message_definition(key, level) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/angus/responses.rb', line 52 def (key, level) = @definitions..find { |name, definition| name == key.to_s && definition.level.downcase == level.downcase } .last if end |
#json(element) ⇒ Object
Sets the content_type to json and serialize element as json
172 173 174 175 |
# File 'lib/angus/responses.rb', line 172 def json(element) #content_type :json JSON(element, :ascii_only => true) end |