elster 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- elster (0.0.2)
4
+ elster (0.0.3)
5
5
  multi_json
6
6
 
7
7
  GEM
@@ -16,11 +16,18 @@ GEM
16
16
  coderay (~> 1.0.5)
17
17
  method_source (~> 0.8)
18
18
  slop (~> 3.3.1)
19
+ pry (0.9.10-java)
20
+ coderay (~> 1.0.5)
21
+ method_source (~> 0.8)
22
+ slop (~> 3.3.1)
23
+ spoon (~> 0.0)
19
24
  slop (3.3.2)
25
+ spoon (0.0.1)
20
26
  turn (0.8.3)
21
27
  ansi
22
28
 
23
29
  PLATFORMS
30
+ java
24
31
  ruby
25
32
 
26
33
  DEPENDENCIES
@@ -18,17 +18,19 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- output = StringIO.new
22
- json = Elster::Streamer.new(output)
23
- json.key(:name, "George")
24
- json.key(:kids) do
25
- json.add("Job")
26
- json.add("Buster")
27
- end
28
- json.close
29
-
30
- puts output.string
31
- # {"name":"George","kids":["Job","Buster"]}
21
+ ``` ruby
22
+ output = StringIO.new
23
+ json = Elster::Streamer.new(output)
24
+ json.key(:name, "George")
25
+ json.key(:kids) do
26
+ json.add("Job")
27
+ json.add("Buster")
28
+ end
29
+ json.close
30
+
31
+ puts output.string
32
+ # {"name":"George","kids":["Job","Buster"]}
33
+ ```
32
34
 
33
35
  ## Contributing
34
36
 
@@ -1,2 +1,44 @@
1
1
  #!/usr/bin/env rake
2
+ require "rubygems"
3
+ require "bundler/setup"
4
+ Bundler.require
5
+
2
6
  require "bundler/gem_tasks"
7
+ require "benchmark"
8
+
9
+ desc "Run all tests"
10
+ task :test do
11
+ require 'turn'
12
+ require "pry"
13
+ require "test/unit"
14
+ require_relative "./test/streamer_test"
15
+ end
16
+
17
+ desc "Run a simple benchmark"
18
+ task :benchmark do
19
+ require "stringio"
20
+ ITERATIONS = 100000
21
+
22
+ Benchmark.bm do |x|
23
+ x.report("Elster ") do
24
+ result = Elster::Streamer.new StringIO.new
25
+ ITERATIONS.times do |i|
26
+ result.key("name_#{i}", "Jason #{i}")
27
+ result.key("age_#{i}", i)
28
+ result.key("nil_#{i}", nil)
29
+ end
30
+ result.close
31
+ end
32
+
33
+ x.report("Hash ") do
34
+ result = {}
35
+ ITERATIONS.times do |i|
36
+ result["name_#{i}"] = "Jason #{i}"
37
+ result["age_#{i}"] = i
38
+ result["nil_#{i}"] = nil
39
+ end
40
+ MultiJson.dump(result)
41
+ end
42
+ end
43
+
44
+ end
@@ -15,6 +15,7 @@ Gem::Specification.new do |gem|
15
15
  gem.version = Elster::VERSION
16
16
 
17
17
  gem.add_dependency "multi_json"
18
+
18
19
  gem.add_development_dependency "minitest"
19
20
  gem.add_development_dependency "turn"
20
21
  gem.add_development_dependency "pry"
@@ -1,8 +1,8 @@
1
- require "stringio"
2
1
  require "multi_json"
3
2
 
4
3
  module Elster
5
4
  class Streamer
5
+ attr_reader :output
6
6
  # Create a new instance of Streamer with the specified output stream. The
7
7
  # `output` must respond to `write`.
8
8
  def initialize(output)
@@ -106,11 +106,36 @@ module Elster
106
106
  end
107
107
 
108
108
  def encode_value(value)
109
+ case value
110
+ when Numeric
111
+ encode_number(value)
112
+ when String
113
+ encode_string(value)
114
+ when nil
115
+ encode_nil(value)
116
+ else
117
+ MultiJson.dump(value)
118
+ end
119
+ end
120
+
121
+ def encode_generic(value)
109
122
  MultiJson.dump(value)
