Class: Digup::Responder

Inherits:
Object show all
Defined in:
lib/digup/responder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#headersObject

Returns the value of attribute headers.



5
6
7
# File 'lib/digup/responder.rb', line 5

def headers
  @headers
end

#requestObject

Returns the value of attribute request.



5
6
7
# File 'lib/digup/responder.rb', line 5

def request
  @request
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/digup/responder.rb', line 5

def response
  @response
end

#response_bodyObject

Returns the value of attribute response_body.



5
6
7
# File 'lib/digup/responder.rb', line 5

def response_body
  @response_body
end

#statusObject

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_jsonObject

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_responseObject

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

Returns:

  • (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_parametersObject

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_typeObject



95
96
97
# File 'lib/digup/responder.rb', line 95

def content_type
  headers['Content-Type'].split(';').first
end

#empty_response?Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


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