Class: Opener::Webservice
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- Opener::Webservice
- 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
-
.accepted_params(*array) ⇒ Object
Specifies what parameters are accepted.
- .callback_handler ⇒ Opener::CallbackHandler
- .http_client ⇒ HTTPClient
- .new_callback_handler ⇒ Opener::CallbackHandler
- .new_http_client ⇒ HTTPClient
- .secret_symbol ⇒ Object
-
.text_processor(processor = nil) ⇒ Class
Specifies the text processor to use or returns it if no parameter is given.
- .token_symbol ⇒ Object
Instance Method Summary collapse
-
#/ ⇒ Object
Puts the text through the primary processor.
- #accepted_params ⇒ Array
-
#analyze(options) ⇒ String, Symbol
Gets the Analyzed output of an input.
-
#analyze_async(options, request_id, callbacks, error_callback = nil) ⇒ Object
Gets the NER of a KAF document and submits it to a callback URL.
- #authenticate! ⇒ Object
- #callback_handler ⇒ Object
-
#extract_callbacks(input) ⇒ Array
Returns an Array containing the callback URLs, ignoring empty values.
- #extract_params ⇒ Object
-
#filtered_params(input_params) ⇒ Hash
Filter the params hash based on the accepted_params.
-
#get_input(params) ⇒ String
Returns the KAF/text input as a String.
-
#get_input_params ⇒ Hash
Returns a Hash containing the input parameters to use.
- #get_request_id ⇒ String
- #http_client ⇒ Object
-
#process_async(input_params, callbacks, error_callback) ⇒ Object
Processes the request asynchronously.
- #process_callback(options, url, text, request_id, callbacks, error_callback) ⇒ Object
-
#process_sync(input_params) ⇒ Object
Processes the request synchronously.
- #secret_symbol ⇒ Object
- #submit_error(url, message) ⇒ Object
- #text_processor ⇒ Class
- #token_symbol ⇒ Object
Class Method Details
.accepted_params(*array) ⇒ Object
Specifies what parameters are accepted.
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_handler ⇒ Opener::CallbackHandler
89 90 91 |
# File 'lib/opener/webservice.rb', line 89 def self.callback_handler return @callback_handler || new_callback_handler end |
.http_client ⇒ HTTPClient
82 83 84 |
# File 'lib/opener/webservice.rb', line 82 def self.http_client return @http_client || new_http_client end |
.new_callback_handler ⇒ 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_client ⇒ 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_symbol ⇒ Object
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.
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_symbol ⇒ Object
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.
38 39 40 |
# File 'lib/opener/webservice.rb', line 38 get '/' do erb :index end |
#accepted_params ⇒ 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.
224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/opener/webservice.rb', line 224 def analyze() processor = text_processor.new() output, error, status = processor.run([: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.
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(, request_id, callbacks, error_callback = nil) begin output, _ = analyze() rescue => error logger.error("Failed to process input: #{error.inspect}") submit_error(error_callback, error.) if error_callback end url = callbacks.shift logger.info("Submitting results to #{url}") begin process_callback(, url, output, request_id, callbacks, error_callback) rescue => error logger.error("Failed to submit the results: #{error.inspect}") submit_error(error_callback, error.) 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_handler ⇒ Object
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.
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_params ⇒ Object
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
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) = input_params.select{|k,v| accepted_params.include?(k.to_sym)} cleaned = {} .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.
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_params ⇒ Hash
Returns a Hash containing the input parameters to use. If a JSON payload is submitted the parameters will be based on the payload.
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_id ⇒ 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_client ⇒ Object
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.
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
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(, url, text, request_id, callbacks, error_callback) if request.content_type == 'application/json' headers = {'Content-Type' => 'application/json'} output = JSON.dump( filtered_params().merge( :input => text, :request_id => request_id, :callbacks => callbacks, :error_callback => error_callback ) ) else headers = {} output = filtered_params().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.
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_symbol ⇒ Object
390 391 392 |
# File 'lib/opener/webservice.rb', line 390 def secret_symbol return self.class.secret_symbol end |
#submit_error(url, message) ⇒ Object
306 307 308 |
# File 'lib/opener/webservice.rb', line 306 def submit_error(url, ) callback_handler.post(url, :body => {:error => }) end |
#text_processor ⇒ Class
144 145 146 |
# File 'lib/opener/webservice.rb', line 144 def text_processor self.class.text_processor end |
#token_symbol ⇒ Object
394 395 396 |
# File 'lib/opener/webservice.rb', line 394 def token_symbol return self.class.token_symbol end |