jsonit 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,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/LICENCE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (C) 2011 by Klaas Speller
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
20
+
data/README.md ADDED
@@ -0,0 +1,167 @@
1
+ # Jsonit
2
+
3
+ Jsonit provides a way to quickly construct handcrafted json documents.
4
+ Jsonit requires objects to respond to `#to_json`.
5
+ It will attempt to load the json gem if this is not the case.
6
+
7
+ ## Usage
8
+
9
+ ``` ruby
10
+ require 'json' # Jsonit expects a #to_json method on object.
11
+ require 'jsonit'
12
+
13
+ # a simple object
14
+ Jsonit::Builder.new do |json|
15
+ json.foo "bar"
16
+ end.to_json #=> {"foo":"bar"}
17
+
18
+ # nested object
19
+ Jsonit::Builder.new do |json|
20
+ json.foo do
21
+ json.bar "baz"
22
+ end
23
+
24
+ json.alpha do
25
+ json.bravo do
26
+ json.charlie "delta"
27
+ end
28
+ end
29
+ end.to_json #=> {"foo":{"bar":"baz"},"alpha":{"bravo":{"charlie":"delta"}}}
30
+
31
+ # arrays
32
+ Jsonit::Builder.new do |json|
33
+ json.first [1, 2, 3]
34
+
35
+ json.second [1, 2, 3] do |itm|
36
+ json.value itm
37
+ end
38
+ end.to_json #=> {"first":[1, 2, 3],"second":[{"value":1},{"value":2},{"value":3}]}
39
+
40
+ ```
41
+
42
+ ## Rails 3
43
+
44
+ Jsonit can be used with rails 3.
45
+
46
+ In `app/helpers/photos_helper.rb`
47
+
48
+ ``` ruby
49
+ class PhotosHelper
50
+ def photos
51
+ Photo.all
52
+ end
53
+ end
54
+ ```
55
+
56
+ In `app/controllers/photos_controller.rb`
57
+
58
+ ``` ruby
59
+ class PhotosController < ApplicationController
60
+ respond_to :json
61
+ end
62
+
63
+ ```
64
+
65
+ In `app/views/photos/index.json.jsonit`
66
+
67
+ ``` ruby
68
+ json.ok true
69
+ json.data photos do |photo|
70
+ photo.title photo.title
71
+ photo.location url_for(photo)
72
+ end
73
+ ```
74
+
75
+ Result will be something like:
76
+
77
+ ``` json
78
+ {
79
+ "ok": true,
80
+ "data": [
81
+ {
82
+ "title": "My First Photo",
83
+ "location": "http://www.example.com/photos/1.json"
84
+ }
85
+ ]
86
+ }
87
+
88
+ ```
89
+
90
+ ## Sinatra
91
+
92
+ Jsonit can be used with Sinatra.
93
+
94
+ In your Gemfile:
95
+
96
+ ``` ruby
97
+ gem 'json'
98
+ gem 'jsonit'
99
+ ```
100
+
101
+ In `views/index.jsonit`:
102
+
103
+ ``` ruby
104
+ json.foo "bar"
105
+ ```
106
+
107
+ In your app:
108
+
109
+ ``` ruby
110
+ class App < Sinatra::Base
111
+ get "/" do
112
+ jsonit :index
113
+ end
114
+ end
115
+ ```
116
+
117
+
118
+ ## Padrino
119
+
120
+ Jsonit can be used with Padrino.
121
+
122
+ In your Gemfile:
123
+
124
+ ``` ruby
125
+ gem 'json'
126
+ gem 'jsonit'
127
+ ```
128
+
129
+ In `app/views/photos/index.json.jsonit`:
130
+
131
+ ``` ruby
132
+ json.photos photos do |photo|
133
+ json.title photo.title
134
+ json.location url_for(:photos, :show, :photo_id => photo.id)
135
+ end
136
+ ```
137
+
138
+ In your controller:
139
+
140
+ ``` ruby
141
+ MyApp.controllers :photos do
142
+ helpers do
143
+ def photos
144
+ Photos.all
145
+ end
146
+ end
147
+
148
+ get :index, :provides => :json do
149
+ render :'photos/index'
150
+ end
151
+
152
+ get :show, :with => :photo_id, :provides => :json do
153
+ render :'photo/show'
154
+ end
155
+ end
156
+ ```
157
+
158
+
159
+ ## Project status
160
+
161
+ Jsonit is currently under active development.
162
+
163
+ ## LICENSE
164
+
165
+ Json is Copyright (c) 2011 Klaas Speller and Voormedia and distributed under the MIT license. See the LICENCE file for more info.
166
+
167
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/jsonit.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "jsonit/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "jsonit"
7
+ s.version = Jsonit::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Klaas Speller"]
10
+ s.email = ["k.speller@voormedia.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{A JSON templating language}
13
+ s.description = %q{Create JSON documents with ease}
14
+
15
+ s.rubyforge_project = "jsonit"
16
+
17
+ s.add_development_dependency "json"
18
+ s.add_development_dependency "tilt"
19
+ s.add_development_dependency "rspec"
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
25
+ end
@@ -0,0 +1,51 @@
1
+ module Jsonit
2
+ class Builder
3
+
4
+ instance_methods.each { |m| undef_method(m) unless m.to_s =~ /^__|object_id/ }
5
+
6
+ def initialize
7
+ @document = scoped! do
8
+ yield self if block_given?
9
+ end
10
+ end
11
+
12
+ def to_s
13
+ @document.to_json
14
+ end
15
+
16
+ def to_json(*)
17
+ to_s
18
+ end
19
+
20
+ def set!(key, value=nil, &blk)
21
+ @current_scope[key] = if !block_given?
22
+ value
23
+ elsif value
24
+ array!(key, value, &blk)
25
+ else
26
+ object!(key, &blk)
27
+ end
28
+ end
29
+ alias_method :method_missing, :set!
30
+
31
+ private
32
+
33
+ def scoped!
34
+ @current_scope, previous_scope = {}, @current_scope
35
+ yield
36
+ @current_scope
37
+ ensure
38
+ @current_scope = previous_scope
39
+ end
40
+
41
+ def object!(key, &blk)
42
+ scoped!(&blk)
43
+ end
44
+
45
+ def array!(key, collection, &blk)
46
+ collection.map do |itm|
47
+ scoped! { blk.call(itm) }
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,14 @@
1
+ require "jsonit/rails/template_handler"
2
+
3
+ module Jsonit
4
+ module Rails
5
+ class Railtie < ::Rails::Railtie
6
+ initializer "jsonit.register_handler" do |app|
7
+ app.paths["app/views"].eager_load!
8
+ ActiveSupport.on_load(:action_view) do
9
+ ActionView::Template.register_template_handler "jsonit", Jsonit::Rails::TemplateHandler
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ require "jsonit/builder"
2
+ module Jsonit
3
+ module Rails
4
+ class TemplateHandler
5
+ default_format = Mime::JSON
6
+
7
+ class << self
8
+ def call(template)
9
+ "::Jsonit::Builder.new do |json|" +
10
+ template.source +
11
+ "end.to_json"
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,35 @@
1
+
2
+ module Tilt
3
+ class JsonitTemplate < ::Tilt::Template
4
+ default_mime_type = 'application/json'
5
+
6
+ def self.engine_initialized?
7
+ defined? ::Jsonit
8
+ end
9
+
10
+ def initialize_engine
11
+ require_template_library 'jsonit'
12
+ end
13
+
14
+ def prepare; end
15
+
16
+ def evaluate(scope, locals, &block)
17
+ return super(scope, locals, &block) if data.respond_to?(:to_str)
18
+ json = ::Jsonit::Builder.new
19
+ data.call(json)
20
+ end
21
+
22
+ def precompiled_preamble(locals)
23
+ "\n#{super};::Jsonit::Builder.new do |json|"
24
+ end
25
+
26
+ def precompiled_postamble(locals)
27
+ "\nend.to_json"
28
+ end
29
+
30
+ def precompiled_template(locals)
31
+ data.to_str
32
+ end
33
+ end
34
+ register 'jsonit', ::Tilt::JsonitTemplate
35
+ end
@@ -0,0 +1,3 @@
1
+ module Jsonit
2
+ VERSION = "0.0.1"
3
+ end
data/lib/jsonit.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "jsonit/builder"
2
+ module Jsonit
3
+ require "json" unless Object.respond_to?(:to_json)
4
+ require "jsonit/rails/railtie" if defined?(::Rails)
5
+ require "jsonit/tilt/tilt_jsonit" if defined?(::Tilt)
6
+
7
+ if defined?(::Sinatra)
8
+ module ::Sinatra::Templates
9
+ def jsonit(template, options={}, locals={})
10
+ render :jsonit, template, options, locals
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,64 @@
1
+ require "spec_helper"
2
+ require "jsonit/builder"
3
+
4
+ describe Jsonit::Builder, "Building json" do
5
+ specify "a simple object" do
6
+ Jsonit::Builder.new.to_json.should == %|{}|
7
+ end
8
+
9
+ specify "a simple object with a string value" do
10
+ Jsonit::Builder.new do |json|
11
+ json.foo "bar"
12
+ end.to_json.should == %|{"foo":"bar"}|
13
+ end
14
+
15
+ specify "#to_json" do
16
+ expect { Jsonit::Builder.new.to_json }.to_not raise_error
17
+ end
18
+
19
+ specify "a nested object" do
20
+ Jsonit::Builder.new do |json|
21
+ json.foo do
22
+ json.bar "baz"
23
+ end
24
+
25
+ json.alpha do
26
+ json.bravo "charlie"
27
+ json.delta "echo"
28
+
29
+ json.foxtrot do
30
+ json.golf "hotel"
31
+ end
32
+ end
33
+ end.to_json.should == %|{"foo":{"bar":"baz"},"alpha":{"bravo":"charlie","delta":"echo","foxtrot":{"golf":"hotel"}}}|
34
+ end
35
+
36
+ specify "an array" do
37
+ Jsonit::Builder.new do |json|
38
+ json.foo [1, "bar", false, true]
39
+ end.to_json.should == %|{"foo":[1,"bar",false,true]}|
40
+ end
41
+
42
+ specify "explicit keys" do
43
+ Jsonit::Builder.new do |json|
44
+ json.set! :foo, "bar"
45
+ end.to_json.should == %|{"foo":"bar"}|
46
+ end
47
+
48
+ specify "explicit with block" do
49
+ Jsonit::Builder.new do |json|
50
+ json.set! :foo do
51
+ json.set! :bar, "baz"
52
+ end
53
+ end.to_json.should == %|{"foo":{"bar":"baz"}}|
54
+ end
55
+
56
+ specify "collections with a block" do
57
+ Jsonit::Builder.new do |json|
58
+ json.foos ["bar", "baz"] do |item|
59
+ json.value item
60
+ end
61
+ json.foo "bar"
62
+ end.to_json.should == %|{"foos":[{"value":"bar"},{"value":"baz"}],"foo":"bar"}|
63
+ end
64
+ end
@@ -0,0 +1,27 @@
1
+ require "spec_helper"
2
+ require "tilt"
3
+
4
+ require "jsonit"
5
+
6
+ describe "Jsonit Tilt Support" do
7
+ it 'is registered for ".jsonit" files' do
8
+ Tilt['test.jsonit'].should == ::Tilt::JsonitTemplate
9
+ Tilt['test.json.jsonit'].should == ::Tilt::JsonitTemplate
10
+ end
11
+
12
+ specify "loading and evaluating templates on #render" do
13
+ template = Tilt::JsonitTemplate.new { |t| "json.foo 'bar'" }
14
+ template.render.should == %|{"foo":"bar"}|
15
+ end
16
+
17
+ specify "can be rendered more than once" do
18
+ template = Tilt::JsonitTemplate.new { |t| "json.foo 'bar'" }
19
+ 3.times { template.render.should == %|{"foo":"bar"}| }
20
+ end
21
+
22
+ specify "passing locals" do
23
+ template = Tilt::JsonitTemplate.new { 'json.foo name' }
24
+ template.render(Object.new, :name => 'Joe').should == %|{"foo":"Joe"}|
25
+ end
26
+ end
27
+
@@ -0,0 +1,5 @@
1
+ $:.unshift File.expand_path('../../lib', __FILE__)
2
+ $:.unshift File.dirname(__FILE__)
3
+
4
+ require "json"
5
+
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonit
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Klaas Speller
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-21 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: json
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: tilt
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rspec
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id003
49
+ description: Create JSON documents with ease
50
+ email:
51
+ - k.speller@voormedia.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - .gitignore
60
+ - Gemfile
61
+ - LICENCE
62
+ - README.md
63
+ - Rakefile
64
+ - jsonit.gemspec
65
+ - lib/jsonit.rb
66
+ - lib/jsonit/builder.rb
67
+ - lib/jsonit/rails/railtie.rb
68
+ - lib/jsonit/rails/template_handler.rb
69
+ - lib/jsonit/tilt/tilt_jsonit.rb
70
+ - lib/jsonit/version.rb
71
+ - spec/jsonit/builder_spec.rb
72
+ - spec/jsonit/tilt/tilt_jsonit_spec.rb
73
+ - spec/spec_helper.rb
74
+ has_rdoc: true
75
+ homepage: ""
76
+ licenses: []
77
+
78
+ post_install_message:
79
+ rdoc_options: []
80
+
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project: jsonit
98
+ rubygems_version: 1.5.3
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: A JSON templating language
102
+ test_files:
103
+ - spec/jsonit/builder_spec.rb
104
+ - spec/jsonit/tilt/tilt_jsonit_spec.rb
105
+ - spec/spec_helper.rb