grape-jbuilder 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=documentation
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - ruby-head
6
+ - rbx-19mode
7
+
8
+ env:
9
+ - JRUBY_OPTS="--1.9"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in grape-jbuilder.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2012 Piotr Niełacny
2
+ Copyright (c) 2013 Shu Masuda
3
+
4
+ MIT License
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,173 @@
1
+ # Grape::Jbuilder
2
+
3
+ Use [Jbuilder](https://github.com/rails/jbuilder) templates in [Grape](https://github.com/intridea/grape)!
4
+
5
+ This gem is completely based on [grape-rabl](https://github.com/LTe/grape-rabl).
6
+
7
+ [![Build Status](https://travis-ci.org/milkcocoa/grape-jbuilder.png?branch=master)](http://travis-ci.org/milkcocoa/grape-jbuilder)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'grape-jbuilder'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install grape-jbuilder
22
+
23
+ ## Usage
24
+
25
+ ### Require grape-jbuilder
26
+
27
+ ```ruby
28
+ # config.ru
29
+ require 'grape/jbuilder'
30
+ ```
31
+
32
+ ### Setup view root directory
33
+ ```ruby
34
+ # config.ru
35
+ require 'grape/jbuilder'
36
+
37
+ use Rack::Config do |env|
38
+ env['api.tilt.root'] = '/path/to/view/root/directory'
39
+ end
40
+ ```
41
+
42
+ ### Tell your API to use Grape::Formatter:: Jbuilder
43
+
44
+ ```ruby
45
+ class API < Grape::API
46
+ format :json
47
+ formatter :json, Grape::Formatter::Jbuilder
48
+ end
49
+ ```
50
+
51
+ ### Use Jbuilder templates conditionally
52
+
53
+ Add the template name to the API options.
54
+
55
+ ```ruby
56
+ get '/user/:id', :jbuilder => 'user.jbuilder' do
57
+ @user = User.find(params[:id])
58
+ end
59
+ ```
60
+
61
+ You can use instance variables in the Jbuilder template.
62
+
63
+ ```ruby
64
+ json.user do
65
+ json.(@user, :name, :email)
66
+ json.project do
67
+ json.(@project, :name)
68
+ end
69
+ end
70
+ ```
71
+
72
+ ## You can omit .jbuilder
73
+
74
+ The following are identical.
75
+
76
+ ```ruby
77
+ get '/home', jbuilder: 'view'
78
+ get '/home', jbuilder: 'view.jbuilder'
79
+ ```
80
+
81
+ ### Example
82
+
83
+ ```ruby
84
+ # config.ru
85
+ require 'grape/jbuilder'
86
+
87
+ use Rack::Config do |env|
88
+ env['api.tilt.root'] = '/path/to/view/root/directory'
89
+ end
90
+
91
+ class UserAPI < Grape::API
92
+ format :json
93
+ formatter :json, Grape::Formatter::Jbuilder
94
+
95
+ # use Jbuilder with 'user.jbuilder' template
96
+ get '/user/:id', jbuilder: 'user' do
97
+ @user = User.find(params[:id])
98
+ end
99
+
100
+ # do not use jbuilder, fallback to the defalt Grape JSON formatter
101
+ get '/users' do
102
+ User.all
103
+ end
104
+ end
105
+ ```
106
+
107
+ ```ruby
108
+ # user.jbuilder
109
+ json.user do
110
+ json.(@user, :name)
111
+ end
112
+ ```
113
+
114
+ ## Usage with Rails
115
+
116
+ Create a grape application.
117
+
118
+ ```ruby
119
+ # app/api/user.rb
120
+ class MyAPI < Grape::API
121
+ format :json
122
+ formatter :json, Grape::Formatter::Jbuilder
123
+ get '/user/:id', jbuilder: 'user' do
124
+ @user = User.find(params[:id])
125
+ end
126
+ end
127
+ ```
128
+
129
+ ```ruby
130
+ # app/views/api/user.jbuilder
131
+ json.user do
132
+ json.(@user, :name)
133
+ end
134
+ ```
135
+
136
+ Edit your **config/application.rb** and add view path
137
+
138
+ ```ruby
139
+ # application.rb
140
+ class Application < Rails::Application
141
+ config.middleware.use(Rack::Config) do |env|
142
+ env['api.tilt.root'] = Rails.root.join 'app', 'views', 'api'
143
+ end
144
+ end
145
+ ```
146
+
147
+ Mount application to Rails router
148
+
149
+ ```ruby
150
+ # routes.rb
151
+ GrapeExampleRails::Application.routes.draw do
152
+ mount MyAPI , at: '/api'
153
+ end
154
+ ```
155
+
156
+ ## Rspec
157
+
158
+ See "Writing Tests" in https://github.com/intridea/grape.
159
+
160
+ Enjoy :)
161
+
162
+
163
+ ## Special Thanks
164
+
165
+ Special thanks to [@LTe](https://github.com/LTe) because this gem is completely based on [grape-rabl](https://github.com/LTe/grape-rabl).
166
+
167
+ ## Contributing
168
+
169
+ 1. Fork it
170
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
171
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
172
+ 4. Push to the branch (`git push origin my-new-feature`)
173
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.rspec_opts = ['-fd -c']
7
+ spec.pattern = FileList['spec/**/*_spec.rb']
8
+ end
9
+
10
+ task default: :spec
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'grape/jbuilder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "grape-jbuilder"
8
+ spec.version = Grape::Jbuilder::VERSION
9
+ spec.authors = ["Shu Masuda"]
10
+ spec.email = ["masushu@gmail.com"]
11
+ spec.description = %q{Use Jbuilder in Grape}
12
+ spec.summary = %q{Use Jbuilder in Grape}
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_dependency "grape", ">= 0.3"
22
+ spec.add_dependency "jbuilder"
23
+ spec.add_dependency "tilt"
24
+ spec.add_dependency "tilt-jbuilder", ">= 0.4.0"
25
+ spec.add_dependency "i18n"
26
+
27
+ spec.add_development_dependency "bundler"
28
+ spec.add_development_dependency "json_expressions"
29
+ spec.add_development_dependency "rack-test"
30
+ spec.add_development_dependency "rake"
31
+ spec.add_development_dependency "rspec"
32
+ end
@@ -0,0 +1,7 @@
1
+ require 'grape'
2
+ require 'hashie/hash'
3
+ require 'jbuilder'
4
+ require 'tilt'
5
+ require 'tilt/jbuilder.rb'
6
+ require 'grape/jbuilder/formatter'
7
+ require 'grape/jbuilder/version'
@@ -0,0 +1,50 @@
1
+ module Grape
2
+ module Formatter
3
+ module Jbuilder
4
+ class << self
5
+
6
+ attr_reader :env
7
+ attr_reader :endpoint
8
+
9
+ def call(object, env)
10
+ @env = env
11
+ @endpoint = env['api.endpoint']
12
+
13
+ if jbuilderable?
14
+ jbuilder do |template|
15
+ engine = ::Tilt.new(view_path(template), nil, view_path: env['api.tilt.root'])
16
+ engine.render(endpoint, {})
17
+ end
18
+ else
19
+ Grape::Formatter::Json.call object, env
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def view_path(template)
26
+ if template.split('.')[-1] == 'jbuilder'
27
+ File.join(env['api.tilt.root'], template)
28
+ else
29
+ File.join(env['api.tilt.root'], (template + '.jbuilder'))
30
+ end
31
+ end
32
+
33
+ def jbuilderable?
34
+ !! endpoint.options[:route_options][:jbuilder]
35
+ end
36
+
37
+ def jbuilder
38
+ template = endpoint.options[:route_options][:jbuilder]
39
+ raise 'missing jbuilder template' unless template
40
+ set_view_root unless env['api.tilt.root']
41
+ yield template
42
+ end
43
+
44
+ def set_view_root
45
+ raise "Use Rack::Config to set 'api.tilt.root' in config.ru"
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ module Grape
2
+ module Jbuilder
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Grape::Jbuilder partials' do
4
+ subject do
5
+ Class.new(Grape::API)
6
+ end
7
+
8
+ before do
9
+ subject.format :json
10
+ subject.formatter :json, Grape::Formatter::Jbuilder
11
+ subject.before { env['api.tilt.root'] = "#{File.dirname(__FILE__)}/views" }
12
+ end
13
+
14
+ def app
15
+ subject
16
+ end
17
+
18
+ it 'proper render partials' do
19
+ subject.get('/home', jbuilder: 'project') do
20
+ @author = OpenStruct.new(author: 'LTe')
21
+ @type = OpenStruct.new(type: 'paper')
22
+ @project = OpenStruct.new(name: 'First', type: @type, author: @author)
23
+ end
24
+
25
+ pattern = {
26
+ project: {
27
+ name: 'First',
28
+ info: {
29
+ type: 'paper'
30
+ },
31
+ author: {
32
+ author: 'LTe'
33
+ }
34
+ }
35
+ }
36
+
37
+ get('/home')
38
+ last_response.body.should match_json_expression(pattern)
39
+ last_response.body.should ==
40
+ "{\"project\":{\"name\":\"First\",\"info\":{\"type\":\"paper\"},\"author\":{\"author\":\"LTe\"}}}"
41
+ end
42
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe Grape::Jbuilder do
4
+ subject do
5
+ Class.new(Grape::API)
6
+ end
7
+
8
+ before do
9
+ subject.format :json
10
+ subject.formatter :json, Grape::Formatter::Jbuilder
11
+ end
12
+
13
+ def app
14
+ subject
15
+ end
16
+
17
+ it 'should work without jbuilder template' do
18
+ subject.get('/home') { 'Hello World' }
19
+ get '/home'
20
+ last_response.body.should == 'Hello World'
21
+ end
22
+
23
+ it 'should raise error about root directory' do
24
+ subject.get('/home', jbuilder: true){}
25
+ get '/home'
26
+ last_response.status.should == 500
27
+ last_response.body.should include "Use Rack::Config to set 'api.tilt.root' in config.ru"
28
+ end
29
+
30
+
31
+ context 'titl root is setup' do
32
+ before do
33
+ subject.before { env['api.tilt.root'] = "#{File.dirname(__FILE__)}/views" }
34
+ end
35
+
36
+ it 'should respond with proper content-type' do
37
+ subject.get('/home', jbuilder: 'user'){}
38
+ get('/home')
39
+ last_response.headers['Content-Type'].should == 'application/json'
40
+ end
41
+
42
+ it 'should not raise error about root directory' do
43
+ subject.get('/home', jbuilder: true){}
44
+ get '/home'
45
+ last_response.status.should == 500
46
+ last_response.body.should_not include "Use Rack::Config to set 'api.tilt.root' in config.ru"
47
+ end
48
+
49
+ ['user', 'user.jbuilder'].each do |jbuilder_option|
50
+ it 'should render jbuilder template (#{jbuilder_option})' do
51
+ subject.get('/home', jbuilder: jbuilder_option) do
52
+ @user = OpenStruct.new(name: 'LTe', email: 'email@example.com')
53
+ @project = OpenStruct.new(name: 'First')
54
+ end
55
+
56
+ pattern = {
57
+ user: {
58
+ name: 'LTe',
59
+ email: 'email@example.com',
60
+ project: {
61
+ name: 'First'
62
+ }
63
+ }
64
+ }
65
+
66
+ get '/home'
67
+ last_response.body.should match_json_expression(pattern)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'bundler'
5
+ Bundler.setup :default, :test
6
+
7
+ require 'active_support/core_ext/hash/conversions'
8
+ require 'grape/jbuilder'
9
+ require 'json_expressions/rspec'
10
+ require 'ostruct'
11
+ require 'rack/test'
12
+ require 'rspec'
13
+
14
+ RSpec.configure do |config|
15
+ config.include Rack::Test::Methods
16
+ end
17
+
18
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
@@ -0,0 +1 @@
1
+ json.(type, :type)
@@ -0,0 +1,9 @@
1
+ json.project do
2
+ json.(@project, :name)
3
+
4
+ json.info do
5
+ json.partial! 'partial', type: @type
6
+ end
7
+
8
+ json.author(@author, :author)
9
+ end
@@ -0,0 +1,7 @@
1
+ json.user do
2
+ json.(@user, :name, :email)
3
+
4
+ json.project do
5
+ json.(@project, :name)
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,229 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape-jbuilder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Shu Masuda
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: grape
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0.3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: jbuilder
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
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: tilt
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
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: tilt-jbuilder
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 0.4.0
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 0.4.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: i18n
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: bundler
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: json_expressions
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: rack-test
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: rake
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ - !ruby/object:Gem::Dependency
159
+ name: rspec
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ description: Use Jbuilder in Grape
175
+ email:
176
+ - masushu@gmail.com
177
+ executables: []
178
+ extensions: []
179
+ extra_rdoc_files: []
180
+ files:
181
+ - .gitignore
182
+ - .rspec
183
+ - .travis.yml
184
+ - Gemfile
185
+ - LICENSE.txt
186
+ - README.md
187
+ - Rakefile
188
+ - grape-jbuilder.gemspec
189
+ - lib/grape/jbuilder.rb
190
+ - lib/grape/jbuilder/formatter.rb
191
+ - lib/grape/jbuilder/version.rb
192
+ - spec/grape_jbuilder_partials_spec.rb
193
+ - spec/grape_jbuilder_spec.rb
194
+ - spec/spec_helper.rb
195
+ - spec/views/_partial.jbuilder
196
+ - spec/views/project.jbuilder
197
+ - spec/views/user.jbuilder
198
+ homepage: ''
199
+ licenses:
200
+ - MIT
201
+ post_install_message:
202
+ rdoc_options: []
203
+ require_paths:
204
+ - lib
205
+ required_ruby_version: !ruby/object:Gem::Requirement
206
+ none: false
207
+ requirements:
208
+ - - '>='
209
+ - !ruby/object:Gem::Version
210
+ version: '0'
211
+ required_rubygems_version: !ruby/object:Gem::Requirement
212
+ none: false
213
+ requirements:
214
+ - - '>='
215
+ - !ruby/object:Gem::Version
216
+ version: '0'
217
+ requirements: []
218
+ rubyforge_project:
219
+ rubygems_version: 1.8.25
220
+ signing_key:
221
+ specification_version: 3
222
+ summary: Use Jbuilder in Grape
223
+ test_files:
224
+ - spec/grape_jbuilder_partials_spec.rb
225
+ - spec/grape_jbuilder_spec.rb
226
+ - spec/spec_helper.rb
227
+ - spec/views/_partial.jbuilder
228
+ - spec/views/project.jbuilder
229
+ - spec/views/user.jbuilder