rocket_pants 1.1.1 → 1.2.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.
data/lib/rocket_pants.rb CHANGED
@@ -29,6 +29,7 @@ module RocketPants
29
29
  autoload :Caching, 'rocket_pants/controller/caching'
30
30
  autoload :ErrorHandling, 'rocket_pants/controller/error_handling'
31
31
  autoload :Instrumentation, 'rocket_pants/controller/instrumentation'
32
+ autoload :JSONP, 'rocket_pants/controller/jsonp'
32
33
  autoload :Rescuable, 'rocket_pants/controller/rescuable'
33
34
  autoload :Respondable, 'rocket_pants/controller/respondable'
34
35
  autoload :Versioning, 'rocket_pants/controller/versioning'
@@ -25,7 +25,8 @@ module RocketPants
25
25
  AbstractController::Callbacks,
26
26
  ActionController::Rescue,
27
27
  ErrorHandling,
28
- Rescuable
28
+ Rescuable,
29
+ JSONP
29
30
  # FormatVerification # TODO: Implement Format Verification
30
31
  ].compact
31
32
 
@@ -49,5 +50,8 @@ module RocketPants
49
50
 
50
51
  ActiveSupport.run_load_hooks(:rocket_pants, self)
51
52
 
53
+ # Methods for integration purposes.
54
+ def self.helper_method(*); end
55
+
52
56
  end
53
57
  end
@@ -0,0 +1,49 @@
1
+ module RocketPants
2
+ # This mixin implements easy JSONP support in your application,
3
+ # making it easy for developers to support it in their response.
4
+ module JSONP
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ class_attribute :_jsonp_parameter
9
+ self._jsonp_parameter = :callback
10
+ end
11
+
12
+ module ClassMethods
13
+
14
+ # Marks the current controller as supporting JSONP-style responses.
15
+ # @parameter [Hash{Symbol => Object}] options An optional hash of options to configure the callback.
16
+ # Non-specified options are passed to the filter call.
17
+ # @option options [Symbol,String] :parameter If set, specifies the param name of the callback. Defaults to :callback.
18
+ # @option options [true, false] :enable Whether to enable JSONP. true by default.
19
+ def jsonp(options = {})
20
+ enable = options.delete(:enable) { true }
21
+ param = options.delete(:parameter).try(:to_sym)
22
+ if enable
23
+ after_filter :wrap_response_in_jsonp, {:if => :jsonp_is_possible?}.reverse_merge(options)
24
+ self._jsonp_parameter = param if param
25
+ else
26
+ skip_after_filter :wrap_response_in_jsonp, options
27
+ end
28
+ end
29
+
30
+ end
31
+
32
+ private
33
+
34
+ def jsonp_is_possible?
35
+ request.get? && response.content_type == "application/json" && jsonp_parameter.present?
36
+ end
37
+
38
+ def jsonp_parameter
39
+ params[_jsonp_parameter]
40
+ end
41
+
42
+ def wrap_response_in_jsonp
43
+ # Finally, set up the callback using the JSONP parameter.
44
+ response.content_type = 'application/javascript'
45
+ response.body = "#{jsonp_parameter}(#{response.body});"
46
+ end
47
+
48
+ end
49
+ end
@@ -13,7 +13,7 @@ module RocketPants
13
13
  def self.normalise_urls(object)
14
14
  if object.is_a?(Array)
15
15
  object.each { |o| o['url'] = nil }
16
- elsif object.is_a?(Hash) || object.is_a?(APISmith::Smash)
16
+ elsif object.is_a?(Hash) || (defined?(APISmith::Smash) && object.is_a?(APISmith::Smash))
17
17
  object['url'] = nil
18
18
  end
19
19
  object
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.1.1
4
+ version: 1.2.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-04-25 00:00:00.000000000 Z
12
+ date: 2012-05-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionpack
@@ -251,6 +251,7 @@ files:
251
251
  - lib/rocket_pants/controller/error_handling.rb
252
252
  - lib/rocket_pants/controller/format_verification.rb
253
253
  - lib/rocket_pants/controller/instrumentation.rb
254
+ - lib/rocket_pants/controller/jsonp.rb
254
255
  - lib/rocket_pants/controller/rescuable.rb
255
256
  - lib/rocket_pants/controller/respondable.rb
256
257
  - lib/rocket_pants/controller/url_for.rb