picky-client 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/picky-client.rb CHANGED
@@ -2,10 +2,10 @@ $KCODE = 'UTF-8' unless RUBY_VERSION > '1.8.7'
2
2
 
3
3
  require 'rubygems'
4
4
 
5
- require 'active_support'
5
+ # require 'active_support'
6
+ require 'yajl'
6
7
 
7
8
  dir = File.dirname __FILE__
8
9
  require File.expand_path('picky-client/engine', dir)
9
- require File.expand_path('picky-client/serializer', dir)
10
10
  require File.expand_path('picky-client/convenience', dir)
11
11
  require File.expand_path('picky-client/helper', dir)
@@ -8,14 +8,6 @@ module Picky
8
8
  def empty?
9
9
  allocations.empty?
10
10
  end
11
- # The rendered results or AR instances if you
12
- # have populated the results.
13
- #
14
- def entries limit = 20
15
- entries = []
16
- allocations.each { |allocation| allocation[5].each { |id| break if entries.size > limit; entries << id } }
17
- entries
18
- end
19
11
  # Returns the topmost limit results.
20
12
  #
21
13
  def ids limit = 20
@@ -71,6 +63,14 @@ module Picky
71
63
 
72
64
  objects
73
65
  end
66
+ # The rendered results or AR instances if you
67
+ # have populated the results.
68
+ #
69
+ def entries limit = 20
70
+ entries = []
71
+ allocations.each { |allocation| allocation[5].each { |id| break if entries.size > limit; entries << id } }
72
+ entries
73
+ end
74
74
 
75
75
  # The ids need to come in the order which the ids were returned by the ids method.
76
76
  #
@@ -68,18 +68,16 @@ module Picky
68
68
  end
69
69
 
70
70
  class Full < Base
71
- default_configuration :host => 'localhost', :port => 4000, :path => '/searches/full'
72
-
73
- # Full needs to deserialize the results.
74
- #
75
- def send_search params = {}
76
- Serializer.deserialize super(params)
71
+ default_configuration :host => 'localhost', :port => 8080, :path => '/searches/full'
72
+
73
+ @@parser_options = { :symbolize_keys => true }
74
+ def send_search params = {}
75
+ Yajl::Parser.parse super(params), @@parser_options
77
76
  end
78
-
79
77
  end
80
78
 
81
79
  class Live < Base
82
- default_configuration :host => 'localhost', :port => 4000, :path => '/searches/live'
80
+ default_configuration :host => 'localhost', :port => 8080, :path => '/searches/live'
83
81
  end
84
82
 
85
83
  end
@@ -50,12 +50,26 @@ describe Picky::Client do
50
50
  before(:each) do
51
51
  @full = Picky::Client::Full.new
52
52
  end
53
+ describe "send_search" do
54
+ before(:each) do
55
+ Net::HTTP.stub! :get => "{\"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}"
56
+ end
57
+ it "should return a parsed hash" do
58
+ @full.send_search(:query => 'something').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}
59
+ end
60
+ it "should be fast" do
61
+ GC.disable
62
+ Benchmark.realtime { @full.send_search(:query => 'something') }.should < 0.00015
63
+ GC.enable
64
+ end
65
+ end
66
+
53
67
  describe "defaults" do
54
68
  it "should set host to 'localhost'" do
55
69
  @full.host.should == 'localhost'
56
70
  end
57
- it "should set port to 4000" do
58
- @full.port.should == 4000
71
+ it "should set port to the right value" do
72
+ @full.port.should == 8080
59
73
  end
60
74
  it "should set path to '/searches/full'" do
61
75
  @full.path.should == '/searches/full'
@@ -123,8 +137,8 @@ describe Picky::Client do
123
137
  it "should set host to 'localhost'" do
124
138
  @live.host.should == 'localhost'
125
139
  end
126
- it "should set port to 4000" do
127
- @live.port.should == 4000
140
+ it "should set port to the right value" do
141
+ @live.port.should == 8080
128
142
  end
129
143
  it "should set path to '/searches/live'" do
130
144
  @live.path.should == '/searches/live'
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
@@ -16,8 +16,22 @@ cert_chain: []
16
16
 
