porth 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in porbt.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011 Tate Johnson <tate@tatey.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,133 @@
1
+ # Porth (Plain Old Ruby Template Handler)
2
+
3
+ Write your views using plain old Ruby. Views are for representation, not defining
4
+ `#as_json` in a model. There's no need to learn a DSL for building arrays and hashes.
5
+ Just use Ruby. Views are written once and rendered in JSON(P) or XML based on
6
+ the requested format. Porth makes few assumptions and can be configured.
7
+
8
+ ## Installation
9
+
10
+ Add this to your project's Gemfile and run `$ bundle install`
11
+
12
+ ``` ruby
13
+ gem 'porth'
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ Create a controller that responds to JSON, XML or both.
19
+
20
+ ``` ruby
21
+ # app/controllers/transmitters_controller.rb
22
+ class TransmittersController < ApplicationController
23
+ respond_to :json, :xml
24
+
25
+ def index
26
+ @transmitters = Transmitter.all
27
+ respond_with @transmitters
28
+ end
29
+
30
+ # ...
31
+ end
32
+ ````
33
+
34
+ Create a template with a `.rb` extension. Write plain old Ruby. Objects
35
+ must respond to `#to_json` or `#to_xml`. Arrays and hashes are best.
36
+
37
+ ``` ruby
38
+ # app/views/transmitters/index.rb
39
+ @transmitters.map do |t|
40
+ {
41
+ name: t.area_served,
42
+ latitude: t.latitude,
43
+ longitude: t.longitude,
44
+ nearby: t.nearby.size
45
+ }
46
+ end
47
+ ```
48
+
49
+ GET /transmitters.json
50
+
51
+ ``` javascript
52
+ [{"name":"Brisbane","latitude":-27.4661111111111,"longitude":152.946388888889,"nearby":11}]
53
+ ```
54
+
55
+ GET /transmitters.json?callback=myFunction
56
+
57
+ ``` javascript
58
+ myFunction([{"name":"Brisbane","latitude":-27.4661111111111,"longitude":152.946388888889,"nearby":11}])
59
+ ```
60
+
61
+ GET /transmitters.xml
62
+
63
+ ``` xml
64
+ <?xml version="1.0" encoding="UTF-8"?>
65
+ <transmitters type="array">
66
+ <transmitter>
67
+ <name>Brisbane</name>
68
+ <latitude type="float">-27.4661111111111</latitude>
69
+ <longitude type="float">152.946388888889</longitude>
70
+ <nearby type="integer">11</nearby>
71
+ </transmitter>
72
+ </transmitters>
73
+ ```
74
+
75
+ `Porth::UnknownFormatError` is raised when the requested format is not supported.
76
+
77
+ ### JSONP
78
+
79
+ Porth calls `#json_callback` to get the function name for JSONP responses. By default
80
+ this method is added to `ActionController::Base` and returns `params[:callback]`. Override
81
+ `#json_callback` to get different behaviour.
82
+
83
+ ``` ruby
84
+ class ApplicationController < ActionController::Base
85
+ # ...
86
+
87
+ protected
88
+
89
+ def json_callback
90
+ nil # Ignore JSONP requests
91
+ end
92
+ end
93
+ ```
94
+
95
+ ### XML
96
+
97
+ Porth guesses the resource's name from the controller's class. `Foo::BarsController`
98
+ becomes `bars`. Override `#xml_root` to explicitly set the resource name.
99
+
100
+ ``` ruby
101
+ class TransmittersController < ApplicationController
102
+ # ...
103
+
104
+ protected
105
+
106
+ def xml_root
107
+ 'sites'
108
+ end
109
+ end
110
+ ```
111
+
112
+ ## Compatibility
113
+
114
+ * ActionPack 3.1.0
115
+ * JRuby 1.6.4
116
+ * Ruby 1.9.2
117
+
118
+ ## Contributing
119
+
120
+ 1. Fork
121
+ 2. Install dependancies by running `$ bundle install`
122
+ 3. Write tests and code
123
+ 4. Make sure the tests pass by running `$ rake`
124
+ 5. Push and send a pull request on GitHub
125
+
126
+ ## Credits
127
+
128
+ Porth is the result of numerous discussions at [Everyday Hero](http://www.everydayhero.com.au)
129
+ around better API design.
130
+
131
+ ## Copyright
132
+
133
+ Copyright © 2011 Tate Johnson. Released under the MIT license. See LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'bundler'
3
+ rescue LoadError
4
+ raise "Could not load the bundler gem. Install it with `gem install bundler`."
5
+ end
6
+
7
+ begin
8
+ Bundler.setup
9
+ rescue Bundler::GemNotFound
10
+ raise RuntimeError, "Bundler couldn't find some gems. Did you run `bundle install`?"
11
+ end
12
+
13
+ require 'bundler/gem_tasks'
14
+ require 'rake/testtask'
15
+
16
+ Rake::TestTask.new :test do |test|
17
+ test.libs << 'test'
18
+ test.pattern = 'test/**/*_test.rb'
19
+ end
20
+
21
+ task :default => :test
data/lib/porth.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'action_controller'
2
+ require 'json'
3
+
4
+ require 'porth/format/json'
5
+ require 'porth/format/xml'
6
+ require 'porth/handler'
7
+ require 'porth/unknown_format_error'
8
+
9
+ Porth::Handler.register_format :json, Porth::Format::JSON
10
+ Porth::Handler.register_format :xml, Porth::Format::XML
11
+
12
+ ActionView::Template.register_template_handler :rb, Porth::Handler
@@ -0,0 +1,19 @@
1
+ module Porth
2
+ module Format
3
+ module JSON
4
+ def self.call controller, object
5
+ json = object.to_json
6
+ callback = controller.send :json_callback
7
+ callback ? "#{callback}(#{json})" : json
8
+ end
9
+
10
+ module ControllerExtensions
11
+ protected
12
+
13
+ def json_callback
14
+ params[:callback]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ module Porth
2
+ module Format
3
+ module XML
4
+ def self.call controller, object
5
+ object.to_xml :root => controller.send(:xml_root)
6
+ end
7
+
8
+ module ControllerExtensions
9
+ protected
10
+
11
+ def xml_root
12
+ self.class.name.sub('Controller', '').underscore.split('/').last.pluralize
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ module Porth
2
+ class Handler
3
+ attr_reader :template
4
+
5
+ def initialize template
6
+ @template = template
7
+ end
8
+
9
+ def call
10
+ "#{format}.call controller, instance_eval { #{template.source} }"
11
+ end
12
+
13
+ def format
14
+ self.class.formats.fetch(template.formats.first) { raise UnknownFormatError }
15
+ end
16
+
17
+ def self.call template
18
+ new(template).call
19
+ end
20
+
21
+ def self.formats
22
+ @formats ||= {}
23
+ end
24
+
25
+ def self.register_format format, mod
26
+ formats[format] = mod
27
+ ActionController::Base.send :include, mod::ControllerExtensions
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,7 @@
1
+ module Porth
2
+ class UnknownFormatError < StandardError
3
+ def message
4
+ 'Unknown format. Supported formats are ' + Handler.formats.keys.join(', ')
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Porth
2
+ VERSION = '0.0.1'
3
+ end
data/porth.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $:.push File.expand_path("../lib", __FILE__)
4
+ require "porth/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "porth"
8
+ s.version = Porth::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.authors = ["Tate Johnson"]
11
+ s.email = ["tate@tatey.com"]
12
+ s.homepage = "https://github.com/tatey/porth"
13
+ s.summary = %q{Plain Old Ruby Template Handler}
14
+ s.description = %q{Write your views using plain old Ruby}
15
+
16
+ s.rubyforge_project = "porbt"
17
+
18
+ s.files = `git ls-files`.split "\n"
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split "\n"
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+
23
+ s.add_runtime_dependency 'actionpack', '>= 3.1.0', '< 4.0.0'
24
+ s.add_runtime_dependency 'json', '~> 1.6.1'
25
+
26
+ s.add_development_dependency 'minitest', '~> 2.6.2'
27
+ s.add_development_dependency 'rake', '~> 0.9.2'
28
+ end
@@ -0,0 +1,3 @@
1
+ (1..2).map do |n|
2
+ {n => n * 2}
3
+ end
@@ -0,0 +1 @@
1
+ [@foo]
@@ -0,0 +1,10 @@
1
+ require 'action_view'
2
+ require 'test_helper'
3
+
4
+ class HandlerTest < MiniTest::Unit::TestCase
5
+ def test_unsupported_format
6
+ assert_raises UnknownFormatError do
7
+ Handler.new(ActionView::Template.new(nil, nil, nil, {:format => 'text/html'})).format
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ class RenderingTest < MiniTest::Unit::TestCase
4
+ class TestsController < ActionController::Base
5
+ def params
6
+ @params ||= {}
7
+ end
8
+ end
9
+
10
+ def render file, format, assigns = {}
11
+ ActionView::Base.new(template_dir, assigns, @controller).tap do |view|
12
+ view.lookup_context.freeze_formats [format]
13
+ end.render :file => file
14
+ end
15
+
16
+ def template_dir
17
+ File.expand_path File.dirname(__FILE__) + '/../fixtures'
18
+ end
19
+
20
+ def setup
21
+ @controller = TestsController.new
22
+ end
23
+
24
+ def test_json
25
+ assert_equal '[{"1":2},{"2":4}]', render('block', :json)
26
+ end
27
+
28
+ def test_json_callback
29
+ @controller.params[:callback] = 'myFunction'
30
+ assert_equal 'myFunction([{"1":2},{"2":4}])', render('block', :json)
31
+ end
32
+
33
+ def test_xml
34
+ assert_equal %{<?xml version="1.0" encoding="UTF-8"?>\n<tests type="array">\n <test 1="2" type="hash"/>\n <test 2="4" type="hash"/>\n</tests>\n}, render('block', :xml)
35
+ end
36
+
37
+ def test_instance_variable
38
+ assert_equal '["bar"]', render('instance_variable', :json, 'foo' => 'bar')
39
+ end
40
+ end
@@ -0,0 +1,22 @@
1
+ require 'test_helper'
2
+
3
+ class XMLTest < MiniTest::Unit::TestCase
4
+ def klass name
5
+ instance_eval <<-RUBY_STRING
6
+ Class.new do
7
+ include Format::XML::ControllerExtensions
8
+
9
+ def self.name
10
+ '#{name}'
11
+ end
12
+ end
13
+ RUBY_STRING
14
+ end
15
+
16
+ def test_send_xml_root
17
+ assert_equal 'foos', klass('Foo').new.send(:xml_root)
18
+ assert_equal 'foos', klass('FooController').new.send(:xml_root)
19
+ assert_equal 'foos', klass('FoosController').new.send(:xml_root)
20
+ assert_equal 'bars', klass('Foo::BarController').new.send(:xml_root)
21
+ end
22
+ end
@@ -0,0 +1,4 @@
1
+ require 'minitest/autorun'
2
+ require 'porth'
3
+
4
+ include Porth
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: porth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tate Johnson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-23 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: actionpack
16
+ requirement: &70355213483100 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ - - <
23
+ - !ruby/object:Gem::Version
24
+ version: 4.0.0
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: *70355213483100
28
+ - !ruby/object:Gem::Dependency
29
+ name: json
30
+ requirement: &70355213481080 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 1.6.1
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: *70355213481080
39
+ - !ruby/object:Gem::Dependency
40
+ name: minitest
41
+ requirement: &70355213479740 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 2.6.2
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: *70355213479740
50
+ - !ruby/object:Gem::Dependency
51
+ name: rake
52
+ requirement: &70355213477700 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ version: 0.9.2
58
+ type: :development
59
+ prerelease: false
60
+ version_requirements: *70355213477700
61
+ description: Write your views using plain old Ruby
62
+ email:
63
+ - tate@tatey.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - .gitignore
69
+ - Gemfile
70
+ - LICENSE
71
+ - README.md
72
+ - Rakefile
73
+ - lib/porth.rb
74
+ - lib/porth/format/json.rb
75
+ - lib/porth/format/xml.rb
76
+ - lib/porth/handler.rb
77
+ - lib/porth/unknown_format_error.rb
78
+ - lib/porth/version.rb
79
+ - porth.gemspec
80
+ - test/fixtures/block.rb
81
+ - test/fixtures/instance_variable.rb
82
+ - test/port/handler_test.rb
83
+ - test/port/rendering_test.rb
84
+ - test/port/xml_test.rb
85
+ - test/test_helper.rb
86
+ homepage: https://github.com/tatey/porth
87
+ licenses: []
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ segments:
99
+ - 0
100
+ hash: -1831659487050102680
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ segments:
108
+ - 0
109
+ hash: -1831659487050102680
110
+ requirements: []
111
+ rubyforge_project: porbt
112
+ rubygems_version: 1.8.10
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Plain Old Ruby Template Handler
116
+ test_files:
117
+ - test/fixtures/block.rb
118
+ - test/fixtures/instance_variable.rb
119
+ - test/port/handler_test.rb
120
+ - test/port/rendering_test.rb
121
+ - test/port/xml_test.rb
122
+ - test/test_helper.rb