api_builder 0.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/api_builder.rb CHANGED
@@ -1,2 +1,3 @@
1
+ require 'api_builder/with_name'
1
2
  require 'api_builder/renderer'
2
3
  require 'api_builder/template'
@@ -1,92 +1,92 @@
1
1
  module ApiBuilder
2
- class ArrayRenderer
3
- def self.render(*args, &block)
4
- renderer = self.new(*args, &block)
5
- renderer.out
2
+ module Renderer
3
+ def id(*args, &block)
4
+ method_missing(:id, *args, &block)
6
5
  end
6
+
7
+ def method_missing(name, *args, &block)
8
+ @_out = {} if @_out.nil?
7
9
 
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
10
  if block_given?
34
- @out = {}
35
- instance_eval(&block)
11
+ out = @_out
12
+ @_out = {}
13
+ block.call
14
+ out[name] = @_out
15
+ @_out = out
36
16
  else
37
- @out = args[0]
17
+ @_out[name] = args[0]
38
18
  end
39
19
  end
40
20
 
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)
21
+ def array(name, value = nil, &block)
22
+ if @_out.nil?
23
+ @_out = ArrayWithName.new(name)
24
+ block.call
25
+ elsif @_out.is_a?(Array)
26
+ out = @_out
27
+ @_out = ArrayWithName.new(name)
28
+ block.call
29
+ out << @_out
30
+ @_out = out
31
+ else # out is a hash
32
+ out = @_out
33
+ @_out = []
34
+ block.call
35
+ out[name] = @_out
36
+ @_out = out
37
+ end
51
38
  end
52
39
 
53
- def method_missing(name, *args, &block)
40
+ def element(name, value = nil, &block)
54
41
  if block_given?
55
- @out.update name => ElementRenderer.render(&block)
42
+ if @_out.nil?
43
+ @_out = HashWithName.new(name)
44
+ block.call
45
+ elsif @_out.is_a?(Array)
46
+ out = @_out
47
+ @_out = HashWithName.new(name)
48
+ block.call
49
+ out << @_out
50
+ @_out = out
51
+ else # out is a hash
52
+ out = @_out
53
+ @_out = {}
54
+ block.call
55
+ out[name] = @_out
56
+ @_out = out
57
+ end
58
+ elsif name.is_a?(Hash)
59
+ if @_out.nil?
60
+ @_out = StringWithName.new(name.keys[0], name.values[0])
61
+ elsif @_out.is_a?(Array)
62
+ @_out << StringWithName.new(name.keys[0], name.values[0])
63
+ else # out is a hash
64
+ @_out[name.keys[0]] = name.values[0]
65
+ end
56
66
  else
57
- @out.update name => args[0]
67
+ if @_out.nil?
68
+ @_out = StringWithName.new(name, value)
69
+ elsif @_out.is_a?(Array)
70
+ @_out << StringWithName.new(name, value)
71
+ else # out is a hash
72
+ @_out[name] = value
73
+ end
58
74
  end
59
75
  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
76
+
77
+ def get_output
78
+ case params[:format].to_sym
79
+ when :json
80
+ if params[:callback]
81
+ "#{params[:callback]}(#{@_out.to_json})"
82
+ else
83
+ @_out.to_json
84
+ end
85
+ when :xml
86
+ @_out.to_xml
71
87
  else
72
- raise "unknown format #{format}"
88
+ raise "unknown format '#{params[:format]}'"
73
89
  end
74
90
  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
91
  end
92
92
  end
@@ -3,12 +3,14 @@ require 'action_view/template'
3
3
 
4
4
  module ActionView
5
5
  module Template::Handlers
6
- class ApiBuilder < Template::Handler
7
- include Compilable
6
+ class ApiBuilder
8
7
 
9
- def compile(template)
10
- "block = lambda { " + template.source + " };" +
11
- "ApiBuilder::BaseRenderer.render(params[:format], &block);"
8
+ def self.call(template)
9
+ "
10
+ extend ApiBuilder::Renderer
11
+ #{template.source}
12
+ get_output
13
+ "
12
14
  end
