Permalink
Browse files

initial implementation of a rack app server

  • Loading branch information...
1 parent f26d10b commit cc20ecf6f21731f4fec7f656a75314c110757f1b @ryanb committed Apr 15, 2011
View
1 .rspec
@@ -1 +1,2 @@
--color
+--backtrace
@@ -0,0 +1,11 @@
+Feature: Remote Server
+
+ Background:
+ Given a remote database
+
+ @focus
+ Scenario: Basic index and search
+ Given records named "John Smith, John Doe, Jane, Joe"
+ When I index the database
+ And I query for "John"
+ Then I should find records named "John Smith, John Doe"
@@ -9,6 +9,14 @@
GC.start
end
+Given /^a remote database$/ do
+ pending
+ Xapit.setup(:database_path => "http://localhost:929292")
+ Xapit.remove_database
+ XapitMember.delete_all
+ GC.start
+end
+
Given /^(indexed )?records? named "([^\"]*)"$/ do |indexed, joined_names|
records = joined_names.split(', ').map { |name| {:name => name} }
create_records(records, indexed)
View
@@ -61,6 +61,7 @@ def self.value_index(*args)
require 'xapit/index_blueprint'
require 'xapit/collection'
require 'xapit/config'
+require 'xapit/server'
require 'xapit/facet_blueprint'
require 'xapit/facet'
require 'xapit/facet_option'
View
@@ -21,5 +21,17 @@ def xapian_document
end
xapian_doc
end
+
+ def self.from_json(json)
+ document = new
+ JSON.parse(json).each do |key, value|
+ document.send("#{key}=", value)
+ end
+ document
+ end
+
+ def to_json
+ # TODO
+ end
end
end
View
@@ -0,0 +1,41 @@
+module Xapit
+ class Server
+ def call(env)
+ request = Rack::Request.new(env)
+ if request.path == "/xapit/documents"
+ case request.request_method
+ when "POST" then add_document(request.params)
+ when "PUT" then update_document(request.params)
+ when "DELETE" then delete_document(request.params)
+ end
+ else
+ render :status => 404
+ end
+ end
+
+ def add_document(params)
+ document = Document.from_json(params["document"])
+ Config.database.add_document(document)
+ render
+ end
+
+ def update_document(params)
+ document = Document.from_json(params["document"])
+ Config.database.replace_document("Q#{document.data}", document)
+ render
+ end
+
+ def delete_document(params)
+ document = Document.from_json(params["document"])
+ Config.database.delete_document("Q#{document.data}")
+ render
+ end
+
+ def render(options = {})
+ options[:status] ||= 200
+ options[:content] ||= ""
+ options[:content_type] ||= "text/html"
+ [options[:status], {"Content-Type" => options[:content_type]}, [options[:content]]]
+ end
+ end
+end
View
@@ -0,0 +1,8 @@
+require "rubygems"
+
+# Add lib directory so we can include xapit, this isn't necessary when a gem is available
+root = File.expand_path('../../..', __FILE__)
+$:.unshift "#{root}/lib"
+require "xapit"
+
+run Xapit.server(:database_path => "#{root}/tmp/testdb", :template_path => "#{root}/fixtures/blankdb")
@@ -28,7 +28,6 @@
end
it "should add terms, values and options for facets" do
- Xapit::Config.database.xapian_database # force xapit to use a writable database at the beginning
stub(XapitMember).xapit_index_blueprint { @index }
member = XapitMember.new(:foo => ["ABC", "DEF"])
ids = Xapit::FacetBlueprint.new(XapitMember, 0, :foo).identifiers_for(member)
View
@@ -0,0 +1,43 @@
+require "spec_helper"
+
+describe Xapit::Server do
+ before(:each) do
+ @app = Xapit::Server.new
+ @request = Rack::MockRequest.new(@app)
+ end
+
+ it "adds new document to the database" do
+ response = @request.post("/xapit/documents", :params => {:document => {:data => "String-123"}.to_json})
+ response.status.should == 200
+ Xapit::Config.database.doccount.should == 1
+ end
+
+ it "responds with 404 when unknown url" do
+ response = @request.post("/xapit/foo")
+ response.status.should == 404
+ end
+
+ describe "indexed" do
+ before(:each) do
+ XapitMember.xapit do |index|
+ index.text :name
+ end
+ @hello = XapitMember.new(:name => "hello world")
+ Xapit.index_all
+ end
+
+ it "removes document from the database" do
+ Xapit::Config.database.doccount.should == 1
+ response = @request.delete("/xapit/documents", :params => {:document => {:data => "XapitMember-#{@hello.id}"}.to_json})
+ response.status.should == 200
+ Xapit::Config.database.doccount.should == 0
+ end
+
+ it "updates documents in the database" do
+ XapitMember.search("hello").should == [@hello]
+ response = @request.put("/xapit/documents", :params => {:document => {:data => "XapitMember-#{@hello.id}", :terms => "goodbye"}.to_json})
+ response.status.should == 200
+ XapitMember.search("hello").should == []
+ end
+ end
+end
View
@@ -10,6 +10,8 @@ Gem::Specification.new do |s|
s.files = Dir["{lib,spec,features,rails_generators,tasks}/**/*", "[A-Z]*", "init.rb", "install.rb", "uninstall.rb"] - ["Gemfile.lock"]
s.require_path = "lib"
+ s.add_dependency 'rack', '~> 1.2.2'
+
s.add_development_dependency 'rspec', '~> 2.5.0'
s.add_development_dependency 'cucumber', '~> 0.10.2'
s.add_development_dependency 'rails', '~> 3.0.6'

0 comments on commit cc20ecf

Please sign in to comment.