class Rack::GoogleCustomSearch

Constants

DEFAULTS_OPTIONS

Public Instance Methods

call(env) click to toggle source
# File lib/rack/google-custom-search.rb, line 4
def call(env)
  status, headers, response = app.call(env)
  
  if headers["Content-Type"] =~ /text\/html|application\/xhtml\+xml/
    body = ""
    response.each { |part| body << part }
    index = body.rindex("</body>")
    if index
      body.insert(index, js_files(DEFAULTS_OPTIONS.merge(options)))
      headers["Content-Length"] = body.length.to_s
      response = [body]
    end
  end

  [status, headers, response]
end

Protected Instance Methods

auto_complete(cond) click to toggle source
# File lib/rack/google-custom-search.rb, line 41
def auto_complete(cond)
  'options.setAutoComplete(true);' if cond
end
js_files(options={}) click to toggle source

Returns JS to be embeded.

# File lib/rack/google-custom-search.rb, line 24
    def js_files(options={})
      returning_value = <<-EOF
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
  google.load('search', '1');
  google.setOnLoadCallback(function() {
    var customSearchControl = new google.search.CustomSearchControl('#{options[:token]}');
    customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
    var options = new google.search.DrawOptions();
    #{auto_complete(options[:auto_complete])}
    customSearchControl.draw('#{options[:css_id]}', options);
  }, true);
</script>
EOF
      returning_value
    end