rack-google-custom-search 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -1,15 +1,16 @@
1
+ # -*- coding: utf-8 -*-
1
2
  module Rack #:nodoc:
2
3
  class GoogleCustomSearch < Struct.new :app, :options
3
-
4
+ DEFAULTS_OPTIONS = {:css_id => 'cse', :auto_complete => false}
4
5
  def call(env)
5
6
  status, headers, response = app.call(env)
6
-
7
+
7
8
  if headers["Content-Type"] =~ /text\/html|application\/xhtml\+xml/
8
9
  body = ""
9
10
  response.each { |part| body << part }
10
11
  index = body.rindex("</body>")
11
12
  if index
12
- body.insert(index, js_files(options[:token]))
13
+ body.insert(index, js_files(DEFAULTS_OPTIONS.merge(options)))
13
14
  headers["Content-Length"] = body.length.to_s
14
15
  response = [body]
15
16
  end
@@ -20,20 +21,26 @@ module Rack #:nodoc:
20
21
 
21
22
  protected
22
23
 
23
- # Returns JS to be embeded.
24
- def js_files(token)
25
- returning_value = <<-EOF
24
+ # Returns JS to be embeded.
25
+ def js_files(options={})
26
+ returning_value = <<-EOF
26
27
  <script src="http://www.google.com/jsapi" type="text/javascript"></script>
27
28
  <script type="text/javascript">
28
-  google.load('search', '1');
29
-  google.setOnLoadCallback(function() {
30
-    var customSearchControl = new google.search.CustomSearchControl('#{token}');
31
-    customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
32
-    customSearchControl.draw('cse');
33
-  }, true);
29
+ google.load('search', '1');
30
+ google.setOnLoadCallback(function() {
31
+ var customSearchControl = new google.search.CustomSearchControl('#{options[:token]}');
32
+ customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
33
+ var options = new google.search.DrawOptions();
34
+ #{auto_complete(options[:auto_complete])}
35
+ customSearchControl.draw('#{options[:css_id]}', options);
36
+ }, true);
34
37
  </script>
35
38
  EOF
36
39
  returning_value
37
- end
40
+ end
41
+
42
+ def auto_complete(cond)
43
+ 'options.setAutoComplete(true);' if cond
44
+ end
38
45
  end
39
46
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rack-google-custom-search}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jerome Riga"]
12
- s.date = %q{2010-11-15}
12
+ s.date = %q{2010-11-25}
13
13
  s.description = %q{Simple rack middleware to copies google custom search script}
14
14
  s.email = %q{jriga@zemis.co.uk}
15
15
  s.extra_rdoc_files = [
@@ -1,7 +1,43 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Rack::GoogleCustomSearch" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
4
+ before { @midddleware = Rack::GoogleCustomSearch.new(nil, {}) }
5
+
6
+ it "should add autocompletion when set to true" do
7
+ expected = <<-EOF
8
+ <script src="http://www.google.com/jsapi" type="text/javascript"></script>
9
+ <script type="text/javascript">
10
+ google.load('search', '1');
11
+ google.setOnLoadCallback(function() {
12
+ var customSearchControl = new google.search.CustomSearchControl('token');
13
+ customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
14
+ var options = new google.search.DrawOptions();
15
+ options.setAutoComplete(true);
16
+ customSearchControl.draw('css_id', options);
17
+ }, true);
18
+ </script>
19
+ EOF
20
+
21
+ actual_result = @midddleware.send(:js_files, {:css_id => 'css_id', :token => 'token', :auto_complete => true})
22
+ actual_result.should == expected
23
+ end
24
+
25
+ it "should return default" do
26
+ expected = <<-EOF
27
+ <script src="http://www.google.com/jsapi" type="text/javascript"></script>
28
+ <script type="text/javascript">
29
+ google.load('search', '1');
30
+ google.setOnLoadCallback(function() {
31
+ var customSearchControl = new google.search.CustomSearchControl('token');
32
+ customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
33
+ var options = new google.search.DrawOptions();
34
+
35
+ customSearchControl.draw('css_id', options);
36
+ }, true);
37
+ </script>
38
+ EOF
39
+
40
+ actual_result = @midddleware.send(:js_files, {:css_id => 'css_id', :token => 'token', :auto_complete => false})
41
+ actual_result.should == expected
6
42
  end
7
43
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-google-custom-search
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jerome Riga
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-15 00:00:00 +00:00
18
+ date: 2010-11-25 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency