Class: Opener::Webservice

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/opener/webservice.rb,
lib/opener/webservice/version.rb,
lib/opener/webservice/opt_parser.rb

Defined Under Namespace

Classes: OptParser

Constant Summary collapse

VERSION =
"2.0.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.accepted_params(*array) ⇒ Object

Specifies what parameters are accepted.

Parameters:

  • array (Array)

    The parameters to accept.



132
133
134
135
136
137
138
139
# File 'lib/opener/webservice.rb', line 132

def self.accepted_params(*array)
  if array.empty?
    return @accepted_params
  else
    @accepted_params = array
  end
  @accepted_params.concat([secret_symbol, token_symbol]) if Sinatra::Application.respond_to?(:authentication)
end

.callback_handlerOpener::CallbackHandler

Returns:

  • (Opener::CallbackHandler)


89
90
91
# File 'lib/opener/webservice.rb', line 89

def self.callback_handler
  return @callback_handler || new_callback_handler
end

.http_clientHTTPClient

Returns:

  • (HTTPClient)


82
83
84
# File 'lib/opener/webservice.rb', line 82

def self.http_client
  return @http_client || new_http_client
end

.new_callback_handlerOpener::CallbackHandler

Returns:

  • (Opener::CallbackHandler)


106
107
108
109
110
# File 'lib/opener/webservice.rb', line 106

def self.new_callback_handler
  handler = Opener::CallbackHandler.new

  return handler
end

.new_http_clientHTTPClient

Returns:

  • (HTTPClient)


96
97
98
99
100
101
# File 'lib/opener/webservice.rb', line 96

def self.new_http_client
  client = HTTPClient.new
  client.connect_timeout = 120

  return client
end

.secret_symbolObject



382
383
384
# File 'lib/opener/webservice.rb', line 382

def self.secret_symbol
  Sinatra::Application.respond_to?(:secret)? Sinatra::Application.secret.to_sym : :secret
end

.text_processor(processor = nil) ⇒ Class

Specifies the text processor to use or returns it if no parameter is given.

Parameters:

  • processor (Class) (defaults to: nil)

Returns:

  • (Class)


119
120
121
122
123
124
125
# File 'lib/opener/webservice.rb', line 119

def self.text_processor(processor=nil)
  if processor.nil?
    return @processor
  else
    @processor = processor
  end
end

.token_symbolObject



386
387
388
# File 'lib/opener/webservice.rb', line 386

def self.token_symbol
  Sinatra::Application.respond_to?(:token)? Sinatra::Application.token.to_sym : :token
end

Instance Method Details

#/Object

Puts the text through the primary processor. This route either accepts regular POST fields or a JSON payload.

Parameters:

  • params (Hash)

    The POST parameters.



38
39
40
# File 'lib/opener/webservice.rb', line 38

get '/' do
  erb :index
end

#accepted_paramsArray

Returns:

  • (Array)


151
152
153
# File 'lib/opener/webservice.rb', line 151

def accepted_params
  self.class.accepted_params
end

#analyze(options) ⇒ String, Symbol

Gets the Analyzed output of an input.

Parameters:

  • options (Hash)

    The options for the text_processor

Returns:

  • (String)

    output the output of the text_processor

  • (Symbol)

    type the output type ot the text_processor

Raises:

  • RunetimeError Raised when the tagging process failed.



224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/opener/webservice.rb', line 224

def analyze(options)
  processor             = text_processor.new(options)
  output, error, status = processor.run(options[:input])

  if processor.respond_to?(:output_type)
    type = processor.output_type
  else
    type = :xml
  end

  raise(error) if !status.nil? && !status.success?

  return output, type
end

#analyze_async(options, request_id, callbacks, error_callback = nil) ⇒ Object

Gets the NER of a KAF document and submits it to a callback URL.

Parameters:

  • options (Hash)
  • request_id (String)
  • callbacks (Array)
  • error_callback (String) (defaults to: nil)


247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/opener/webservice.rb', line 247

def analyze_async(options, request_id, callbacks, error_callback = nil)
  begin
    output, _ = analyze(options)
  rescue => error
    logger.error("Failed to process input: #{error.inspect}")

    submit_error(error_callback, error.message) if error_callback
  end

  url = callbacks.shift

  logger.info("Submitting results to #{url}")

  begin
    process_callback(options, url, output, request_id, callbacks, error_callback)
  rescue => error
    logger.error("Failed to submit the results: #{error.inspect}")

    submit_error(error_callback, error.message) if error_callback
  end
end

#authenticate!Object



345
346
347
348
349
350
351
352
# File 'lib/opener/webservice.rb', line 345

def authenticate!
  credentials = {
    secret_symbol => params[secret_symbol.to_s],
    token_symbol => params[token_symbol.to_s]
  }
  response = http_client.get(Sinatra::Application.authentication, credentials)
  halt response.body unless response.ok?
end

#callback_handlerObject

See Also:



341
342
343
# File 'lib/opener/webservice.rb', line 341

def callback_handler
  return self.class.callback_handler
end

#extract_callbacks(input) ⇒ Array

Returns an Array containing the callback URLs, ignoring empty values.

Parameters:

  • input (Array|String)

Returns:

  • (Array)


316
317
318
319
320
321
322
# File 'lib/opener/webservice.rb', line 316

def extract_callbacks(input)
  return [] if input.nil? || input.empty?

  callbacks = input.compact.reject(&:empty?)

  return callbacks
end

#extract_paramsObject



374
375
376
377
378
379
380
# File 'lib/opener/webservice.rb', line 374

def extract_params
  if request.referrer
    uri = URI.parse(request.referrer)
    extracted = Rack::Utils.parse_nested_query(uri.query)
    params.merge!(extracted)
  end
end

#filtered_params(input_params) ⇒ Hash

Filter the params hash based on the accepted_params

Parameters:

  • input_params (Hash)

Returns:

  • (Hash)


173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/opener/webservice.rb', line 173

def filtered_params(input_params)
  options = input_params.select{|k,v| accepted_params.include?(k.to_sym)}
  cleaned = {}

  options.each do |k, v|
    v = true  if v == "true"
    v = false if v == "false"

    cleaned[k.to_sym] = v
  end

  return cleaned
end

#get_input(params) ⇒ String

Returns the KAF/text input as a String.

Parameters:

  • params (Hash)

Returns:

  • (String)


404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/opener/webservice.rb', line 404

def get_input(params)
  input = nil

  if params[:input]
    input = params[:input]

  elsif params[:input_url]
    resp  = HTTPClient.get(params[:input_url], :follow_redirect => true)
    input = resp.body if resp.ok?
  end

  return input
end

#get_input_paramsHash

Returns a Hash containing the input parameters to use. If a JSON payload is submitted the parameters will be based on the payload.

Returns:

  • (Hash)


360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/opener/webservice.rb', line 360

def get_input_params
  input = {}

  if request.content_type == 'application/json'
    JSON.load(request.body).each do |key, value|
      input[key.to_sym] = value
    end
  else
    input = params
  end

  return input
end

#get_request_idString

Returns:

  • (String)


327
328
329
# File 'lib/opener/webservice.rb', line 327

def get_request_id
  return params[:request_id] || UUIDTools::UUID.random_create
end

#http_clientObject

See Also:



334
335
336
# File 'lib/opener/webservice.rb', line 334

def http_client
  return self.class.http_client
end

#process_async(input_params, callbacks, error_callback) ⇒ Object

Processes the request asynchronously.

Parameters:

  • input_params (Hash)
  • callbacks (Array)

    The callback URLs to use.

  • error_callback (String)


194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/opener/webservice.rb', line 194

def process_async(input_params, callbacks, error_callback)
  request_id = get_request_id
  output_url = callbacks.last

  Thread.new do
    analyze_async(
      filtered_params(input_params),
      request_id,
      callbacks,
      error_callback
    )
  end

  content_type :json

  {
    :request_id => request_id.to_s,
    :output_url => [output_url, request_id].join("/")
  }.to_json
end

#process_callback(options, url, text, request_id, callbacks, error_callback) ⇒ Object

Parameters:

  • options (Hash)
  • url (String)
  • text (String)
  • request_id (String)
  • callbacks (Array)


276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/opener/webservice.rb', line 276

def process_callback(options, url, text, request_id, callbacks, error_callback)
  if request.content_type == 'application/json'
    headers = {'Content-Type' => 'application/json'}
    output  = JSON.dump(
      filtered_params(options).merge(
        :input          => text,
        :request_id     => request_id,
        :callbacks      => callbacks,
        :error_callback => error_callback
      )
    )
  else
    headers = {}
    output  = filtered_params(options).merge(
      :input          => text,
      :request_id     => request_id,
      :'callbacks[]'  => callbacks,
      :error_callback => error_callback
    )
  end

  extract_params

  callback_handler.post(url, :body => output, :header => headers)
end

#process_sync(input_params) ⇒ Object

Processes the request synchronously.

Parameters:

  • input_params (Hash)


160
161
162
163
164
165
# File 'lib/opener/webservice.rb', line 160

def process_sync(input_params)
  output, type = analyze(filtered_params(input_params))
  content_type(type)

  body(output)
end

#secret_symbolObject



390
391
392
# File 'lib/opener/webservice.rb', line 390

def secret_symbol
  return self.class.secret_symbol
end

#submit_error(url, message) ⇒ Object

Parameters:

  • url (String)
  • message (String)


306
307
308
# File 'lib/opener/webservice.rb', line 306

def submit_error(url, message)
  callback_handler.post(url, :body => {:error => message})
end

#text_processorClass

Returns:

  • (Class)


144
145
146
# File 'lib/opener/webservice.rb', line 144

def text_processor
  self.class.text_processor
end

#token_symbolObject



394
395
396
# File 'lib/opener/webservice.rb', line 394

def token_symbol
  return self.class.token_symbol
end