grape-rabl 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 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,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in grape-rabl.gemspec
4
+ gemspec
5
+
6
+ gem "grape", :git => "git://github.com/intridea/grape.git", :branch => "frontier"
7
+
8
+ group :test do
9
+ gem "rspec", "~> 2.8.0"
10
+ gem "rack-test"
11
+ gem "rake"
12
+ gem "ruby-debug19"
13
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Piotr Niełacny
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,95 @@
1
+ # Grape::Rabl
2
+
3
+ Use rabl templates in grape!
4
+
5
+ [![Build Status](http://travis-ci.org/LTe/grape-rabl.png)](http://github.com/LTe/grape-rabl)
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'grape-rabl'
14
+ gem 'grape', :git => "git://github.com/intridea/grape.git", :branch => "frontier"
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ ## Usage
22
+
23
+ ### Require grape-rabl
24
+ ```ruby
25
+ # config.ru
26
+ require 'grape-rabl'
27
+ ```
28
+
29
+ ### Setup view root directory
30
+ ```ruby
31
+ # config.ru
32
+ require 'grape-rabl'
33
+
34
+ use Rack::Config do |env|
35
+ env['api.tilt.root'] = '/path/to/view/root/directory'
36
+ end
37
+ ```
38
+
39
+ ### Create grape application
40
+
41
+ To *get post put delete options* add **:rabl** options with template name.
42
+
43
+ ```ruby
44
+ get "/path", :rabl => "template_name.rabl" do
45
+ # stuff
46
+ @var = "hello"
47
+ end
48
+
49
+ post "/path", :rabl => "template_name_diff.rabl" do
50
+ # stuff
51
+ @user = User.find_user("email@example.com")
52
+ end
53
+ ```
54
+
55
+ **You can use instance variables in templates!**
56
+
57
+ ### Example
58
+
59
+ ```ruby
60
+ # config.ru
61
+ require 'grape-rabl'
62
+
63
+ use Rack::Config do |env|
64
+ env['api.tilt.root'] = '/path/to/view/root/directory'
65
+ end
66
+
67
+ class UserAPI < Grape::API
68
+ # use rabl with 'hello.rabl' template
69
+ get '/user', :rabl => 'hello.rabl' do
70
+ @user = User.first
71
+ end
72
+
73
+ # do not use rabl, normal usage
74
+ get '/user2' do
75
+ { :some => :hash }
76
+ end
77
+ end
78
+ ```
79
+
80
+ ```ruby
81
+ # hello.rabl
82
+ object @user => :user
83
+
84
+ attributes :name
85
+ ```
86
+
87
+ Enjoy :)
88
+
89
+ ## Contributing
90
+
91
+ 1. Fork it
92
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
93
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
94
+ 4. Push to the branch (`git push origin my-new-feature`)
95
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.rspec_opts = ['-fd -c']
8
+ spec.pattern = FileList['spec/**/*_spec.rb']
9
+ end
10
+
11
+ task :default => :spec
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/grape-rabl/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Piotr Niełacny"]
6
+ gem.email = ["piotr.nielacny@gmail.com"]
7
+ gem.description = %q{Use rabl in grape}
8
+ gem.summary = %q{Use rabl in grape}
9
+ gem.homepage = ""
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 = "grape-rabl"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Grape::Rabl::VERSION
17
+
18
+ gem.add_dependency "rabl"
19
+ gem.add_dependency "tilt"
20
+ gem.add_dependency "i18n"
21
+ end
@@ -0,0 +1,42 @@
1
+ require 'tilt'
2
+ Rabl.register!
3
+
4
+ module Grape
5
+ module Middleware
6
+ class Formatter
7
+ alias :old_after :after
8
+
9
+ def after
10
+ status, headers, bodies = *@app_response
11
+ current_endpoint = env['api.endpoint']
12
+
13
+ rabl(current_endpoint) do |template|
14
+ engine = ::Tilt.new(File.join(env['api.tilt.root'], template))
15
+ rendered = engine.render(current_endpoint, {})
16
+ Rack::Response.new(rendered, status, headers).to_a
17
+ end
18
+ end
19
+
20
+ def rabl(endpoint)
21
+ if template = rablable?(endpoint)
22
+ yield template
23
+ else
24
+ old_after
25
+ end
26
+ end
27
+
28
+ def rablable?(endpoint)
29
+ if template = endpoint.options[:route_options][:rabl]
30
+ set_view_root unless env['api.tilt.root']
31
+ template
32
+ else
33
+ false
34
+ end
35
+ end
36
+
37
+ def set_view_root
38
+ raise "Use Rack::Config to set 'api.tilt.root' in config.ru"
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ module Grape
2
+ module Rabl
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/lib/grape-rabl.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "rabl"
2
+ require "grape"
3
+ require "grape-rabl/version"
4
+ require "grape-rabl/formatter"
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe Grape::Rabl do
4
+ subject { Class.new(Grape::API) }
5
+ before { subject.default_format :json }
6
+ def app; subject end
7
+
8
+ it 'should work without rabl template' do
9
+ subject.get("/home") {"Hello World"}
10
+ get "/home"
11
+ last_response.body.should == "Hello World"
12
+ end
13
+
14
+ it "should raise error about root directory" do
15
+ subject.get("/home", :rabl => true){}
16
+ lambda{ get "/home" }.should raise_error("Use Rack::Config to set 'api.tilt.root' in config.ru")
17
+ end
18
+
19
+ context "titl root is setup" do
20
+ before do
21
+ subject.before { env["api.tilt.root"] = "#{File.dirname(__FILE__)}/views" }
22
+ end
23
+
24
+ it "should not raise error about root directory" do
25
+ subject.get("/home", :rabl => true){}
26
+ lambda{ get "/home" }.should_not raise_error("Use Rack::Config to set 'api.tilt.root' in config.ru")
27
+ end
28
+
29
+ it "should render rabl template" do
30
+ subject.get("/home", :rabl => "user.rabl") do
31
+ @user = OpenStruct.new(:name => "LTe", :email => "email@example.com")
32
+ @project = OpenStruct.new(:name => "First")
33
+ end
34
+
35
+ get "/home"
36
+ last_response.body.should == '{"user":{"name":"LTe","email":"email@example.com","project":{"name":"First"}}}'
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,17 @@
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 'grape-rabl'
8
+ require 'rspec'
9
+ require 'rack/test'
10
+ require 'ostruct'
11
+
12
+ RSpec.configure do |config|
13
+ config.include Rack::Test::Methods
14
+ end
15
+
16
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
17
+
@@ -0,0 +1,6 @@
1
+ object @user => :user
2
+ attributes :name, :email
3
+
4
+ child @project => :project do
5
+ attributes :name
6
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape-rabl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Piotr Niełacny
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rabl
16
+ requirement: &17866020 !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: *17866020
25
+ - !ruby/object:Gem::Dependency
26
+ name: tilt
27
+ requirement: &17863200 !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: *17863200
36
+ - !ruby/object:Gem::Dependency
37
+ name: i18n
38
+ requirement: &17874680 !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: *17874680
47
+ description: Use rabl in grape
48
+ email:
49
+ - piotr.nielacny@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - grape-rabl.gemspec
60
+ - lib/grape-rabl.rb
61
+ - lib/grape-rabl/formatter.rb
62
+ - lib/grape-rabl/version.rb
63
+ - spec/grape_rabl_spec.rb
64
+ - spec/spec_helper.rb
65
+ - spec/views/user.rabl
66
+ homepage: ''
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.10
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Use rabl in grape
90
+ test_files: []
91
+ has_rdoc: