Module: Spine::Actions::Responder

Included in:
Action
Defined in:
lib/spine/actions/responder.rb

Instance Method Summary collapse

Instance Method Details

#build_url(url, data) ⇒ Object



48
49
50
51
52
53
# File 'lib/spine/actions/responder.rb', line 48

def build_url(url, data)
  uri = URI(url)
  query = URI.decode_www_form(uri.query || '') || []
  uri.query = URI.encode_www_form(query + data.to_a)
  uri.to_s
end

#finish_responseObject



43
44
45
46
# File 'lib/spine/actions/responder.rb', line 43

def finish_response
  @responded = true
  response
end

#redirect(url, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/spine/actions/responder.rb', line 33

def redirect(url, options = {})
  response[Rack::CONTENT_TYPE] = 'application/x-www-form-urlencoded'
  response.redirect(
    build_url(url, options.fetch(:data, {})),
    options.fetch(:status, 302)
  )

  finish_response
end

#respond(content, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/spine/actions/responder.rb', line 11

def respond(content, options = {})
  response[Rack::CONTENT_TYPE] = options.fetch(:content_type) {
    format.mime_type
  }
  response.status = options[:status] if options[:status]
  response.write(format.dump(content))

  finish_response
end

#responded?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/spine/actions/responder.rb', line 7

def responded?
  @responded
end

#send_data(binary, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/spine/actions/responder.rb', line 21

def send_data(binary, options = {})
  response['Content-Transfer-Encoding'] = 'binary'
  response[Rack::CONTENT_TYPE] = options.fetch(:content_type, 'application/octet-stream')

  if options[:filename]
    response['Content-Disposition'] = "inline; filename='#{ options[:filename] }'"
  end
  response.write(binary)

  finish_response
end