17
17
  date: 2010-10-12 00:00:00 +02:00
18
18
  default_executable:
19
- dependencies: []
20
-
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: yajl-ruby
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 7
31
+ - 8
32
+ version: 0.7.8
33
+ type: :runtime
34
+ version_requirements: *id001
21
35
  description:
22
36
  email: florian.hanke+picky-client@gmail.com
23
37
  executables: []
@@ -30,13 +44,11 @@ files:
30
44
  - lib/picky-client/convenience.rb
31
45
  - lib/picky-client/engine.rb
32
46
  - lib/picky-client/helper.rb
33
- - lib/picky-client/serializer.rb
34
47
  - lib/picky-client.rb
35
48
  - README.rdoc
36
49
  - spec/picky-client/convenience_spec.rb
37
50
  - spec/picky-client/engine_spec.rb
38
51
  - spec/picky-client/helper_spec.rb
39
- - spec/picky-client/serializer_spec.rb
40
52
  has_rdoc: false
41
53
  homepage: http://floere.github.com/picky
42
54
  licenses: []
@@ -73,4 +85,3 @@ test_files:
73
85
  - spec/picky-client/convenience_spec.rb
74
86
  - spec/picky-client/engine_spec.rb
75
87
  - spec/picky-client/helper_spec.rb
76
- - spec/picky-client/serializer_spec.rb
@@ -1,25 +0,0 @@
1
- module Picky
2
-
3
- # This class handles serialization and deserialization.
4
- #
5
- class Serializer
6
-
7
- # Serialize the Results.
8
- #
9
- # Note: This code is executed on the search engine side.
10
- #
11
- def self.serialize serializable_results
12
- Marshal.dump serializable_results.serialize
13
- end
14
-
15
- # Create new search results from serialized ones.
16
- #
17
- # Note: This code is executed on the client side.
18
- #
19
- def self.deserialize serialized_results
20
- Marshal.load serialized_results
21
- end
22
-
23
- end
24
-
25
- end
@@ -1,39 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Picky::Serializer do
4
-
5
- describe "serialize-deserialize" do
6
- it "should serialize and deserialize certain values" do
7
- results = stub :results
8
- results.stub! :serialize => {}
9
-
10
- deserialized = Picky::Serializer.deserialize Picky::Serializer.serialize(results)
11
-
12
- deserialized.should == {}
13
- end
14
- end
15
-
16
- describe "serialize" do
17
- it "should serialize" do
18
- results = stub :results, :serialize => {
19
- :allocations => [[nil, nil, nil, [1,2,3,4,5,6,7,8]],
20
- [nil, nil, nil, [9,10,11,12,13,14,15,16]],
21
- [nil, nil, nil, [17,18,19,20,21,22,23]]],
22
- :offset => 123,
23
- :total => 12345,
24
- :duration => 0.12345
25
- }
26
-
27
- Picky::Serializer.serialize(results).should == "\x04\b{\t:\x10allocations[\b[\t000[\ri\x06i\ai\bi\ti\ni\vi\fi\r[\t000[\ri\x0Ei\x0Fi\x10i\x11i\x12i\x13i\x14i\x15[\t000[\fi\x16i\x17i\x18i\x19i\x1Ai\ei\x1C:\voffseti\x01{:\ntotali\x0290:\rdurationf\x0F0.12345\x00\xF2|"
28
- end
29
- end
30
-
31
- describe "deserialize" do
32
- it "should deserialize" do
33
- results = Picky::Serializer.deserialize "\x04\b{\t:\x10allocations[\b[\t000[\ri\x06i\ai\bi\ti\ni\vi\fi\r[\t000[\ri\x0Ei\x0Fi\x10i\x11i\x12i\x13i\x14i\x15[\t000[\fi\x16i\x17i\x18i\x19i\x1Ai\ei\x1C:\voffseti\x01{:\ntotali\x0290:\rdurationf\x0F0.12345\x00\xF2|"
34
-
35
- results.should be_kind_of(Hash)
36
- end
37
- end
38
-
39
- end