Module: Lesli::Interfaces::Application::Responder

Included in:
ApplicationDeviseController, ApplicationLesliController
Defined in:
app/controllers/lesli/interfaces/application/responder.rb

Instance Method Summary collapse

Instance Method Details

#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” })



104
105
106
# File 'app/controllers/lesli/interfaces/application/responder.rb', line 104

def respond_with_action(action, message = "Action Required")
    respond_with_http(490, { :message => message, :action => action })
end

#respond_with_error(message = "", details = []) ⇒ Object

JSON failure response



109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/lesli/interfaces/application/responder.rb', line 109

def respond_with_error(message = "", details = [])
    # Message should be a String
    message = "" unless message.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 => message, :details => details })
end

#respond_with_http(status, payload) ⇒ Object

Respond with an standard http message



122
123
124
125
126
127
128
# File 'app/controllers/lesli/interfaces/application/responder.rb', line 122

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_foundObject

JSON not found response



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/lesli/interfaces/application/responder.rb', line 66

def respond_with_not_found
    # Keep compatibility with Deutsche Leibrenten
    if defined? DeutscheLeibrenten
        response_body = {
            successful: false,
            error: {
                message: I18n.t("core.shared.messages_danger_not_found"),
                details: []
            }
        }
        return render(status: 404, json: response_body.to_json)
    end

    respond_with_http(404, {
                          message: I18n.t("core.shared.messages_danger_not_found")
                      })
end

#respond_with_pagination(records, payload = nil) ⇒ Object

Usage example tasks = Task .joins(:detail) .page(query[:page]) .per(query[:perPage])

respond_with_successful_pagination(tasks)

IMPORTANT: It is strictly necessary to use the pagination methods

to make this work properly


53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/lesli/interfaces/application/responder.rb', line 53

def respond_with_pagination(records, payload = nil)
    respond_with_http(200, {
        pagination: {
            page: records.current_page,
            pages: records.total_pages,
            total: records.total_count,
            results: records.length
        },
        records: payload || records
    })
end

#respond_with_successful(payload = nil) ⇒ Object

Return an standard http 200 respond



38
39
40
41
# File 'app/controllers/lesli/interfaces/application/responder.rb', line 38

def respond_with_successful(payload = nil)
    # Response for modern Lesli 3 apps
    respond_with_http(200, payload)
end

#respond_with_unauthorized(detail = {}) ⇒ Object

JSON not found response



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/controllers/lesli/interfaces/application/responder.rb', line 85

def respond_with_unauthorized(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.roles.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