13
15
  end
14
16
  end
@@ -0,0 +1,46 @@
1
+ require 'builder'
2
+
3
+ module ApiBuilder
4
+ class HashWithName < Hash
5
+ send :attr_accessor, :name
6
+
7
+ def initialize(name)
8
+ @name = name
9
+ end
10
+
11
+ def to_xml(options = {})
12
+ super options.update(:root => name, :skip_types => true, :dasherize => false)
13
+ end
14
+ end
15
+
16
+ class ArrayWithName < Array
17
+ send :attr_accessor, :name
18
+
19
+ def initialize(name)
20
+ @name = name
21
+ end
22
+
23
+ def to_xml(options = {})
24
+ super options.update(:root => name, :skip_types => true, :dasherize => false)
25
+ end
26
+ end
27
+
28
+ class StringWithName < String
29
+ send :attr_accessor, :name
30
+
31
+ def initialize(name, value)
32
+ @name = name
33
+ super value.to_s
34
+ end
35
+
36
+ def to_xml(options = {})
37
+ if options[:builder]
38
+ xml = options[:builder]
39
+ else
40
+ xml = Builder::XmlMarkup.new
41
+ xml.instruct!
42
+ end
43
+ xml.tag! name, to_s
44
+ end
45
+ end
46
+ end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_builder
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 19
5
+ prerelease:
5
6
  segments:
6
- - 0
7
- - 0
8
7
  - 1
9
- version: 0.0.1
8
+ - 0
9
+ - 2
10
+ version: 1.0.2
10
11
  platform: ruby
11
12
  authors:
12
13
  - Lasse Bunk
@@ -14,11 +15,10 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2011-09-11 00:00:00 +02:00
18
- default_executable:
18
+ date: 2011-09-20 00:00:00 Z
19
19
  dependencies: []
20
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.
21
+ description: ApiBuilder is a Ruby on Rails template engine that allows for multiple formats being laid out in a single specification, currently XML and JSON.
22
22
  email: lassebunk@gmail.com
23
23
  executables: []
24
24
 
@@ -27,15 +27,10 @@ extensions: []
27
27
  extra_rdoc_files: []
28
28
 
29
29
  files:
30
- - api_builder.gemspec
31
- - init.rb
32
30
  - lib/api_builder/renderer.rb
33
31
  - lib/api_builder/template.rb
32
+ - lib/api_builder/with_name.rb
34
33
  - lib/api_builder.rb
35
- - MIT-LICENSE
36
- - Rakefile
37
- - README.rdoc
38
- has_rdoc: true
39
34
  homepage: http://github.com/lassebunk/api_builder
40
35
  licenses: []
41
36
 
@@ -45,23 +40,27 @@ rdoc_options: []
45
40
  require_paths:
46
41
  - lib
47
42
  required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
48
44
  requirements:
49
45
  - - ">="
50
46
  - !ruby/object:Gem::Version
47
+ hash: 3
51
48
  segments:
52
49
  - 0
53
50
  version: "0"
54
51
  required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
55
53
  requirements:
56
54
  - - ">="
57
55
  - !ruby/object:Gem::Version
56
+ hash: 3
58
57
  segments:
59
58
  - 0
60
59
  version: "0"
61
60
  requirements: []
62
61
 
63
62
  rubyforge_project:
64
- rubygems_version: 1.3.6
63
+ rubygems_version: 1.8.10
65
64
  signing_key:
66
65
  specification_version: 3
67
66
  summary: Multiple API formats from a single specification.
data/MIT-LICENSE DELETED
@@ -1,20 +0,0 @@
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 DELETED
@@ -1,42 +0,0 @@
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 DELETED
@@ -1,23 +0,0 @@
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
data/api_builder.gemspec DELETED
@@ -1,11 +0,0 @@
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 DELETED
@@ -1 +0,0 @@
1
- require 'api_builder'