Class: Digup::Responder
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#request ⇒ Object
Returns the value of attribute request.
-
#response ⇒ Object
Returns the value of attribute response.
-
#response_body ⇒ Object
Returns the value of attribute response_body.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
-
#append_javascript_to_evaluate_json ⇒ Object
Appends javascript to every page when response_type Setting includes :json Appends javascript to display json response appendes to original response.
-
#append_template_to_response(template) ⇒ Object
Appends template to reponse depending on the settings.
-
#build_response ⇒ Object
build a response to be sent to client.
- #can_handle_response? ⇒ Boolean
-
#clear_parameters ⇒ Object
clears log message and reponse_type unless its not a redirection If its redirection log needs to be displayed on redirected page.
- #content_type ⇒ Object
- #empty_response? ⇒ Boolean
- #html_response? ⇒ Boolean
-
#initialize(original_response) ⇒ Responder
constructor
A new instance of Responder.
- #javascript_response? ⇒ Boolean
- #json_response? ⇒ Boolean
- #response_not_a_file? ⇒ Boolean
-
#valid? ⇒ Boolean
If responsder is valid, then only response is modified.
Constructor Details
#initialize(original_response) ⇒ Responder
Returns a new instance of Responder.
7 8 9 10 |
# File 'lib/digup/responder.rb', line 7 def initialize(original_response) @original_response = original_response @status, @headers, @response = original_response end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
5 6 7 |
# File 'lib/digup/responder.rb', line 5 def headers @headers end |
#request ⇒ Object
Returns the value of attribute request.
5 6 7 |
# File 'lib/digup/responder.rb', line 5 def request @request end |
#response ⇒ Object
Returns the value of attribute response.
5 6 7 |
# File 'lib/digup/responder.rb', line 5 def response @response end |
#response_body ⇒ Object
Returns the value of attribute response_body.
5 6 7 |
# File 'lib/digup/responder.rb', line 5 def response_body @response_body end |
#status ⇒ Object
Returns the value of attribute status.
5 6 7 |
# File 'lib/digup/responder.rb', line 5 def status @status end |
Instance Method Details
#append_javascript_to_evaluate_json ⇒ Object
Appends javascript to every page when response_type Setting includes :json Appends javascript to display json response appendes to original response
24 25 26 27 28 29 30 31 |
# File 'lib/digup/responder.rb', line 24 def append_javascript_to_evaluate_json if response_body.include?('</body>') position = response_body.rindex('</body>') response_body.insert(position, Template.javascript_template_to_evaluate_json) else response_body << Template.javascript_template_to_evaluate_json end end |
#append_template_to_response(template) ⇒ Object
Appends template to reponse depending on the settings
13 14 15 16 17 18 19 20 |
# File 'lib/digup/responder.rb', line 13 def append_template_to_response(template) position = if response_body.include?('</body>') response_body.rindex('</body>') else json_response? ? response_body.length - 1 : response_body.length end response_body.insert(position, (json_response? ? '' : "\n") + template ) end |
#build_response ⇒ Object
build a response to be sent to client
51 52 53 |
# File 'lib/digup/responder.rb', line 51 def build_response valid? ? [status, headers, [response_body]] : @original_response end |
#can_handle_response? ⇒ Boolean
67 68 69 |
# File 'lib/digup/responder.rb', line 67 def can_handle_response? Setting.content_type_to_handle.include?(content_type) end |
#clear_parameters ⇒ Object
clears log message and reponse_type unless its not a redirection If its redirection log needs to be displayed on redirected page. So its not cleared
57 58 59 60 61 |
# File 'lib/digup/responder.rb', line 57 def clear_parameters unless [301, 302].include?(status) Digup.clear_all end end |
#content_type ⇒ Object
95 96 97 |
# File 'lib/digup/responder.rb', line 95 def content_type headers['Content-Type'].split(';').first end |
#empty_response? ⇒ Boolean
37 38 39 40 41 42 |
# File 'lib/digup/responder.rb', line 37 def empty_response? (@response.is_a?(Array) && @response.size <= 1) || !@response.respond_to?(:body) || !response_body.respond_to?(:empty?) || response_body.empty? end |
#html_response? ⇒ Boolean
71 72 73 74 75 76 77 |
# File 'lib/digup/responder.rb', line 71 def html_response? if Digup.response_type.present? Digup.response_type == :html else content_type == 'text/html' end end |
#javascript_response? ⇒ Boolean
79 80 81 82 83 84 85 |
# File 'lib/digup/responder.rb', line 79 def javascript_response? if Digup.response_type.present? Digup.response_type == :js else content_type == 'text/javascript' end end |
#json_response? ⇒ Boolean
87 88 89 90 91 92 93 |
# File 'lib/digup/responder.rb', line 87 def json_response? if Digup.response_type.present? Digup.response_type == :json else content_type == 'application/json' end end |
#response_not_a_file? ⇒ Boolean
33 34 35 |
# File 'lib/digup/responder.rb', line 33 def response_not_a_file? headers["Content-Transfer-Encoding"] != "binary" end |
#valid? ⇒ Boolean
If responsder is valid, then only response is modified
45 46 47 48 |
# File 'lib/digup/responder.rb', line 45 def valid? !empty_response? && !response_body.frozen? && response_not_a_file? && status == 200 && can_handle_response? end |