Module: Lesli::ResponderInterface
- Included in:
- ApplicationDeviseController, ApplicationLesliController
- Defined in:
- app/interfaces/lesli/responder_interface.rb
Instance Method Summary collapse
-
#respond_as_pagination(payload) ⇒ Object
Usage example tasks = Task .joins(:detail) .page(query[:page]) .per(query[:perPage]).
-
#respond_as_successful(payload = nil) ⇒ Object
Return an standard http 200 respond.
-
#respond_with_action(action, message = "Action Required") ⇒ Object
JSON failure response due users has to perform an action example: respond_with_action({ :redirect => “telephone_confirmation” }).
-
#respond_with_error(message = "", details = []) ⇒ Object
JSON failure response.
-
#respond_with_http(status, payload) ⇒ Object
Respond with an standard http message.
-
#respond_with_not_found ⇒ Object
JSON not found response.
- #respond_with_pagination(payload) ⇒ Object
- #respond_with_successful(payload = nil) ⇒ Object
-
#respond_with_unauthorized(detail = {}) ⇒ Object
JSON not found response.
Instance Method Details
#respond_as_pagination(payload) ⇒ Object
Usage example tasks = Task .joins(:detail) .page(query[:page]) .per(query[:perPage])
respond_with_pagination(tasks)
IMPORTANT: It is strictly necessary to use the pagination methods
to make this work properly
51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/interfaces/lesli/responder_interface.rb', line 51 def respond_as_pagination(payload) { pagination: { page: payload.current_page, pages: payload.total_pages, total: payload.total_count, results: payload.length }, records: payload } end |
#respond_as_successful(payload = nil) ⇒ Object
Return an standard http 200 respond
37 38 39 |
# File 'app/interfaces/lesli/responder_interface.rb', line 37 def respond_as_successful(payload = nil) payload end |
#respond_with_action(action, message = "Action Required") ⇒ Object
JSON failure response due users has to perform an action example: respond_with_action({ :redirect => “telephone_confirmation” })
101 102 103 |
# File 'app/interfaces/lesli/responder_interface.rb', line 101 def respond_with_action(action, = "Action Required") respond_with_http(490, { :message => , :action => action }) end |
#respond_with_error(message = "", details = []) ⇒ Object
JSON failure response
106 107 108 109 110 111 112 113 114 115 116 |
# File 'app/interfaces/lesli/responder_interface.rb', line 106 def respond_with_error( = "", details = []) # Message should be a String = "" unless .instance_of?(String) # TODO: # check if active error and then: # message = error message to sentence # details = error array of messages # check another types of errors and parse respond according respond_with_http(400, { :message => , :details => details }) end |
#respond_with_http(status, payload) ⇒ Object
Respond with an standard http message
119 120 121 122 123 124 125 |
# File 'app/interfaces/lesli/responder_interface.rb', line 119 def respond_with_http(status, payload) unless payload.nil? return render(:status => status, content_type: "application/json", json: payload.to_json) end render(:status => status, content_type: "application/json", json: "") end |
#respond_with_not_found ⇒ Object
JSON not found response
75 76 77 78 79 |
# File 'app/interfaces/lesli/responder_interface.rb', line 75 def respond_with_not_found respond_with_http(404, { message: I18n.t("core.shared.messages_danger_not_found") }) end |
#respond_with_pagination(payload) ⇒ Object
70 71 72 |
# File 'app/interfaces/lesli/responder_interface.rb', line 70 def respond_with_pagination(payload) respond_with_http(200, respond_as_pagination(payload)) end |
#respond_with_successful(payload = nil) ⇒ Object
66 67 68 |
# File 'app/interfaces/lesli/responder_interface.rb', line 66 def respond_with_successful(payload = nil) respond_with_http(200, respond_as_successful(payload)) end |
#respond_with_unauthorized(detail = {}) ⇒ Object
JSON not found response
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/interfaces/lesli/responder_interface.rb', line 82 def (detail = {}) error_object = {} error_object[:message] = I18n.t("core.shared.view_text_unauthorized_request") error_object[:detail] = detail if Rails.env == "development" error_object[:role] = "( #{current_user.lesliroles.map(&:name).join(', ')} )" if (Rails.env == "development") && !current_user.blank? respond_to do |format| format.json { render status: 401, json: error_object.to_json } format.html { redirect_to "/401" } if Rails.env == "production" format.html { render status: 401, json: error_object.to_json } # format.xlsx { redirect_to "/401" } if Rails.env == "production" # format.xlsx { render status: 401, json: error_object.to_json } end end |