class DeziClient

DeziClient is a Ruby client for indexing and searching documents with the Dezi REST search platform. See dezi.org/ for details on the server.

See the test/test_dezi_client.rb for full example.

Usage:

client = DeziClient.new(
     :server     => 'http://localhost:5000',
     :username   => 'foo',
     :password   => 'secret',
)

doc = 'some/path/file.html'
response = client.add(doc)   # DeziResponse returned
if !response.is_success
   raise "Failed to add #{doc} to server"
end

# if Dezi server has auto_commit==false
# then must call commit()
client.commit()

response = client.search('q' => 'search string')  # DeziResponse returned
if !response.is_success
   raise "Failed to search"
end

response.results.each |result|
   puts "result: #{result.uri}"
end

Related classes: DeziResponse and DeziDoc

Attributes

about_server[RW]
commit_uri[RW]
cookies[RW]
debug[RW]
facets[RW]
fields[RW]
index_uri[RW]
last_response[RW]
rollback_uri[RW]
search_uri[RW]
server[RW]

attributes

user_agent[RW]

Public Class Methods

new(args) click to toggle source
# File lib/dezi/client.rb, line 136
def initialize(args)
    @debug = ENV['DEZI_DEBUG']
    
    if (args.has_key? :server)
        @server = args[:server]
    else 
        @server = 'http://localhost:5000'
    end

    # sanity check
    begin
        uri = URI.parse(@server)
    rescue URI::InvalidURIError => err
        raise "Bad :server value " + err
    end
    if (!uri.host || !uri.port)
        raise "Bad :server value " + @server
    end
    
    if (args.has_key? :username and args.has_key? :password)
        @un = args[:username]
        @pw = args[:password]
    end
    
    if (args.has_key? :user_agent)
        @user_agent = args[:user_agent]
    else
        @user_agent = 'dezi-client-ruby/'+version()
    end

    if (args.has_key? :cookies)
        @cookies = args[:cookies]
    else 
        @cookies = ''
    end
    
    if (args.has_key? :search and args.has_key? :index)
        @search_uri = @server + args[:search]
        @index_uri  = @server + args[:index]
    else
        conn = connection(@server)
        response = conn.get '/'

        if response.status != 200
            raise "Bad about response from server #{@server}: " . response.body
        end
        @about_server = response.body
        if @debug
            puts @about_server.inspect
        end
    
        @search_uri   = @about_server['search']
        @index_uri    = @about_server['index']
        @commit_uri   = @about_server['commit']
        @rollback_uri = @about_server['rollback']
        @fields       = @about_server['fields']
        @facets       = @about_server['facets']
    end
    
    @searcher = connection( @search_uri )
    
end

Public Instance Methods

add(doc, uri=nil, content_type=nil) click to toggle source

add() takes an initial argument of “doc” which can be a DeziDoc object, a string representing a Pathname, or a string representing the content of a document. If “doc” represents the content of a document, additional arguments “uri” and “content_type” are required.

# File lib/dezi/client.rb, line 256
def add(doc, uri=nil, content_type=nil)
    return _put_doc(doc, uri, content_type)
end
commit() click to toggle source

commit() and rollback() are only relevant if the Dezi server has “auto_commit” turned off.

# File lib/dezi/client.rb, line 279
def commit()
    conn = connection(@commit_uri)
    resp = conn.post
    return DeziResponse.new(resp)
end
connection(uri) click to toggle source
# File lib/dezi/client.rb, line 113
def connection(uri)
    opts = { 
        :url => uri,
        :headers => {
             'User-Agent'   => @user_agent,
             'Accept'       => 'application/json',
             'Cookie'       => @cookies
        }   
    }   
    conn = Faraday.new(opts) do |faraday|
        faraday.request :url_encoded
        [:mashify, :json, :raise_error].each{|mw| faraday.response(mw) }
        faraday.response :logger if @debug
        faraday.adapter  :excon   # IMPORTANT this is last
    end 

    if (@un && @pw)
        conn.request :basic_auth, @un, @pw
    end

    return conn
end
delete(uri) click to toggle source
# File lib/dezi/client.rb, line 269
def delete(uri)
    doc_uri = @index_uri + '/' + uri
    conn = connection(doc_uri)
    resp = conn.delete
    return DeziResponse.new(resp)
end
rollback() click to toggle source

commit() and rollback() are only relevant if the Dezi server has “auto_commit” turned off.

# File lib/dezi/client.rb, line 288
def rollback()
    conn = connection(@rollback_uri)
    resp = conn.post 
    return DeziResponse.new(resp)
end
update(doc, uri=nil, content_type=nil) click to toggle source

update() takes an initial argument of “doc” which can be a DeziDoc object, a string representing a Pathname, or a string representing the content of a document. If “doc” represents the content of a document, additional arguments “uri” and “content_type” are required.

# File lib/dezi/client.rb, line 265
def update(doc, uri=nil, content_type=nil)
    return _put_doc(doc, uri, content_type)
end
version() click to toggle source
# File lib/dezi/client.rb, line 109
def version
    return "1.1.2"
end

Private Instance Methods

_put_doc(doc, uri=nil, content_type=nil) click to toggle source
# File lib/dezi/client.rb, line 201
def _put_doc(doc, uri=nil, content_type=nil)
    body_buf = ""
            
    if (doc.is_a?(DeziDoc))
        body_buf = doc.as_string()
        if (uri == nil)
            uri = doc.uri
        end
        if (content_type == nil)
            content_type = doc.mime_type
        end
        
    elsif (Pathname.new(doc).exist?)
        file = File.new(doc, 'r')
        body_buf = file.read()
        if (uri == nil)
            uri = doc
        end
        
    else
        body_buf = doc
        if (uri == nil)
            raise "uri required"
        end
        
    end
    
    if (!content_type or !content_type.length)
        content_type = MIME::Types.type_for(uri)[0]
    end
    
    server_uri = '/' + uri
    if (@debug)
        puts "uri=#{uri}"
        puts "body=#{body_buf}"
        puts "content_type="#{content_type}"
    end
    
    conn = connection(@index_uri)
    resp = conn.post do |req|
        req.url @index_uri + server_uri
        req.body = body_buf
        req.headers['Content-Type'] = content_type
    end
        
    return DeziResponse.new(resp)
end