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 =
"1.0.1"

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.



107
108
109
110
111
112
113
114
# File 'lib/opener/webservice.rb', line 107

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

.http_clientHTTPClient

Returns:

  • (HTTPClient)


73
74
75
# File 'lib/opener/webservice.rb', line 73

def self.http_client
  return @http_client || new_http_client
end

.new_http_clientHTTPClient

Returns:

  • (HTTPClient)


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

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

  return client
end

.secret_symbolObject



310
311
312
# File 'lib/opener/webservice.rb', line 310

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)


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

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

.token_symbolObject



314
315
316
# File 'lib/opener/webservice.rb', line 314

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

Parameters:

  • params (Hash)

    The POST parameters.



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

get '/' do
  erb :index
end

#accepted_paramsArray

Returns:

  • (Array)


126
127
128
# File 'lib/opener/webservice.rb', line 126

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.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/opener/webservice.rb', line 185

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:

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


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/opener/webservice.rb', line 208

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(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



293
294
295
296
297
298
299
300
# File 'lib/opener/webservice.rb', line 293

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

#extract_callbacks(input) ⇒ Array

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

Parameters:

  • input (Array|String)

Returns:

  • (Array)


271
272
273
274
275
276
277
# File 'lib/opener/webservice.rb', line 271

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

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

  return callbacks
end

#extract_paramsObject



302
303
304
305
306
307
308
# File 'lib/opener/webservice.rb', line 302

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_paramsHash

Filter the params hash based on the accepted_params

Returns:

  • (Hash)

    accepted params



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/opener/webservice.rb', line 144

def filtered_params
  options = params.select{|k,v| accepted_params.include?(k.to_sym)}
  cleaned = {}
  options.each_pair do |k, v|
    v = true  if v == "true"
    v = false if v == "false"
    cleaned[k.to_sym] = v
  end

  return cleaned
end

#get_request_idString

Returns:

  • (String)


282
283
284
# File 'lib/opener/webservice.rb', line 282

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

#http_clientObject

See Also:



289
290
291
# File 'lib/opener/webservice.rb', line 289

def http_client
  return self.class.http_client
end

#process_async(callbacks, error_callback) ⇒ Object

Processes the request asynchronously.

Parameters:

  • callbacks (Array)

    The callback URLs to use.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/opener/webservice.rb', line 161

def process_async(callbacks, error_callback)
  request_id = get_request_id
  output_url = callbacks.last
  Thread.new do
    analyze_async(filtered_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(url, text, request_id, callbacks, error_callback) ⇒ Object

Parameters:

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


236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/opener/webservice.rb', line 236

def process_callback(url, text, request_id, callbacks, error_callback)
  # FIXME: this is a bit of a hack to prevent the webservice from clogging
  # Airbrake during the hackathon. For whatever reason somebody is posting
  # internal server errors from *somewhere*. Validation? What's that?
  return if text =~ /^internal server error/i
  
  output = {
    :input          => text,
    :request_id     => request_id,
    :'callbacks[]'  => callbacks,
    :error_callback => error_callback
  }
  
  extract_params
  
  http_client.post_async(
    url,
    :body => filtered_params.merge(output)
  )
end

#process_syncObject

Processes the request synchronously.



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

def process_sync
  output, type = analyze(filtered_params)
  content_type(type)
  body(output)
end

#secret_symbolObject



318
319
320
# File 'lib/opener/webservice.rb', line 318

def secret_symbol
  return self.class.secret_symbol
end

#submit_error(url, message) ⇒ Object

Parameters:

  • url (String)
  • message (String)


261
262
263
# File 'lib/opener/webservice.rb', line 261

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

#text_processorClass

Returns:

  • (Class)


119
120
121
# File 'lib/opener/webservice.rb', line 119

def text_processor
  self.class.text_processor
end

#token_symbolObject



322
323
324
# File 'lib/opener/webservice.rb', line 322

def token_symbol
  return self.class.token_symbol
end