entity_responder 1.0.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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in entity_responder.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jason Rust
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Responders::EntityResponder
2
+
3
+ The default [Rails
4
+ Responder](http://api.rubyonrails.org/classes/ActionController/Responder.html)
5
+ returns `head :no_content` for PUT and DELETE requests for non-HTML
6
+ requests (JSON, XML, etc.). Some APIs or the JS frameworks that consume
7
+ them would prefer that the responses from those operations include the
8
+ entity in the same way that a POST request to create an entity does.
9
+
10
+ Per [RFC 2616 Section
11
+ 9.7](http://tools.ietf.org/html/rfc2616#section-9.7) either way of
12
+ responding is valid:
13
+
14
+ > A successful response SHOULD be 200 (OK) if the response includes an
15
+ > entity describing the status, 202 (Accepted) if the action has not
16
+ > yet been enacted, or 204 (No Content) if the action has been
17
+ > enacted but the response does not include an entity.
18
+
19
+ This Responder takes the former approach and returns a 200 response and
20
+ the serialized version of the resource for PUT and DELETE requests.
21
+
22
+ ## Installation
23
+
24
+ Add this line to your application's Gemfile:
25
+
26
+ gem 'entity_responder'
27
+
28
+ And then execute:
29
+
30
+ $ bundle
31
+
32
+ Or install it yourself as:
33
+
34
+ $ gem install entity_responder
35
+
36
+ ## Usage
37
+
38
+ Add `Responders::EntityResponder` to your responder chain:
39
+
40
+ ```ruby
41
+ class AppResponder < Responder
42
+ include Responders::EntityResponder
43
+ end
44
+
45
+ class MyController < ApplicationController
46
+ self.responder = AppResponder
47
+ end
48
+ ```
49
+
50
+ Or use it with
51
+ [plataformatec/responders](https://github.com/plataformatec/responders):
52
+
53
+ ```ruby
54
+ class MyController < ApplicationController
55
+ responders Responders::EntityResponder
56
+ end
57
+ ```
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs.push "test"
6
+ t.test_files = FileList['test/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'entity_responder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "entity_responder"
8
+ spec.version = EntityResponder::VERSION
9
+ spec.authors = ["Jason Rust"]
10
+ spec.email = ["jason@lessonplanet.com"]
11
+ spec.description = %q{A Rails responder that returns an entity in the response body rather than the :no_content header.}
12
+ spec.summary = %q{A Rails responder that returns an entity in the response body rather than the :no_content header.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest-ansi"
24
+ spec.add_development_dependency "actionpack"
25
+ end
@@ -0,0 +1,7 @@
1
+ module EntityResponder
2
+ autoload :VERSION, 'entity_responder/version'
3
+ end
4
+
5
+ module Responders
6
+ autoload :EntityResponder, 'responders/entity_responder'
7
+ end
@@ -0,0 +1,12 @@
1
+ module EntityResponder
2
+ module VERSION
3
+ MAJOR = 1
4
+ MINOR = 0
5
+ PATCH = 0
6
+ STAGE = nil
7
+
8
+ def self.to_s
9
+ [MAJOR, MINOR, PATCH, STAGE].reject(&:nil?).join('.')
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ module Responders
2
+ module EntityResponder
3
+ protected
4
+
5
+ # This is the common behavior for formats associated with APIs, such as :xml and :json.
6
+ def api_behavior(error)
7
+ raise error unless resourceful?
8
+ if !get? && !post?
9
+ display resource, :status => :ok, :location => api_location
10
+ else
11
+ super
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class EntityResponderTest < ActionController::TestCase
4
+ tests ResourcesController
5
+
6
+ def setup
7
+ @id = 1
8
+ @resource = Resource.new(@id)
9
+ end
10
+
11
+ def test_get_resource
12
+ get :show, id: @id, :format => :json
13
+
14
+ assert_equal 200, response.status
15
+ refute_empty response.body.strip
16
+ end
17
+
18
+ def test_post_resource
19
+ post :create, :format => :json
20
+
21
+ assert_equal 201, response.status
22
+ refute_empty response.body.strip
23
+ end
24
+
25
+ def test_put_resource
26
+ put :update, id: @id, :format => :json
27
+
28
+ assert_equal 200, response.status
29
+ refute_empty response.body.strip
30
+ end
31
+
32
+ def test_delete_resource
33
+ delete :destroy, id: @id, :format => :json
34
+
35
+ assert_equal 200, response.status
36
+ refute_empty response.body.strip
37
+ end
38
+ end
@@ -0,0 +1,63 @@
1
+ require 'minitest/autorun'
2
+ require 'bundler'
3
+
4
+ Bundler.setup
5
+
6
+ # Configure Rails
7
+ ENV["RAILS_ENV"] = "test"
8
+
9
+ require 'minitest/ansi'
10
+ MiniTest::ANSI.use!
11
+
12
+ require 'action_controller'
13
+ require 'action_controller/test_case'
14
+ require 'entity_responder'
15
+
16
+ Routes = ActionDispatch::Routing::RouteSet.new
17
+ Routes.draw do
18
+ resources :resources
19
+ end
20
+
21
+ class ActiveSupport::TestCase
22
+ setup do @routes = Routes end
23
+ end
24
+
25
+ class AppResponder < ActionController::Responder
26
+ include Responders::EntityResponder
27
+ end
28
+
29
+ class ResourcesController < ActionController::Base
30
+ include Routes.url_helpers
31
+ self.responder = AppResponder
32
+ before_filter :set_resource
33
+ respond_to :json
34
+
35
+ def action
36
+ options = params.slice(:flash, :flash_now)
37
+ flash[:success] = "Flash is set" if params[:set_flash]
38
+ respond_with(@resource, options)
39
+ end
40
+ alias :show :action
41
+ alias :create :action
42
+ alias :update :action
43
+ alias :destroy :action
44
+
45
+ def set_resource
46
+ @resource = Resource.new(params[:id].to_i)
47
+ end
48
+ end
49
+
50
+ class Resource
51
+ attr_accessor :id
52
+
53
+ include ActiveModel::Conversion
54
+ include ActiveModel::Validations
55
+
56
+ def initialize(id)
57
+ @id = id
58
+ end
59
+
60
+ def persisted?
61
+ true
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: entity_responder
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason Rust
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: minitest-ansi
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: actionpack
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: A Rails responder that returns an entity in the response body rather
79
+ than the :no_content header.
80
+ email:
81
+ - jason@lessonplanet.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - entity_responder.gemspec
92
+ - lib/entity_responder.rb
93
+ - lib/entity_responder/version.rb
94
+ - lib/responders/entity_responder.rb
95
+ - test/entity_responder_test.rb
96
+ - test/test_helper.rb
97
+ homepage: ''
98
+ licenses:
99
+ - MIT
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 1.8.23
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: A Rails responder that returns an entity in the response body rather than
122
+ the :no_content header.
123
+ test_files:
124
+ - test/entity_responder_test.rb
125
+ - test/test_helper.rb