Module: Moonstone::Racker

Included in:
BasicSearch, LocalSearch
Defined in:
lib/moonstone/racker.rb,
lib/moonstone/racker/client.rb,
lib/moonstone/racker/basic_search.rb,
lib/moonstone/racker/local_search.rb

Overview

include me in a Moonstone::Engine, maybe?

Defined Under Namespace

Modules: BasicSearch, LocalClient, LocalSearch

Constant Summary collapse

PathMatcher =
%r{^/([\w_]+)\.([\w_]+)$}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate_rackup_file(engine, store) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/moonstone/racker.rb', line 67

def self.generate_rackup_file(engine, store)      
  rackup = <<RACKUP
options[:Port] = 9293
#{yield}
require 'moonstone/racker/local_search'
#{engine}.module_eval do
  include Moonstone::Racker::LocalSearch
end
run #{engine}.new(:store => "#{File.expand_path store}")
RACKUP

  File.open "#{File.dirname(store)}/config.ru", "w" do |f|
    f.puts rackup
  end
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/moonstone/racker.rb', line 9

def call(env)
  request, response = Rack::Request.new(env), Rack::Response.new
  # Determine (or possibly fake) an HTTP method
  real = request.request_method.upcase
  http_method = if (real == 'POST') && (fake = request.params['_method'])
    fake.upcase
  else
    real
  end
  # Match against a very limited species of URI path.
  whole, action, ext = request.path_info.match(PathMatcher).to_a
  # Poor man's content negotiation
  content_type = case ext
  when 'json'
    'application/json'
  end
  response['Content-Type'] = content_type if content_type
  # Poor man's routing
  method_name = action ? "#{ext || 'html'}_#{http_method}_#{action}" : nil
  if method_name && respond_to?(method_name)
    response.body = send(method_name, request).to_s
  else
    response.status, response.body = 404, "404"
  end
  response.finish
end

#json_GET_document(request) ⇒ Object



63
64
65
# File 'lib/moonstone/racker.rb', line 63

def json_GET_document(request)
  document(request.params['id'].to_i).to_json
end

#json_GET_engine_version(request) ⇒ Object



47
48
49
50
51
# File 'lib/moonstone/racker.rb', line 47

def json_GET_engine_version(request)
  { :name => self.class.name, 
    :version => `git show-ref -h -s --abbrev HEAD`.chomp.split.first
  }.to_json
end

#json_GET_index_info(request) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/moonstone/racker.rb', line 53

def json_GET_index_info(request)
  md =  || {}
  {   :build_date => md["build_date"],
      :build_engine => {  :name => md["engine_name"], 
                          :version => md["engine_version"]},
      :query_conditions => md["query_conditions"],
      :doc_count => doc_count
  }.to_json
end

#search_options(request) ⇒ Object

helper for action methods



37
38
39
40
41
42
43
44
45
# File 'lib/moonstone/racker.rb', line 37

def search_options(request)
  params = request.params
  limit = params['limit']
  offset = params['offset']
  options = {}
  options[:limit] = limit.to_i if limit
  options[:offset] = offset.to_i if offset
  options
end