rocket_pants 1.3.0 → 1.4.0

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.
@@ -83,39 +83,27 @@ module RocketPants
83
83
  render_json normalise_object(object, options), options
84
84
  end
85
85
 
86
+ def respond_with_object_and_type(object, options, type, singular)
87
+ pre_process_exposed_object object, type, singular
88
+ options = options.reverse_merge(:compact => true) unless singular
89
+ meta = expose_metadata metadata_for(object, options, type, singular)
90
+ render_json({:response => normalise_object(object, options)}.merge(meta), options)
91
+ post_process_exposed_object object, type, singular
92
+ end
93
+
86
94
  # Renders a single resource.
87
95
  def resource(object, options = {})
88
- pre_process_exposed_object object, :resource, true
89
- render_json({
90
- :response => normalise_object(object, options)
91
- }, options)
92
- post_process_exposed_object object, :resource, true
96
+ respond_with_object_and_type object, options, :resource, true
93
97
  end
94
98
 
95
99
  # Renders a normal collection to JSON.
96
100
  def collection(collection, options = {})
97
- pre_process_exposed_object collection, :collection, false
98
- options = options.reverse_merge(:compact => true)
99
- meta = expose_metadata(:count => collection.length)
100
- render_json({
101
- :response => normalise_object(collection, options),
102
- :count => collection.length
103
- }.merge(meta), options)
104
- post_process_exposed_object collection, :collection, false
101
+ respond_with_object_and_type collection, options, :collection, false
105
102
  end
106
103
 
107
104
  # Renders a paginated collecton to JSON.
108
105
  def paginated(collection, options = {})
109
- pre_process_exposed_object collection, :paginated, false
110
- options = options.reverse_merge(:compact => true)
111
- meta = expose_metadata({
112
- :count => collection.length,
113
- :pagination => Respondable.extract_pagination(collection)
114
- })
115
- render_json({
116
- :response => normalise_object(collection, options),
117
- }.merge(meta), options)
118
- post_process_exposed_object collection, :paginated, false
106
+ respond_with_object_and_type collection, options, :paginated, false
119
107
  end
120
108
 
121
109
  # Exposes an object to the response - Essentiall, it tells the
@@ -160,5 +148,20 @@ module RocketPants
160
148
  metadata
161
149
  end
162
150
 
151
+ # Extracts the metadata for the current response, merging in options etc.
152
+ # Implements a simple hook to allow adding extra metadata to your api.
153
+ #
154
+ # @param [Object] object the data being exposed
155
+ # @param [Hash{Symbol => Object}] options expose options optionally including new metadata.
156
+ # @param [Symbol] type the type of the current object
157
+ # @param [true,false] singular true iff the current object is a singular resource
158
+ def metadata_for(object, options, type, singular)
159
+ {}.tap do |metadata|
160
+ metadata[:count] = object.length unless singular
161
+ metadata[:pagination] = Respondable.extract_pagination(object) if type == :paginated
162
+ metadata.merge! options[:metadata] if options[:metadata]
163
+ end
164
+ end
165
+
163
166
  end
164
167
  end
@@ -106,6 +106,7 @@ module RocketPants
106
106
  register! :invalid_version, :http_status => :not_found
107
107
  register! :not_implemented, :http_status => :service_unavailable
108
108
  register! :not_found, :http_status => :not_found
109
+ register! :bad_request, :http_status => :bad_request
109
110
 
110
111
  end
111
112
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rocket_pants
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-06 00:00:00.000000000 Z
12
+ date: 2012-05-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionpack
@@ -286,7 +286,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
286
286
  version: 1.3.6
287
287
  requirements: []
288
288
  rubyforge_project:
289
- rubygems_version: 1.8.21
289
+ rubygems_version: 1.8.24
290
290
  signing_key:
291
291
  specification_version: 3
292
292
  summary: JSON API love for Rails and ActionController