respond_with 0.0.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/.gitignore +19 -0
- data/.travis.yml +8 -0
- data/Gemfile +8 -0
- data/LICENSE +22 -0
- data/README.md +42 -0
- data/Rakefile +6 -0
- data/lib/respond_with/helpers.rb +12 -0
- data/lib/respond_with/response.rb +59 -0
- data/lib/respond_with/version.rb +3 -0
- data/lib/respond_with.rb +9 -0
- data/respond_with.gemspec +21 -0
- data/test/all.rb +3 -0
- data/test/respond_with_test.rb +81 -0
- data/test/support/item_serializer.rb +9 -0
- data/test/support/mock_item.rb +11 -0
- data/test/support/sample_application.rb +9 -0
- data/test/test_helper.rb +12 -0
- metadata +112 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Dane Harrigan
|
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,42 @@
|
|
1
|
+
# RespondWith [][2]
|
2
|
+
|
3
|
+
The `respond_with` gem is an extension of Sinatra. It is intended to
|
4
|
+
make API responses dead simple.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
`respond_with` relies on the serialize gem which is still in development
|
9
|
+
so you'll need to install that from git.
|
10
|
+
|
11
|
+
Add these lines to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'respond_with', :git => 'git://github.com/daneharrigan/respond_with.git'
|
14
|
+
gem 'serialize', :git => 'git://github.com/daneharrigan/serialize.git'
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
require "sinatra"
|
22
|
+
require "respond_with"
|
23
|
+
|
24
|
+
# in a sinatra app
|
25
|
+
get "/resources/:id.?:format?" do
|
26
|
+
@resource = Resource.find(params[:id])
|
27
|
+
respond_with ResourceSerializer.new(@resource)
|
28
|
+
end
|
29
|
+
|
30
|
+
Notice we're using a `ResourceSerializer`. This is built with the
|
31
|
+
[serialize][1] gem.
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create new Pull Request
|
40
|
+
|
41
|
+
[1]: https://github.com/heroku/serialize
|
42
|
+
[2]: https://secure.travis-ci.org/daneharrigan/respond_with
|
data/Rakefile
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
module RespondWith
|
2
|
+
class Response
|
3
|
+
attr :app
|
4
|
+
attr :params
|
5
|
+
attr :object
|
6
|
+
attr :request
|
7
|
+
|
8
|
+
def initialize(options={})
|
9
|
+
@app = options[:app]
|
10
|
+
@params = options[:params]
|
11
|
+
@object = options[:object]
|
12
|
+
@request = options[:request]
|
13
|
+
end
|
14
|
+
|
15
|
+
def render
|
16
|
+
response_type = Negotiator.pick(request_types.join(", "), available_responses)
|
17
|
+
|
18
|
+
if response_type.nil?
|
19
|
+
@app.halt 406
|
20
|
+
else
|
21
|
+
@app.content_type(response_type)
|
22
|
+
@object.to(response_type)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def request_types
|
27
|
+
format = params.delete "format"
|
28
|
+
content_type = MIME::Types.type_for(format).first.to_s if format
|
29
|
+
|
30
|
+
if content_type && request.accept.first != content_type
|
31
|
+
request.accept.delete content_type
|
32
|
+
request.accept.unshift content_type
|
33
|
+
end
|
34
|
+
|
35
|
+
return request.accept
|
36
|
+
end
|
37
|
+
|
38
|
+
private :request_types
|
39
|
+
|
40
|
+
def available_responses
|
41
|
+
responses = request_types.map do |request_type|
|
42
|
+
if @object.responses.include?(request_type)
|
43
|
+
[request_type, 1.0]
|
44
|
+
end
|
45
|
+
end.compact
|
46
|
+
|
47
|
+
if request_types.include? "*/*"
|
48
|
+
@object.responses.each do |response_type|
|
49
|
+
response_rating = [response_type, 1.0]
|
50
|
+
responses << response_rating unless responses.include? response_rating
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
Hash[responses]
|
55
|
+
end
|
56
|
+
|
57
|
+
private :available_responses
|
58
|
+
end
|
59
|
+
end
|
data/lib/respond_with.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/respond_with/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Dane Harrigan"]
|
6
|
+
gem.email = ["dane.harrigan@gmail.com"]
|
7
|
+
gem.description = %q{Make API responses hella simple}
|
8
|
+
gem.summary = %q{Make API responses hella simple}
|
9
|
+
gem.homepage = "https://github.com/daneharrigan/respond_with"
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "respond_with"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = RespondWith::VERSION
|
17
|
+
gem.add_runtime_dependency "sinatra"
|
18
|
+
gem.add_runtime_dependency "negotiator"
|
19
|
+
gem.add_runtime_dependency "mime-types"
|
20
|
+
gem.add_runtime_dependency "serialize"
|
21
|
+
end
|
data/test/all.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class RespondWithTest < MiniTest::Unit::TestCase
|
4
|
+
include Rack::Test::Methods
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@item_hash = { name: "Item", type: "Sample", size: 42 }
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_invalid_extention_renders_406
|
11
|
+
get "/item.invalid"
|
12
|
+
assert_equal 406, last_response.status
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_invalid_accept_header_renders_406
|
16
|
+
header "Accept", "invalid/type"
|
17
|
+
get "/item"
|
18
|
+
assert_equal 406, last_response.status
|
19
|
+
end
|
20
|
+
|
21
|
+
# JSON
|
22
|
+
def test_request_with_json_extension
|
23
|
+
get "/item.json"
|
24
|
+
assert_equal @item_hash.to_json, last_response.body
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_request_with_json_accept_header
|
28
|
+
header "Accept", "application/json"
|
29
|
+
get "/item"
|
30
|
+
|
31
|
+
assert_equal @item_hash.to_json, last_response.body
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_application_json_content_type
|
35
|
+
get "/item.json"
|
36
|
+
assert_equal "application/json;charset=utf-8", last_response.content_type
|
37
|
+
end
|
38
|
+
|
39
|
+
# XML
|
40
|
+
def test_request_with_xml_and_json_accept_header
|
41
|
+
header "Accept", "application/xml, applicaiton/json"
|
42
|
+
get "/item"
|
43
|
+
xml = @item_hash.to_xml(root: "mock-item")
|
44
|
+
|
45
|
+
assert_equal xml, last_response.body
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_request_with_xml_extension
|
49
|
+
get "/item.xml"
|
50
|
+
xml = @item_hash.to_xml(root: "mock-item")
|
51
|
+
|
52
|
+
assert_equal xml, last_response.body
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_request_with_xml_accept_header
|
56
|
+
header "Accept", "application/xml"
|
57
|
+
get "/item"
|
58
|
+
xml = @item_hash.to_xml(root: "mock-item")
|
59
|
+
|
60
|
+
assert_equal xml, last_response.body
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_application_xml_content_type
|
64
|
+
get "/item.xml"
|
65
|
+
assert_equal "application/xml;charset=utf-8", last_response.content_type
|
66
|
+
end
|
67
|
+
|
68
|
+
def app
|
69
|
+
MyApp
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class MyApp < Sinatra::Base
|
74
|
+
get "/item" do
|
75
|
+
respond_with ItemSerializer.new(MockItem.new)
|
76
|
+
end
|
77
|
+
|
78
|
+
get "/item.:format" do
|
79
|
+
respond_with ItemSerializer.new(MockItem.new)
|
80
|
+
end
|
81
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "bundler/setup"
|
2
|
+
require "minitest/autorun"
|
3
|
+
require "rack/test"
|
4
|
+
|
5
|
+
require "respond_with"
|
6
|
+
require "serialize"
|
7
|
+
|
8
|
+
require "support/mock_item"
|
9
|
+
require "support/item_serializer"
|
10
|
+
require "support/sample_application"
|
11
|
+
|
12
|
+
ENV["RACK_ENV"] = "test"
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: respond_with
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dane Harrigan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: sinatra
|
16
|
+
requirement: &70118407496880 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70118407496880
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: negotiator
|
27
|
+
requirement: &70118407496380 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70118407496380
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: mime-types
|
38
|
+
requirement: &70118407495840 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70118407495840
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: serialize
|
49
|
+
requirement: &70118407495200 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70118407495200
|
58
|
+
description: Make API responses hella simple
|
59
|
+
email:
|
60
|
+
- dane.harrigan@gmail.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- .gitignore
|
66
|
+
- .travis.yml
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- lib/respond_with.rb
|
72
|
+
- lib/respond_with/helpers.rb
|
73
|
+
- lib/respond_with/response.rb
|
74
|
+
- lib/respond_with/version.rb
|
75
|
+
- respond_with.gemspec
|
76
|
+
- test/all.rb
|
77
|
+
- test/respond_with_test.rb
|
78
|
+
- test/support/item_serializer.rb
|
79
|
+
- test/support/mock_item.rb
|
80
|
+
- test/support/sample_application.rb
|
81
|
+
- test/test_helper.rb
|
82
|
+
homepage: https://github.com/daneharrigan/respond_with
|
83
|
+
licenses: []
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 1.8.11
|
103
|
+
signing_key:
|
104
|
+
specification_version: 3
|
105
|
+
summary: Make API responses hella simple
|
106
|
+
test_files:
|
107
|
+
- test/all.rb
|
108
|
+
- test/respond_with_test.rb
|
109
|
+
- test/support/item_serializer.rb
|
110
|
+
- test/support/mock_item.rb
|
111
|
+
- test/support/sample_application.rb
|
112
|
+
- test/test_helper.rb
|