grape-erb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 61c271263ad70c27a490f61a1b66a8f3dbaa92ae
4
+ data.tar.gz: 0679c58edfcadfbe1b977ad4901a8f26cdc48b80
5
+ SHA512:
6
+ metadata.gz: ccf0a0e9c335105d927b977803f018012d524b0a6e94b223c5a42827f221583473e11f44bf0a549f21075b2ac898bfe722807edbf34084a1dd17e770ddfca585
7
+ data.tar.gz: d9074b3abbd5fe1a5135ec194beaf9da02c3be632ac0d0313146a087146d3829840b20fad750d0b66ba6ab7678861fec5d413a35a9cf939ccd18be6237bbea84
@@ -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
@@ -0,0 +1,7 @@
1
+ #### Next
2
+
3
+ * Your contribution here.
4
+
5
+ #### v0.0.1
6
+
7
+ * Initial public release. [@Howl](https://github.com/mimosa)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Howl Wong
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.
@@ -0,0 +1,153 @@
1
+ # Grape::Erb
2
+
3
+ Use [Erb](https://github.com/mimosa/erb) templates in [Grape](https://github.com/intridea/grape)!
4
+
5
+ ## Installation
6
+
7
+ Add the `grape` and `grape-erb` gems to Gemfile.
8
+
9
+ ```ruby
10
+ gem 'grape'
11
+ gem 'grape-erb'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ ## Usage
19
+
20
+ ### Setup view root directory
21
+ ```ruby
22
+ # config.ru
23
+ use Rack::Config do |env|
24
+ env['api.tilt.root'] = '/path/to/view/root/directory'
25
+ end
26
+ ```
27
+
28
+ ### Tell your API to use Grape::Formatter::Erb
29
+
30
+ ```ruby
31
+ class API < Grape::API
32
+ content_type :json, 'application/json'
33
+ content_type :html, 'text/html'
34
+ formatter :html, Grape::Formatter::Erb
35
+ format :html
36
+ end
37
+ ```
38
+
39
+ ### Use erb templates conditionally
40
+
41
+ Add the template name to the API options.
42
+
43
+ ```ruby
44
+ get "/user/:id", erb: "users/show" do
45
+ @user = User.find(params[:id])
46
+ end
47
+ ```
48
+
49
+ You can use instance variables in the Erb template.
50
+
51
+ ```ruby
52
+ <% unless @user.nil? %>
53
+ <h2><%= @user.name %></h2>
54
+ <p><%= @user.bio %></p>
55
+ <% end %>
56
+ ```
57
+
58
+ ### Use erb layout
59
+
60
+ Gape-erb first looks for a layout file in `#{env['api.tilt.root']}/layouts/application.html.erb`.
61
+
62
+ You can override the default layout conventions:
63
+
64
+ ```ruby
65
+ # config.ru
66
+ use Rack::Config do |env|
67
+ env['api.tilt.root'] = '/path/to/view/root/directory'
68
+ env['api.tilt.layout'] = 'layouts/another'
69
+ end
70
+ ```
71
+
72
+ ### Enable template caching
73
+
74
+ Grape-erb allows for template caching after templates are loaded initially.
75
+
76
+ You can enable template caching:
77
+
78
+ ```ruby
79
+ # config.ru
80
+ Grape::Erb.configure do |c|
81
+ c.cache_template_loading = true # default: false
82
+ end
83
+ ```
84
+
85
+ ## You can omit .erb
86
+
87
+ The following are identical.
88
+
89
+ ```ruby
90
+ get "/home", erb: "view"
91
+ get "/home", erb: "view.html.erb"
92
+ ```
93
+
94
+ ## Usage with rails
95
+
96
+ Create grape application
97
+
98
+ ```ruby
99
+ # app/api/user.rb
100
+ class MyAPI < Grape::API
101
+ content_type :json, 'application/json'
102
+ content_type :js, 'text/javascript'
103
+ content_type :html, 'text/html'
104
+
105
+ formatter :html, Grape::Formatter::Erb
106
+ formatter :js, Grape::Formatter::Erb
107
+
108
+ format :json # Default format
109
+
110
+ get '/user/:id' do
111
+ env['api.format'] = :html
112
+ content_type 'text/html'
113
+
114
+ @user = User.find(params[:id])
115
+
116
+ render erb: 'users/show'
117
+ end
118
+ end
119
+ ```
120
+
121
+ ```ruby
122
+ # app/views/api/user.html.erb
123
+ <% unless @user.nil? %>
124
+ <h2><%= @user.name %></h2>
125
+ <p><%= @user.bio %></p>
126
+ <% end %>
127
+ ```
128
+
129
+ Edit your **config/application.rb** and add view path
130
+
131
+ ```ruby
132
+ # application.rb
133
+ class Application < Rails::Application
134
+ config.middleware.use(Rack::Config) do |env|
135
+ env['api.tilt.root'] = Rails.root.join "app", "views", "api"
136
+ end
137
+ end
138
+ ```
139
+
140
+ ## Specs
141
+
142
+ See ["Writing Tests"](https://github.com/intridea/grape#writing-tests) in [https://github.com/intridea/grape](grape) README.
143
+
144
+ Enjoy :)
145
+
146
+
147
+ ## Contributing
148
+
149
+ 1. Fork it
150
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
151
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
152
+ 4. Push to the branch (`git push origin my-new-feature`)
153
+ 5. Create new Pull Request
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/grape-erb/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Howl王"]
6
+ gem.email = ["howl.wong@gmail.com"]
7
+ gem.description = %q{Use erb in grape}
8
+ gem.summary = %q{Use erb in grape}
9
+ gem.homepage = "https://github.com/mimosa/grape-erb"
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.name = "grape-erb"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = Grape::Erb::VERSION
16
+ gem.required_ruby_version = '>= 1.9.3'
17
+
18
+ gem.add_dependency "grape"
19
+ gem.add_dependency "erubis"
20
+ gem.add_dependency "tilt"
21
+ gem.add_dependency "i18n"
22
+ end
@@ -0,0 +1,6 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'grape'
3
+ require 'grape/erb'
4
+ require 'grape-erb/version'
5
+ require 'grape-erb/formatter'
6
+ require 'grape-erb/renderer'
@@ -0,0 +1,62 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Grape
4
+ module Erb
5
+ class Formatter
6
+ attr_reader :env, :endpoint, :object, :options
7
+
8
+ def initialize(object, env)
9
+ @env = env
10
+ @endpoint = env['api.endpoint']
11
+ @object = object
12
+
13
+ @options = {
14
+ erb: env['api.tilt.erb'],
15
+ format: env['api.format'],
16
+ locals: env['api.tilt.erb.locals'],
17
+ view_path: env['api.tilt.root']
18
+ }
19
+ end
20
+
21
+ def call(opts={})
22
+ @options.merge!(opts)
23
+ if template?
24
+ output = erb_template(template).render(endpoint, locals)
25
+ layout = layout_name
26
+ return layout.nil? ? output : erb_template(layout).render(endpoint, locals) { output }
27
+ else
28
+ fail 'missing erb template'
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def view_path(view_name)
35
+ view_name += ".#{@options[:format]}.erb" unless view_name =~ /.erb$/
36
+ File.join(@options[:view_path], view_name)
37
+ end
38
+
39
+ def template?
40
+ !!template
41
+ end
42
+
43
+ def locals
44
+ @options[:locals] || endpoint.options[:route_options][:locals] || {}
45
+ end
46
+
47
+ def template
48
+ @options[:erb] || endpoint.options[:route_options][:erb]
49
+ end
50
+
51
+ def erb_template(view_name)
52
+ _path = view_path(view_name)
53
+ ::Tilt::ErubisTemplate.new(_path, @options)
54
+ end
55
+
56
+ def layout_name
57
+ view_name = (env['api.tilt.layout'] || 'layouts/application')
58
+ view_name if File.exist?( view_path(view_name) )
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Grape
4
+ module Erb
5
+ module Renderer
6
+ #
7
+ JS_ESCAPE_MAP = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }
8
+ #
9
+ def render(opts = {})
10
+ Grape::Erb::Formatter.new(nil, env).call(opts)
11
+ end
12
+ #
13
+ def escape_javascript(javascript)
14
+ result = ''
15
+ if javascript
16
+ result += javascript.gsub(/(\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) {|match| JS_ESCAPE_MAP[match] }
17
+ end
18
+ result
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ Grape::Endpoint.send(:include, Grape::Erb::Renderer)
@@ -0,0 +1,7 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Grape
4
+ module Erb
5
+ VERSION = '0.0.1'
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'grape-erb'
3
+
4
+ module Grape
5
+ module Formatter
6
+ module Erb
7
+ class << self
8
+ def call(object, env)
9
+ fail "Use Rack::Config to set 'api.tilt.root' in config.ru" unless env['api.tilt.root']
10
+ Grape::Erb::Formatter.new(object, env).call
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape-erb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Howl王
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: grape
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: erubis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: tilt
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: i18n
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Use erb in grape
70
+ email:
71
+ - howl.wong@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - CHANGELOG.md
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - grape-erb.gemspec
82
+ - lib/grape-erb.rb
83
+ - lib/grape-erb/formatter.rb
84
+ - lib/grape-erb/renderer.rb
85
+ - lib/grape-erb/version.rb
86
+ - lib/grape/erb.rb
87
+ homepage: https://github.com/mimosa/grape-erb
88
+ licenses: []
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 1.9.3
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.5.0
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Use erb in grape
110
+ test_files: []