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 =
"1.0.1"
Class Method Summary collapse
-
.accepted_params(*array) ⇒ Object
Specifies what parameters are accepted.
- .http_client ⇒ HTTPClient
- .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
-
#extract_callbacks(input) ⇒ Array
Returns an Array containing the callback URLs, ignoring empty values.
- #extract_params ⇒ Object
-
#filtered_params ⇒ Hash
Filter the params hash based on the accepted_params.
- #get_request_id ⇒ String
- #http_client ⇒ Object
-
#process_async(callbacks, error_callback) ⇒ Object
Processes the request asynchronously.
- #process_callback(url, text, request_id, callbacks, error_callback) ⇒ Object
-
#process_sync ⇒ 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.
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_client ⇒ HTTPClient
73 74 75 |
# File 'lib/opener/webservice.rb', line 73 def self.http_client return @http_client || new_http_client end |
.new_http_client ⇒ 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_symbol ⇒ Object
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.
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_symbol ⇒ Object
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
37 38 39 |
# File 'lib/opener/webservice.rb', line 37 get '/' do erb :index end |
#accepted_params ⇒ 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.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/opener/webservice.rb', line 185 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.
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(, 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
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.
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_params ⇒ Object
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_params ⇒ Hash
Filter the params hash based on the accepted_params
144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/opener/webservice.rb', line 144 def filtered_params = params.select{|k,v| accepted_params.include?(k.to_sym)} cleaned = {} .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_id ⇒ 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_client ⇒ Object
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.
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
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_sync ⇒ Object
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_symbol ⇒ Object
318 319 320 |
# File 'lib/opener/webservice.rb', line 318 def secret_symbol return self.class.secret_symbol end |
#submit_error(url, message) ⇒ Object
261 262 263 |
# File 'lib/opener/webservice.rb', line 261 def submit_error(url, ) http_client.post_async(url, :body => {:error => }) end |
#text_processor ⇒ Class
119 120 121 |
# File 'lib/opener/webservice.rb', line 119 def text_processor self.class.text_processor end |
#token_symbol ⇒ Object
322 323 324 |
# File 'lib/opener/webservice.rb', line 322 def token_symbol return self.class.token_symbol end |