110
123
  end
111
124
 
112
125
  def encode_string(value)
113
- encode_value(value.to_s)
126
+ if /\A[\w\d \t]+\Z/ =~ value
127
+ "\"#{value}\""
128
+ else
129
+ encode_generic(value.to_s)
130
+ end
131
+ end
132
+
133
+ def encode_number(value)
134
+ value.to_s
135
+ end
136
+
137
+ def encode_nil(value)
138
+ "null"
114
139
  end
115
140
 
116
141
  def nest_in
@@ -1,3 +1,3 @@
1
1
  module Elster
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,10 +1,5 @@
1
- require "test/unit"
2
- require 'turn'
3
1
  require "stringio"
4
2
  require "json"
5
- require "pry"
6
-
7
- require "elster"
8
3
 
9
4
  class StreamerTest < MiniTest::Unit::TestCase
10
5
  attr_reader :output, :json
@@ -102,7 +97,7 @@ class StreamerTest < MiniTest::Unit::TestCase
102
97
  assert_equal "Bobertson", parsed[0]["child"]["name"]
103
98
  end
104
99
 
105
- def tested_array_nested_object
100
+ def test_array_nested_object
106
101
  json.add do
107
102
  json.key(:name, "Bobert")
108
103
  end
@@ -114,4 +109,25 @@ class StreamerTest < MiniTest::Unit::TestCase
114
109
  assert_equal "Bobert", parsed[0]["name"]
115
110
  assert_equal 44, parsed[1]["age"]
116
111
  end
112
+
113
+ def test_ruby_hash
114
+ json.key("name", {:first => "Bobert"})
115
+ json.close
116
+
117
+ assert_equal "Bobert", parsed["name"]["first"]
118
+ end
119
+
120
+ def test_ruby_array
121
+ json.key("an_array", [1, 2])
122
+ json.close
123
+
124
+ assert_equal [1, 2], parsed["an_array"]
125
+ end
126
+
127
+ def test_string_with_cr
128
+ json.key("description", "I am \nmulti\nlined")
129
+ json.close
130
+
131
+ assert_equal "I am \nmulti\nlined", parsed["description"]
132
+ end
117
133
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elster
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-20 00:00:00.000000000Z
12
+ date: 2012-08-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
16
- requirement: &70299023008800 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70299023008800
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: minitest
27
- requirement: &70299022991060 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *70299022991060
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: turn
38
- requirement: &70299022965300 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '0'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *70299022965300
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: pry
49
- requirement: &70299022961820 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,7 +69,12 @@ dependencies:
54
69
  version: '0'
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *70299022961820
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
58
78
  description:
59
79
  email:
60
80
  - bigjasonwebb@gmail.com
@@ -85,6 +105,7 @@ files:
85
105
  - /Users/jwebb/src/bigjason/elster/lib/elster/version.rb
86
106
  - /Users/jwebb/src/bigjason/elster/lib/elster.rb
87
107
  - /Users/jwebb/src/bigjason/elster/LICENSE
108
+ - /Users/jwebb/src/bigjason/elster/pkg/elster-0.0.2.gem
88
109
  - /Users/jwebb/src/bigjason/elster/Rakefile
89
110
  - /Users/jwebb/src/bigjason/elster/README.md
90
111
  - /Users/jwebb/src/bigjason/elster/test/streamer_test.rb
@@ -100,17 +121,22 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
121
  - - ! '>='
101
122
  - !ruby/object:Gem::Version
102
123
  version: '0'
124
+ segments:
125
+ - 0
126
+ hash: 2730646358943498707
103
127
  required_rubygems_version: !ruby/object:Gem::Requirement
104
128
  none: false
105
129
  requirements:
106
130
  - - ! '>='
107
131
  - !ruby/object:Gem::Version
108
132
  version: '0'
133
+ segments:
134
+ - 0
135
+ hash: 2730646358943498707
109
136
  requirements: []
110
137
  rubyforge_project:
111
- rubygems_version: 1.8.10
138
+ rubygems_version: 1.8.24
112
139
  signing_key:
113
140
  specification_version: 3
114
141
  summary: A simple streaming JSON encoder.
115
142
  test_files: []
116
- has_rdoc: