elster 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (27) hide show
  1. data/Users/jwebb/src/bigjason/elster/Gemfile +4 -0
  2. data/Users/jwebb/src/bigjason/elster/Gemfile.lock +30 -0
  3. data/Users/jwebb/src/bigjason/elster/LICENSE +22 -0
  4. data/Users/jwebb/src/bigjason/elster/README.md +39 -0
  5. data/Users/jwebb/src/bigjason/elster/Rakefile +2 -0
  6. data/Users/jwebb/src/bigjason/elster/doc/Elster/Streamer.html +526 -0
  7. data/Users/jwebb/src/bigjason/elster/doc/Elster.html +129 -0
  8. data/Users/jwebb/src/bigjason/elster/doc/_index.html +125 -0
  9. data/Users/jwebb/src/bigjason/elster/doc/class_list.html +53 -0
  10. data/Users/jwebb/src/bigjason/elster/doc/css/common.css +1 -0
  11. data/Users/jwebb/src/bigjason/elster/doc/css/full_list.css +57 -0
  12. data/Users/jwebb/src/bigjason/elster/doc/css/style.css +328 -0
  13. data/Users/jwebb/src/bigjason/elster/doc/file.README.html +107 -0
  14. data/Users/jwebb/src/bigjason/elster/doc/file_list.html +55 -0
  15. data/Users/jwebb/src/bigjason/elster/doc/frames.html +28 -0
  16. data/Users/jwebb/src/bigjason/elster/doc/index.html +107 -0
  17. data/Users/jwebb/src/bigjason/elster/doc/js/app.js +214 -0
  18. data/Users/jwebb/src/bigjason/elster/doc/js/full_list.js +173 -0
  19. data/Users/jwebb/src/bigjason/elster/doc/js/jquery.js +4 -0
  20. data/Users/jwebb/src/bigjason/elster/doc/method_list.html +84 -0
  21. data/Users/jwebb/src/bigjason/elster/doc/top-level-namespace.html +112 -0
  22. data/Users/jwebb/src/bigjason/elster/elster.gemspec +21 -0
  23. data/Users/jwebb/src/bigjason/elster/lib/elster/streamer.rb +145 -0
  24. data/Users/jwebb/src/bigjason/elster/lib/elster/version.rb +3 -0
  25. data/Users/jwebb/src/bigjason/elster/lib/elster.rb +2 -0
  26. data/Users/jwebb/src/bigjason/elster/test/streamer_test.rb +117 -0
  27. metadata +116 -0
