toast 0.7.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +4 -1
- data/app/controller/toast_controller.rb +9 -1
- data/lib/toast/association.rb +56 -0
- data/lib/toast/collection.rb +8 -0
- data/lib/toast/config_dsl.rb +2 -0
- data/lib/toast/record.rb +7 -0
- data/lib/toast/resource.rb +16 -5
- data/lib/toast/single.rb +8 -0
- data/lib/toast/version.rb +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -14,7 +14,10 @@ Its main features are:
|
|
14
14
|
* exposing data values with JSON maps
|
15
15
|
* exposing associations by links (URLs)
|
16
16
|
|
17
|
-
Toast works with
|
17
|
+
Toast works with
|
18
|
+
|
19
|
+
* Ruby on Rails >= 3.1.0 (currently tested up to 3.2.6)
|
20
|
+
* Ruby 1.8.7 and 1.9.3
|
18
21
|
|
19
22
|
WARNING
|
20
23
|
=======
|
@@ -6,7 +6,12 @@ class ToastController < ApplicationController
|
|
6
6
|
|
7
7
|
@resource = Toast::Resource.build( params, request )
|
8
8
|
|
9
|
-
|
9
|
+
unless request.headers["LINK"].nil?
|
10
|
+
# extract "path_info" from link header
|
11
|
+
request.headers["LINK"] =~ /(#{request.protocol + request.host + request.script_name})(.*)/
|
12
|
+
end
|
13
|
+
|
14
|
+
render @resource.apply(request.method, request.body.read, request.content_type, $2)
|
10
15
|
|
11
16
|
rescue Toast::ResourceNotFound => e
|
12
17
|
return head(:not_found)
|
@@ -23,6 +28,9 @@ class ToastController < ApplicationController
|
|
23
28
|
rescue Toast::UnsupportedMediaType => e
|
24
29
|
return head(:unsupported_media_type)
|
25
30
|
|
31
|
+
rescue Toast::ResourceNotAcceptable => e
|
32
|
+
return head(:not_acceptable)
|
33
|
+
|
26
34
|
rescue Exception => e
|
27
35
|
log_exception e
|
28
36
|
puts e if Rails.env == "test"
|
data/lib/toast/association.rb
CHANGED
@@ -104,5 +104,61 @@ module Toast
|
|
104
104
|
def delete
|
105
105
|
raise MethodNotAllowed
|
106
106
|
end
|
107
|
+
|
108
|
+
def link link_path_info
|
109
|
+
|
110
|
+
slash, link_resource_name, link_id = link_path_info.split('/')
|
111
|
+
link_model = Resource.get_class_by_resource_name(link_resource_name)
|
112
|
+
link_record = link_model.find(link_id)
|
113
|
+
|
114
|
+
if @model.reflect_on_association(@assoc.to_sym).collection?
|
115
|
+
# has_many, hbtm
|
116
|
+
@record.send(@assoc) << link_record
|
117
|
+
else
|
118
|
+
# has_one, belongs_to
|
119
|
+
@record.send(@assoc+"=",link_record)
|
120
|
+
@record.save
|
121
|
+
end
|
122
|
+
|
123
|
+
{
|
124
|
+
:nothing => true,
|
125
|
+
:status => :ok
|
126
|
+
}
|
127
|
+
|
128
|
+
rescue ActiveRecord::AssociationTypeMismatch
|
129
|
+
raise Toast::ResourceNotAcceptable
|
130
|
+
rescue ActiveRecord::RecordNotFound
|
131
|
+
raise Toast::ResourceNotFound
|
132
|
+
end
|
133
|
+
|
134
|
+
def unlink link_path_info
|
135
|
+
|
136
|
+
slash, link_resource_name, link_id = link_path_info.split('/')
|
137
|
+
link_model = Resource.get_class_by_resource_name(link_resource_name)
|
138
|
+
link_record = link_model.find(link_id)
|
139
|
+
|
140
|
+
if @model.reflect_on_association(@assoc.to_sym).collection?
|
141
|
+
# has_many, hbtm
|
142
|
+
@record.send(@assoc).delete(link_record) unless link_record.nil?
|
143
|
+
else
|
144
|
+
|
145
|
+
# has_one, belongs_to
|
146
|
+
if @record.send(@assoc) == link_record
|
147
|
+
@record.send(@assoc+"=",nil)
|
148
|
+
@record.save!
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
{
|
153
|
+
:nothing => true,
|
154
|
+
:status => :ok
|
155
|
+
}
|
156
|
+
rescue ActiveRecord::RecordNotFound
|
157
|
+
# link_record not linked: nothing to be done
|
158
|
+
{
|
159
|
+
:nothing => true,
|
160
|
+
:status => :ok
|
161
|
+
}
|
162
|
+
end
|
107
163
|
end
|
108
164
|
end
|
data/lib/toast/collection.rb
CHANGED
data/lib/toast/config_dsl.rb
CHANGED
data/lib/toast/record.rb
CHANGED
data/lib/toast/resource.rb
CHANGED
@@ -6,6 +6,7 @@ module Toast
|
|
6
6
|
class PayloadFormatError < Exception; end
|
7
7
|
class UnsupportedMediaType < Exception; end
|
8
8
|
class RequestedVersionNotDefined < Exception; end
|
9
|
+
class ResourceNotAcceptable < Exception; end
|
9
10
|
|
10
11
|
# Represents a resource. There are following resource types as sub classes:
|
11
12
|
# Record, Collection, Association, Single
|
@@ -99,12 +100,22 @@ module Toast
|
|
99
100
|
end
|
100
101
|
end
|
101
102
|
|
102
|
-
def apply method, payload, payload_media_type
|
103
|
+
def apply method, payload, payload_media_type, link_path_info
|
103
104
|
case method
|
104
|
-
when "PUT","POST"
|
105
|
-
|
106
|
-
|
107
|
-
|
105
|
+
when "PUT","POST"
|
106
|
+
if link_path_info
|
107
|
+
self.link link_path_info
|
108
|
+
else
|
109
|
+
self.send(method.downcase, payload, payload_media_type)
|
110
|
+
end
|
111
|
+
when "DELETE"
|
112
|
+
if link_path_info
|
113
|
+
self.unlink link_path_info
|
114
|
+
else
|
115
|
+
self.delete
|
116
|
+
end
|
117
|
+
when "GET"
|
118
|
+
self.get
|
108
119
|
else
|
109
120
|
raise MethodNotAllowed
|
110
121
|
end
|
data/lib/toast/single.rb
CHANGED
data/lib/toast/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 7
|
8
|
-
-
|
9
|
-
version: 0.7.
|
8
|
+
- 1
|
9
|
+
version: 0.7.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- "robokopp (Robert Anni\xC3\xA9s)"
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2012-07-
|
17
|
+
date: 2012-07-13 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|