api_builder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Lasse Bunk
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,42 @@
1
+ == About
2
+
3
+ ApiBuilder is a Ruby on Rails template engine that allows for multiple formats being laid out in one specification, currently XML and JSON.
4
+ The template engine currently returns a hash that is converted to either JSON or XML, JSON being the only usable format right now.
5
+
6
+ == Installation
7
+
8
+ $ rails plugin install git://github.com/lassebunk/api_builder.git
9
+
10
+ == Examples
11
+
12
+ In app/views/users/index.apibuilder:
13
+
14
+ array do
15
+ @users.each do |user|
16
+ element do
17
+ id @user.id
18
+ name @user.name
19
+ end
20
+ end
21
+ end
22
+
23
+ In app/views/users/show.apibuilder:
24
+
25
+ element do
26
+ id @user.id
27
+ name @user.name
28
+ address do
29
+ street @user.street
30
+ city @user.city
31
+ end
32
+ array :interests do
33
+ @user.interests.each do |interest|
34
+ element interest.name
35
+ end
36
+ end
37
+ end
38
+
39
+ More examples to come. This is the first version which will be expanded as I use this myself.
40
+ Inputs are welcome at lassebunk@gmail.com.
41
+
42
+ Copyright (c) 2011 Lasse Bunk, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env rake
2
+ require 'rake/testtask'
3
+ require 'rdoc/task'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the api_builder plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the api_builder plugin.'
17
+ RDoc::Task.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'ApiBuilder'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1,11 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "api_builder"
3
+ s.version = "0.0.1"
4
+
5
+ s.author = "Lasse Bunk"
6
+ s.email = "lassebunk@gmail.com"
7
+ s.description = "ApiBuilder is a Ruby on Rails template engine that allows for multiple formats being laid out in one specification, currently XML and JSON."
8
+ s.summary = "Multiple API formats from a single specification."
9
+ s.homepage = "http://github.com/lassebunk/api_builder"
10
+ s.files = Dir['**/*']
11
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'api_builder'
@@ -0,0 +1,92 @@
1
+ module ApiBuilder
2
+ class ArrayRenderer
3
+ def self.render(*args, &block)
4
+ renderer = self.new(*args, &block)
5
+ renderer.out
6
+ end
7
+
8
+ attr_reader :out
9
+
10
+ def initialize(*args, &block)
11
+ @out = []
12
+ instance_eval(&block)
13
+ end
14
+
15
+ def element(*args, &block)
16
+ @out << ElementRenderer.render(*args, &block)
17
+ end
18
+
19
+ def array(*args, &block)
20
+ @out << ArrayRenderer.render(*args, &block)
21
+ end
22
+ end
23
+
24
+ class ElementRenderer
25
+ def self.render(*args, &block)
26
+ renderer = self.new(*args, &block)
27
+ renderer.out
28
+ end
29
+
30
+ attr_reader :out
31
+
32
+ def initialize(*args, &block)
33
+ if block_given?
34
+ @out = {}
35
+ instance_eval(&block)
36
+ else
37
+ @out = args[0]
38
+ end
39
+ end
40
+
41
+ def id(value)
42
+ @out.update :id => value
43
+ end
44
+
45
+ def property(hash)
46
+ @out.update hash
47
+ end
48
+
49
+ def array(name, &block)
50
+ @out.update name => ArrayRenderer.render(&block)
51
+ end
52
+
53
+ def method_missing(name, *args, &block)
54
+ if block_given?
55
+ @out.update name => ElementRenderer.render(&block)
56
+ else
57
+ @out.update name => args[0]
58
+ end
59
+ end
60
+ end
61
+
62
+ class BaseRenderer
63
+ def self.render(format = "json", &block)
64
+ renderer = self.new(&block)
65
+
66
+ case format
67
+ when "json"
68
+ renderer.out.to_json
69
+ when "xml"
70
+ renderer.out.to_xml
71
+ else
72
+ raise "unknown format #{format}"
73
+ end
74
+ end
75
+
76
+ attr_reader :out
77
+
78
+ def initialize(*args, &block)
79
+ instance_eval(&block)
80
+ end
81
+
82
+ def element(*args, &block)
83
+ raise "root element already defined" unless @out.nil?
84
+ @out = ElementRenderer.render(*args, &block)
85
+ end
86
+
87
+ def array(&block)
88
+ raise "root element already defined" unless @out.nil?
89
+ @out = ArrayRenderer.render(&block)
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,17 @@
1
+ require 'action_view/base'
2
+ require 'action_view/template'
3
+
4
+ module ActionView
5
+ module Template::Handlers
6
+ class ApiBuilder < Template::Handler
7
+ include Compilable
8
+
9
+ def compile(template)
10
+ "block = lambda { " + template.source + " };" +
11
+ "ApiBuilder::BaseRenderer.render(params[:format], &block);"
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ ActionView::Template.register_template_handler :apibuilder, ActionView::Template::Handlers::ApiBuilder
@@ -0,0 +1,2 @@
1
+ require 'api_builder/renderer'
2
+ require 'api_builder/template'
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api_builder
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Lasse Bunk
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-09-11 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: ApiBuilder is a Ruby on Rails template engine that allows for multiple formats being laid out in one specification, currently XML and JSON.
22
+ email: lassebunk@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - api_builder.gemspec
31
+ - init.rb
32
+ - lib/api_builder/renderer.rb
33
+ - lib/api_builder/template.rb
34
+ - lib/api_builder.rb
35
+ - MIT-LICENSE
36
+ - Rakefile
37
+ - README.rdoc
38
+ has_rdoc: true
39
+ homepage: http://github.com/lassebunk/api_builder
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.6
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Multiple API formats from a single specification.
68
+ test_files: []
69
+