@@ -0,0 +1,84 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+ <html>
4
+ <head>
5
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
+
7
+ <link rel="stylesheet" href="css/full_list.css" type="text/css" media="screen" charset="utf-8" />
8
+
9
+ <link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" />
10
+
11
+
12
+
13
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
14
+
15
+ <script type="text/javascript" charset="utf-8" src="js/full_list.js"></script>
16
+
17
+
18
+ <base id="base_target" target="_parent" />
19
+ </head>
20
+ <body>
21
+ <script type="text/javascript" charset="utf-8">
22
+ if (window.top.frames.main) {
23
+ document.getElementById('base_target').target = 'main';
24
+ document.body.className = 'frames';
25
+ }
26
+ </script>
27
+ <div id="content">
28
+ <h1 id="full_list_header">Method List</h1>
29
+ <div id="nav">
30
+
31
+ <span><a target="_self" href="class_list.html">
32
+ Classes
33
+ </a></span>
34
+
35
+ <span><a target="_self" href="method_list.html">
36
+ Methods
37
+ </a></span>
38
+
39
+ <span><a target="_self" href="file_list.html">
40
+ Files
41
+ </a></span>
42
+
43
+ </div>
44
+ <div id="search">Search: <input type="text" /></div>
45
+
46
+ <ul id="full_list" class="method">
47
+
48
+
49
+ <li class="r1 ">
50
+ <span class='object_link'><a href="Elster/Streamer.html#add-instance_method" title="Elster::Streamer#add (method)">#add</a></span>
51
+
52
+ <small>Elster::Streamer</small>
53
+
54
+ </li>
55
+
56
+
57
+ <li class="r2 ">
58
+ <span class='object_link'><a href="Elster/Streamer.html#close-instance_method" title="Elster::Streamer#close (method)">#close</a></span>
59
+
60
+ <small>Elster::Streamer</small>
61
+
62
+ </li>
63
+
64
+
65
+ <li class="r1 ">
66
+ <span class='object_link'><a href="Elster/Streamer.html#initialize-instance_method" title="Elster::Streamer#initialize (method)">#initialize</a></span>
67
+
68
+ <small>Elster::Streamer</small>
69
+
70
+ </li>
71
+
72
+
73
+ <li class="r2 ">
74
+ <span class='object_link'><a href="Elster/Streamer.html#key-instance_method" title="Elster::Streamer#key (method)">#key</a></span>
75
+
76
+ <small>Elster::Streamer</small>
77
+
78
+ </li>
79
+
80
+
81
+ </ul>
82
+ </div>
83
+ </body>
84
+ </html>
@@ -0,0 +1,112 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
+ <head>
5
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
+ <title>
7
+ Top Level Namespace
8
+
9
+ &mdash; Documentation by YARD 0.8.2.1
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" />
16
+
17
+ <script type="text/javascript" charset="utf-8">
18
+ hasFrames = window.top.frames.main ? true : false;
19
+ relpath = '';
20
+ framesUrl = "frames.html#!" + escape(window.location.href);
21
+ </script>
22
+
23
+
24
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
25
+
26
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
27
+
28
+
29
+ </head>
30
+ <body>
31
+ <div id="header">
32
+ <div id="menu">
33
+
34
+ <a href="_index.html">Index</a> &raquo;
35
+
36
+
37
+ <span class="title">Top Level Namespace</span>
38
+
39
+
40
+ <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
41
+ </div>
42
+
43
+ <div id="search">
44
+
45
+ <a class="full_list_link" id="class_list_link"
46
+ href="class_list.html">
47
+ Class List
48
+ </a>
49
+
50
+ <a class="full_list_link" id="method_list_link"
51
+ href="method_list.html">
52
+ Method List
53
+ </a>
54
+
55
+ <a class="full_list_link" id="file_list_link"
56
+ href="file_list.html">
57
+ File List
58
+ </a>
59
+
60
+ </div>
61
+ <div class="clear"></div>
62
+ </div>
63
+
64
+ <iframe id="search_frame"></iframe>
65
+
66
+ <div id="content"><h1>Top Level Namespace
67
+
68
+
69
+
70
+ </h1>
71
+
72
+ <dl class="box">
73
+
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+ </dl>
82
+ <div class="clear"></div>
83
+
84
+ <h2>Defined Under Namespace</h2>
85
+ <p class="children">
86
+
87
+
88
+ <strong class="modules">Modules:</strong> <span class='object_link'><a href="Elster.html" title="Elster (module)">Elster</a></span>
89
+
90
+
91
+
92
+
93
+ </p>
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+ </div>
104
+
105
+ <div id="footer">
106
+ Generated on Sun Aug 19 23:07:22 2012 by
107
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
108
+ 0.8.2.1 (ruby-1.9.2).
109
+ </div>
110
+
111
+ </body>
112
+ </html>
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/elster/version", __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Jason Webb"]
6
+ gem.email = ["bigjasonwebb@gmail.com"]
7
+ gem.summary = %q{A simple streaming JSON encoder.}
8
+ gem.homepage = ""
9
+
10
+ gem.files = Dir["#{File.dirname(__FILE__)}/**/*"]
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.name = "elster"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = Elster::VERSION
16
+
17
+ gem.add_dependency "multi_json"
18
+ gem.add_development_dependency "minitest"
19
+ gem.add_development_dependency "turn"
20
+ gem.add_development_dependency "pry"
21
+ end
@@ -0,0 +1,145 @@
1
+ require "stringio"
2
+ require "multi_json"
3
+
4
+ module Elster
5
+ class Streamer
6
+ # Create a new instance of Streamer with the specified output stream. The
7
+ # `output` must respond to `write`.
8
+ def initialize(output)
9
+ @output = output
10
+ # Some mutable state to make Rich Hickey weep.
11
+ @stack = []
12
+ @item_count = 0
13
+ end
14
+
15
+ # Close the encoding optionally closing the provided output stream.
16
+ def close(close_stream=false)
17
+ end_section
18
+ if close_stream
19
+ @output.close
20
+ end
21
+ end
22
+
23
+ # Output an object key value pair. If a block is passed, the `value` will be
24
+ # ignored and a nested object or array can be added.
25
+ #
26
+ # Example:
27
+ #
28
+ # json.key(:mistake, "HUGE!")
29
+ #
30
+ # { "mistake" : "HUGE!" }
31
+ #
32
+ #
33
+ # json.key(:mistake) do
34
+ # json.key(:type, "HUGE!")
35
+ # end
36
+ #
37
+ # { "mistake" : { "type" : "HUGE!" } }
38
+ #
39
+ #
40
+ # json.key(:mistake) do
41
+ # json.add("HUGE!")
42
+ # end
43
+ #
44
+ # { "mistake" : [ "HUGE!" ] }
45
+ def key(key, value=nil, &block)
46
+ if @item_count > 0
47
+ write ","
48
+ else
49
+ @current_type = :object
50
+ begin_section
51
+ end
52
+
53
+ write encode_value(key)
54
+ write ":"
55
+
56
+ if block
57
+ nest_in
58
+ block.call
59
+ nest_out
60
+ else
61
+ write encode_value(value)
62
+ end
63
+
64
+ @item_count += 1
65
+ end
66
+
67
+ # Output an array. If a block is passes the `value` will be ignored and a nested value
68
+ # started.
69
+ #
70
+ # Example:
71
+ #
72
+ # json.add(1)
73
+ # json.add("Ansible")
74
+ #
75
+ # [ 1, "Ansible" ]
76
+ #
77
+ # json.add(1)
78
+ # json.add do
79
+ # json.set(:name, "Wiggens")
80
+ # end
81
+ #
82
+ # [ 1, { "name" : "Wiggens" } ]
83
+ def add(value=nil, &block)
84
+ if @item_count > 0
85
+ write ","
86
+ else
87
+ @current_type = :array
88
+ begin_section
89
+ end
90
+
91
+ if block
92
+ nest_in
93
+ block.call
94
+ nest_out
95
+ else
96
+ write encode_value(value)
97
+ end
98
+
99
+ @item_count += 1
100
+ end
101
+
102
+ protected
103
+
104
+ def write(output)
105
+ @output.write(output)
106
+ end
107
+
108
+ def encode_value(value)
109
+ MultiJson.dump(value)
110
+ end
111
+
112
+ def encode_string(value)
113
+ encode_value(value.to_s)
114
+ end
115
+
116
+ def nest_in
117
+ @stack.push([@item_count, @current_type])
118
+ @item_count = 0
119
+ @current_type = nil
120
+ end
121
+
122
+ def nest_out
123
+ end_section
124
+ @item_count, @current_type = @stack.pop
125
+ end
126
+
127
+ def begin_section
128
+ case @current_type
129
+ when :array
130
+ write("[")
131
+ else
132
+ write("{")
133
+ end
134
+ end
135
+
136
+ def end_section
137
+ case @current_type
138
+ when :array
139
+ write("]")
140
+ else
141
+ write("}")
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,3 @@
1
+ module Elster
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,2 @@
1
+ require "elster/version"
2
+ require "elster/streamer"
@@ -0,0 +1,117 @@
1
+ require "test/unit"
2
+ require 'turn'
3
+ require "stringio"
4
+ require "json"
5
+ require "pry"
6
+
7
+ require "elster"
8
+
9
+ class StreamerTest < MiniTest::Unit::TestCase
10
+ attr_reader :output, :json
11
+
12
+ def setup
13
+ @output = StringIO.new
14
+ @json = Elster::Streamer.new(output)
15
+ end
16
+
17
+ def teardown
18
+ @parsed = nil
19
+ end
20
+
21
+ def parsed
22
+ @parsed ||= JSON.parse(output.string)
23
+ end
24
+
25
+ def test_object_single_value
26
+ json.key(:name, "Bobert")
27
+ json.close
28
+
29
+ assert_equal "Bobert", parsed["name"]
30
+ end
31
+
32
+ def test_object_multiple_values
33
+ json.key(:first_name, "Bobert")
34
+ json.key(:last_name, "Price")
35
+ json.close
36
+
37
+ assert_equal "Bobert", parsed["first_name"]
38
+ assert_equal "Price", parsed["last_name"]
39
+ end
40
+
41
+ def test_object_numbers
42
+ json.key(:age, 40)
43
+ json.close
44
+
45
+ assert_equal 40, parsed["age"]
46
+ end
47
+
48
+ def test_array_muliple
49
+ json.add(1)
50
+ json.add(2)
51
+ json.close
52
+
53
+ assert_equal [1,2], parsed
54
+ end
55
+
56
+ def test_array_single
57
+ json.add("Hooper")
58
+ json.close
59
+
60
+ assert_equal ["Hooper"], parsed
61
+ end
62
+
63
+ def test_nested_object
64
+ json.key(:person) do
65
+ json.key(:name, "Bobert")
66
+ json.key(:age, 55)
67
+ end
68
+ json.close
69
+
70
+ assert_equal "Bobert", parsed["person"]["name"]
71
+ end
72
+
73
+ def test_deep_nested_object
74
+ json.key(:person) do
75
+ json.key(:name) do
76
+ json.key(:first, "Bobert")
77
+ end
78
+ end
79
+ json.close
80
+
81
+ assert_equal "Bobert", parsed["person"]["name"]["first"]
82
+ end
83
+
84
+ def test_nested_array_in_object
85
+ json.key(:children) do
86
+ json.add(1)
87
+ json.add(2)
88
+ end
89
+ json.close
90
+
91
+ assert_equal [1,2], parsed["children"]
92
+ end
93
+
94
+ def test_nested_object_in_array
95
+ json.add do
96
+ json.key(:child) do
97
+ json.key(:name, "Bobertson")
98
+ end
99
+ end
100
+ json.close
101
+
102
+ assert_equal "Bobertson", parsed[0]["child"]["name"]
103
+ end
104
+
105
+ def tested_array_nested_object
106
+ json.add do
107
+ json.key(:name, "Bobert")
108
+ end
109
+ json.add do
110
+ json.key(:age, 44)
111
+ end
112
+ json.close
113
+
114
+ assert_equal "Bobert", parsed[0]["name"]
115
+ assert_equal 44, parsed[1]["age"]
116
+ end
117
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elster
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason Webb
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-20 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: multi_json
16
+ requirement: &70299023008800 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70299023008800
25
+ - !ruby/object:Gem::Dependency
26
+ name: minitest
27
+ requirement: &70299022991060 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70299022991060
36
+ - !ruby/object:Gem::Dependency
37
+ name: turn
38
+ requirement: &70299022965300 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70299022965300
47
+ - !ruby/object:Gem::Dependency
48
+ name: pry
49
+ requirement: &70299022961820 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70299022961820
58
+ description:
59
+ email:
60
+ - bigjasonwebb@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - /Users/jwebb/src/bigjason/elster/doc/_index.html
66
+ - /Users/jwebb/src/bigjason/elster/doc/class_list.html
67
+ - /Users/jwebb/src/bigjason/elster/doc/css/common.css
68
+ - /Users/jwebb/src/bigjason/elster/doc/css/full_list.css
69
+ - /Users/jwebb/src/bigjason/elster/doc/css/style.css
70
+ - /Users/jwebb/src/bigjason/elster/doc/Elster/Streamer.html
71
+ - /Users/jwebb/src/bigjason/elster/doc/Elster.html
72
+ - /Users/jwebb/src/bigjason/elster/doc/file.README.html
73
+ - /Users/jwebb/src/bigjason/elster/doc/file_list.html
74
+ - /Users/jwebb/src/bigjason/elster/doc/frames.html
75
+ - /Users/jwebb/src/bigjason/elster/doc/index.html
76
+ - /Users/jwebb/src/bigjason/elster/doc/js/app.js
77
+ - /Users/jwebb/src/bigjason/elster/doc/js/full_list.js
78
+ - /Users/jwebb/src/bigjason/elster/doc/js/jquery.js
79
+ - /Users/jwebb/src/bigjason/elster/doc/method_list.html
80
+ - /Users/jwebb/src/bigjason/elster/doc/top-level-namespace.html
81
+ - /Users/jwebb/src/bigjason/elster/elster.gemspec
82
+ - /Users/jwebb/src/bigjason/elster/Gemfile
83
+ - /Users/jwebb/src/bigjason/elster/Gemfile.lock
84
+ - /Users/jwebb/src/bigjason/elster/lib/elster/streamer.rb
85
+ - /Users/jwebb/src/bigjason/elster/lib/elster/version.rb
86
+ - /Users/jwebb/src/bigjason/elster/lib/elster.rb
87
+ - /Users/jwebb/src/bigjason/elster/LICENSE
88
+ - /Users/jwebb/src/bigjason/elster/Rakefile
89
+ - /Users/jwebb/src/bigjason/elster/README.md
90
+ - /Users/jwebb/src/bigjason/elster/test/streamer_test.rb
91
+ homepage: ''
92
+ licenses: []
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 1.8.10
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: A simple streaming JSON encoder.
115
+ test_files: []
116
+ has_rdoc: