Class: DeziClient
- Inherits:
-
Object
- Object
- DeziClient
- Defined in:
- lib/dezi/client.rb
Overview
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
Instance Attribute Summary collapse
-
#about_server ⇒ Object
Returns the value of attribute about_server.
-
#commit_uri ⇒ Object
Returns the value of attribute commit_uri.
-
#cookies ⇒ Object
Returns the value of attribute cookies.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#facets ⇒ Object
Returns the value of attribute facets.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#index_uri ⇒ Object
Returns the value of attribute index_uri.
-
#last_response ⇒ Object
Returns the value of attribute last_response.
-
#rollback_uri ⇒ Object
Returns the value of attribute rollback_uri.
-
#search_uri ⇒ Object
Returns the value of attribute search_uri.
-
#server ⇒ Object
attributes.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Instance Method Summary collapse
-
#add(doc, uri = nil, content_type = nil) ⇒ Object
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.
-
#commit ⇒ Object
commit() and rollback() are only relevant if the Dezi server has “auto_commit” turned off.
- #connection(uri) ⇒ Object
- #delete(uri) ⇒ Object
-
#initialize(args) ⇒ DeziClient
constructor
A new instance of DeziClient.
-
#rollback ⇒ Object
commit() and rollback() are only relevant if the Dezi server has “auto_commit” turned off.
- #search(params) ⇒ Object
-
#update(doc, uri = nil, content_type = nil) ⇒ Object
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.
- #version ⇒ Object
Constructor Details
#initialize(args) ⇒ DeziClient
Returns a new instance of DeziClient.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# 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 |
Instance Attribute Details
#about_server ⇒ Object
Returns the value of attribute about_server.
97 98 99 |
# File 'lib/dezi/client.rb', line 97 def about_server @about_server end |
#commit_uri ⇒ Object
Returns the value of attribute commit_uri.
100 101 102 |
# File 'lib/dezi/client.rb', line 100 def commit_uri @commit_uri end |
#cookies ⇒ Object
Returns the value of attribute cookies.
107 108 109 |
# File 'lib/dezi/client.rb', line 107 def @cookies end |
#debug ⇒ Object
Returns the value of attribute debug.
105 106 107 |
# File 'lib/dezi/client.rb', line 105 def debug @debug end |
#facets ⇒ Object
Returns the value of attribute facets.
103 104 105 |
# File 'lib/dezi/client.rb', line 103 def facets @facets end |
#fields ⇒ Object
Returns the value of attribute fields.
102 103 104 |
# File 'lib/dezi/client.rb', line 102 def fields @fields end |
#index_uri ⇒ Object
Returns the value of attribute index_uri.
99 100 101 |
# File 'lib/dezi/client.rb', line 99 def index_uri @index_uri end |
#last_response ⇒ Object
Returns the value of attribute last_response.
104 105 106 |
# File 'lib/dezi/client.rb', line 104 def last_response @last_response end |
#rollback_uri ⇒ Object
Returns the value of attribute rollback_uri.
101 102 103 |
# File 'lib/dezi/client.rb', line 101 def rollback_uri @rollback_uri end |
#search_uri ⇒ Object
Returns the value of attribute search_uri.
98 99 100 |
# File 'lib/dezi/client.rb', line 98 def search_uri @search_uri end |
#server ⇒ Object
attributes
96 97 98 |
# File 'lib/dezi/client.rb', line 96 def server @server end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
106 107 108 |
# File 'lib/dezi/client.rb', line 106 def user_agent @user_agent end |
Instance Method Details
#add(doc, uri = nil, content_type = nil) ⇒ Object
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.
256 257 258 |
# File 'lib/dezi/client.rb', line 256 def add(doc, uri=nil, content_type=nil) return _put_doc(doc, uri, content_type) end |
#commit ⇒ Object
commit() and rollback() are only relevant if the Dezi server has “auto_commit” turned off.
279 280 281 282 283 |
# File 'lib/dezi/client.rb', line 279 def commit() conn = connection(@commit_uri) resp = conn.post return DeziResponse.new(resp) end |
#connection(uri) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# 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) ⇒ Object
269 270 271 272 273 274 |
# 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 ⇒ Object
commit() and rollback() are only relevant if the Dezi server has “auto_commit” turned off.
288 289 290 291 292 |
# File 'lib/dezi/client.rb', line 288 def rollback() conn = connection(@rollback_uri) resp = conn.post return DeziResponse.new(resp) end |
#search(params) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/dezi/client.rb', line 294 def search(params) if (!params.has_key?("q")) raise "'q' param required" end resp = @searcher.get @search_uri, params dr = DeziResponse.new(resp) if (!dr.is_success()) @last_response = dr return false else return dr end end |
#update(doc, uri = nil, content_type = nil) ⇒ Object
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.
265 266 267 |
# File 'lib/dezi/client.rb', line 265 def update(doc, uri=nil, content_type=nil) return _put_doc(doc, uri, content_type) end |
#version ⇒ Object
109 110 111 |
# File 'lib/dezi/client.rb', line 109 def version return "1.1.2" end |