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 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 Ruby on Rails >= 3.1.0 (currently tested up to 3.2.6)
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
- render @resource.apply(request.method, request.body.read, request.content_type)
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"
@@ -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
@@ -122,5 +122,13 @@ module Toast
122
122
  def delete
123
123
  raise MethodNotAllowed
124
124
  end
125
+
126
+ def link l
127
+ raise MethodNotAllowed
128
+ end
129
+
130
+ def unlink l
131
+ raise MethodNotAllowed
132
+ end
125
133
  end
126
134
  end
@@ -16,6 +16,8 @@ module Toast
16
16
  @pass_params_to = []
17
17
  @in_collection = ConfigDSL::InCollection.new model, self
18
18
  @media_type = "application/json"
19
+
20
+ @model.attr_accessible []
19
21
  end
20
22
 
21
23
  def exposed_attributes
@@ -93,5 +93,12 @@ module Toast
93
93
  }
94
94
  end
95
95
 
96
+ def link l
97
+ raise MethodNotAllowed
98
+ end
99
+
100
+ def unlink l
101
+ raise MethodNotAllowed
102
+ end
96
103
  end
97
104
  end
@@ -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
- self.send(method.downcase, payload, payload_media_type)
106
- when "DELETE","GET"
107
- self.send(method.downcase)
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
@@ -77,5 +77,13 @@ module Toast
77
77
  def delete
78
78
  raise MethodNotAllowed
79
79
  end
80
+
81
+ def link l
82
+ raise MethodNotAllowed
83
+ end
84
+
85
+ def unlink l
86
+ raise MethodNotAllowed
87
+ end
80
88
  end
81
89
  end
@@ -1,3 +1,3 @@
1
1
  module Toast
2
- VERSION = '0.7.0'
2
+ VERSION = '0.7.1'
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 7
8
- - 0
9
- version: 0.7.0
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-10 00:00:00 +02:00
17
+ date: 2012-07-13 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency