Class: Rhoconnect::Handler::Search::Engine

Inherits:
Object
  • Object
show all
Includes:
Helpers::AuthMethod, Helpers::Binding
Defined in:
lib/rhoconnect/handler/search/engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Binding

#bind_handler

Methods included from Helpers::AuthMethod

#auth_method

Constructor Details

#initialize(model, client, route_handler, params = {}) ⇒ Engine

Returns a new instance of Engine.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rhoconnect/handler/search/engine.rb', line 10

def initialize(model, client, route_handler, params = {})
  raise ArgumentError.new(UNKNOWN_CLIENT) unless client
  raise ArgumentError.new(UNKNOWN_SOURCE) unless (model and model.source)
  raise ArgumentError.new('Invalid app for source') unless model.source.app
  raise ArgumentError.new('Invalid sync handler') unless route_handler
  
  @client = client
  @source = model.source
  @model = model
  # if handler is not bound - bind it to self
  # normally it should be bound to a Controller's instance
  @route_handler = bind_handler(:search_handler_method, route_handler)  
  @params = params
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



5
6
7
# File 'lib/rhoconnect/handler/search/engine.rb', line 5

def client
  @client
end

#modelObject

Returns the value of attribute model.



5
6
7
# File 'lib/rhoconnect/handler/search/engine.rb', line 5

def model
  @model
end

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/rhoconnect/handler/search/engine.rb', line 5

def params
  @params
end

#route_handlerObject

Returns the value of attribute route_handler.



5
6
7
# File 'lib/rhoconnect/handler/search/engine.rb', line 5

def route_handler
  @route_handler
end

#sourceObject

Returns the value of attribute source.



5
6
7
# File 'lib/rhoconnect/handler/search/engine.rb', line 5

def source
  @source
end

Instance Method Details

#do_searchObject



25
26
27
28
29
30
# File 'lib/rhoconnect/handler/search/engine.rb', line 25

def do_search
  return if auth_method('login',client.id) == false
  res = run_search
  auth_method('logoff',client.id)
  res
end

#run_searchObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rhoconnect/handler/search/engine.rb', line 32

def run_search
  errordoc = nil
  docobj = nil
  result = nil
  begin
    errordoc = :search_errors
    docobj = client
    client.compute_token(:search_token)
    result = @route_handler.call
    client.put_data(:search,result) unless @source.is_pass_through?
    # operation,sync succeeded, remove errors
    docobj.lock(errordoc) do
      docobj.flush_data(errordoc)
    end
  rescue Exception => e
    # store sync,operation exceptions to be sent to all clients for this source/user
    log "Model raised search exception: #{e}"
    log e.backtrace.join("\n")
    docobj.lock(errordoc) do
      docobj.put_data(errordoc,{"search-error"=>{'message'=>e.message}},true)
    end
  end
  # pass through expects result hash
  @source.is_pass_through? ? result : true
end