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.["LINK"].nil?
request.["LINK"] =~ /(#{request.protocol + request.host + request.script_name})(.*)/
end
toast_response = @resource.apply(request.method, request.body.read, request.content_type, $2)
if pi = toast_response[:pagination_info]
url = request.url.split('?').first
qpar = request.query_parameters.clone
= []
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
|