picky 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,8 +4,16 @@ class Hash
4
4
 
5
5
  # Dumps binary self to the path given.
6
6
  #
7
+ # TODO Still used? If yes, spec!
8
+ #
7
9
  def dump_to path
8
10
  File.open(path, 'w:binary') { |out_file| Marshal.dump self, out_file }
9
11
  end
10
12
 
13
+ # Use yajl's encoding.
14
+ #
15
+ def to_json options = {}
16
+ Yajl::Encoder.encode self, options
17
+ end
18
+
11
19
  end
@@ -1,6 +1,7 @@
1
1
  module Results
2
2
 
3
- # This is the internal results object. Usually, to_marshal, or to_json is called on it.
3
+ # This is the internal results object. Usually, to_marshal, or to_json
4
+ # is called on it to get a string for the answer.
4
5
  #
5
6
  class Base
6
7
 
@@ -32,10 +33,10 @@ module Results
32
33
  :duration => duration,
33
34
  :total => total)
34
35
  end
35
- # Convert to marshal format.
36
+ # The default format is json.
36
37
  #
37
- def to_marshal
38
- Marshal.dump serialize
38
+ def to_response options = {}
39
+ to_json options
39
40
  end
40
41
  # Convert to json format.
41
42
  #
@@ -9,11 +9,5 @@ module Results
9
9
  ?> + super
10
10
  end
11
11
 
12
- # The default response style for full results is to_marshal.
13
- #
14
- def to_response
15
- to_marshal
16
- end
17
-
18
12
  end
19
13
  end
@@ -7,6 +7,7 @@ gem 'bundler', '>=0.9.26'
7
7
  gem 'rack', '1.2.1'
8
8
  gem 'rack-mount', '0.6.9'
9
9
  gem 'text', '0.2.0'
10
+ gem 'yajl-ruby', '0.7.8', :require => 'yajl'
10
11
 
11
12
  # Should be optional.
12
13
  #
@@ -4,7 +4,42 @@ describe Hash do
4
4
 
5
5
  describe 'dump_to' do
6
6
  it 'should description' do
7
-
7
+ # TODO
8
+ end
9
+ end
10
+
11
+ describe "to_json" do
12
+ before(:each) do
13
+ # A very realistic example.
14
+ #
15
+ @obj = { :allocations => [
16
+ ['c', 17.53, 275179, [["name","s*","s"]],[]],
17
+ ['c', 15.01, 164576, [["category","s*","s"]],[]],
18
+ ['p', 12.94, 415634, [["street","s*","s"]],[]],
19
+ ['p', 12.89, 398247, [["name","s*","s"]],[]],
20
+ ['p', 12.67, 318912, [["city","s*","s"]],[]],
21
+ ['p', 12.37, 235933, [["first_name","s*","s"]],[]],
22
+ ["p", 11.76, 128259, [["maiden_name","s*","s"]],[]],
23
+ ['p', 11.73, 124479, [["occupation","s*","s"]],[]],
24
+ ['c', 11.35, 84807, [["street","s*","s"]],[]],
25
+ ['c', 11.15, 69301, [["city","s*","s"]],[]],
26
+ ['p', 4.34, 77, [["street_number","s*","s"]],[]],
27
+ ['c', 2.08, 8, [["street_number","s*","s"]],[]],
28
+ ['c', 1.61, 5, [["adword","s*","s"]],[]]
29
+ ],
30
+ :offset => 0,
31
+ :duration => 0.04,
32
+ :total => 2215417
33
+ }
34
+ end
35
+ it "should be correct" do
36
+ @obj.to_json.should == '{"allocations":[["c",17.53,275179,[["name","s*","s"]],[]],["c",15.01,164576,[["category","s*","s"]],[]],["p",12.94,415634,[["street","s*","s"]],[]],["p",12.89,398247,[["name","s*","s"]],[]],["p",12.67,318912,[["city","s*","s"]],[]],["p",12.37,235933,[["first_name","s*","s"]],[]],["p",11.76,128259,[["maiden_name","s*","s"]],[]],["p",11.73,124479,[["occupation","s*","s"]],[]],["c",11.35,84807,[["street","s*","s"]],[]],["c",11.15,69301,[["city","s*","s"]],[]],["p",4.34,77,[["street_number","s*","s"]],[]],["c",2.08,8,[["street_number","s*","s"]],[]],["c",1.61,5,[["adword","s*","s"]],[]]],"offset":0,"duration":0.04,"total":2215417}'
37
+ end
38
+ it "should take options" do
39
+ lambda { @obj.to_json(:some => :option) }.should_not raise_error
40
+ end
41
+ it "should be fast" do
42
+ Benchmark.realtime { @obj.to_json }.should < 0.00006
8
43
  end
9
44
  end
10
45
 
@@ -30,17 +30,6 @@ describe Results do
30
30
  end
31
31
  end
32
32
  end
33
-
34
- describe 'to_marshal' do
35
- before(:each) do
36
- @results = Results::Base.new
37
- end
38
- it 'should do it correctly' do
39
- @results.stub! :serialize => :serialized
40
-
41
- @results.to_marshal.should == "\x04\b:\x0Fserialized"
42
- end
43
- end
44
33
 
45
34
  describe 'to_json' do
46
35
  before(:each) do
@@ -88,7 +88,7 @@ describe Routing do
88
88
  @routing.route '/searches/some_route' => Query::Full.new(:some_index, :some_other_index)
89
89
 
90
90
  @routing.routes.freeze
91
- @routing.call(env).should == [200, {"Content-Type"=>"application/octet-stream", "Content-Length"=>"50"}, ["\x04\b{\t:\x10allocations[\x00:\voffseti\x00:\rdurationi\x00:\ntotali\x00"]]
91
+ @routing.call(env).should == [200, {"Content-Type"=>"application/octet-stream", "Content-Length"=>"52"}, ["{\"allocations\":[],\"offset\":0,\"duration\":0,\"total\":0}"]]
92
92
  end
93
93
  it 'should route correctly' do
94
94
  env = rack_defaults_for '/searches/some_route?query=some_query&type=some_type'
@@ -100,7 +100,7 @@ describe Routing do
100
100
  @routing.route '/searches/some_route' => Query::Full.new(:some_index, :some_other_index), :query => { :type => :some_type }
101
101
 
102
102
  @routing.routes.freeze
103
- @routing.call(env).should == [200, {"Content-Type"=>"application/octet-stream", "Content-Length"=>"50"}, ["\x04\b{\t:\x10allocations[\x00:\voffseti\x00:\rdurationi\x00:\ntotali\x00"]]
103
+ @routing.call(env).should == [200, {"Content-Type"=>"application/octet-stream", "Content-Length"=>"52"}, ["{\"allocations\":[],\"offset\":0,\"duration\":0,\"total\":0}"]]
104
104
  end
105
105
  it 'should route correctly' do
106
106
  env = rack_defaults_for '/searches/some_wrong_route?query=some_query'
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Florian Hanke