Module: Lesli::ResponderInterface

Included in:
ApplicationDeviseController, ApplicationLesliController
Defined in:
app/interfaces/lesli/responder_interface.rb

Instance Method Summary collapse

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



117
118
119
# File 'app/interfaces/lesli/responder_interface.rb', line 117

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



122
123
124
125
126
127
128
129
130
131
132
# File 'app/interfaces/lesli/responder_interface.rb', line 122

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



135
136
137
138
139
140
141
# File 'app/interfaces/lesli/responder_interface.rb', line 135

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(message = nil) ⇒ Object

JSON not found response



75
76
77
78
79
80
81
82
# File 'app/interfaces/lesli/responder_interface.rb', line 75

def respond_with_not_found message=nil

    @message = message || I18n.t("core.shared.messages_danger_not_found")
    respond_to do |format|
        format.json{ respond_with_http(404, { message: @message }) }
        format.html{ render('lesli/errors/not_found', status: :not_found) }
    end
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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/interfaces/lesli/responder_interface.rb', line 85

def respond_with_unauthorized(detail = {})

    @error_object = {
        error_role: nil,
        error_detail: nil,
        error_message: I18n.t("core.shared.view_text_unauthorized_request")
    }

    unless Rails.env.production?
        @error_object[:error_detail] = detail unless detail.empty?
        if current_user.present?
            @error_object[:error_role] = "( #{current_user.lesliroles.map(&:name).join(', ')} )"
        end
    end

    respond_to do |format|
        format.json{ render(status: :unauthorized, json: @error_object) }
        format.html{ render('lesli/errors/unauthorized', status: :unauthorized) }

        # format.xlsx do
        #   if Rails.env.production?
        #     redirect_to "/401" # Or a specific Excel error download if applicable
        #   else
        #     # For development, you might still want a JSON response for debugging
        #     render status: :unauthorized, json: error_object.to_json
        #   end
        # end
    end
end