Class: ToastController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controller/toast_controller.rb

Instance Method Summary collapse

Instance Method Details

#catch_allObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controller/toast_controller.rb', line 5

def catch_all

  begin

    @resource = Toast::Resource.build( params, request )

    unless request.headers["LINK"].nil?
      # extract "path_info" from link header
      request.headers["LINK"] =~ /(#{request.protocol + request.host + request.script_name})(.*)/
    end

    toast_response = @resource.apply(request.method, request.body.read, request.content_type, $2)

    # pagination
    if pi = toast_response[:pagination_info]
      # URL w/o parameters

      url =  request.url.split('?').first
      qpar = request.query_parameters.clone

      # change/add page parameter
      link_header = []

      if pi[:prev]
        qpar[:page] = pi[:prev]
        response.link "#{url}?#{qpar.to_query}", :rel => :prev
      end

      if pi[:next]
        qpar[:page] = pi[:next]
        response.link "#{url}?#{qpar.to_query}", :rel => :next
      end

      qpar[:page] = pi[:last]
      response.link "#{url}?#{qpar.to_query}", :rel => :last

      qpar[:page] = 1
      response.link "#{url}?#{qpar.to_query}", :rel => :first

    end

    render toast_response

  rescue Toast::ResourceNotFound => e
    return head(:not_found)

  rescue Toast::PayloadInvalid => e
    return render :text => e.message, :status => :forbidden

  rescue Toast::PayloadFormatError => e
    return head(:bad_request)

  rescue Toast::BadRequest => e
    return head(:bad_request)

  rescue Toast::MethodNotAllowed => e
    return head(:method_not_allowed)

  rescue Toast::UnsupportedMediaType => e
    return head(:unsupported_media_type)

  rescue Toast::ResourceNotAcceptable => e
    return head(:not_acceptable)

  rescue Toast::Conflict => e
    return render :text => e.message, :status => :conflict

  rescue Exception => e
    log_exception e
    puts e if Rails.env == "test"
    return head(:internal_server_error)
  end

end

#log_exception(e) ⇒ Object



86
87
88
# File 'app/controller/toast_controller.rb', line 86

def log_exception e
  puts "#{e.class}: '#{e.message}'\n\n#{e.backtrace[0..14].join("\n")}\n\n"
end

#not_foundObject



80
81
82
# File 'app/controller/toast_controller.rb', line 80

def not_found
  return head(:not_found)
end