opener-webservice 1.2.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 573f4773532c5dec1df14ada6c020e8dd97faff9
4
- data.tar.gz: 1b7738d6bebb403d45f3f4f281f1bd32e5a82783
3
+ metadata.gz: 02a6433aa01c06b1251256710c706a59e45586b2
4
+ data.tar.gz: 6dc2b0270835171445461234f8123fb13e267d36
5
5
  SHA512:
6
- metadata.gz: cbcf4d076901ff9f275548955b984923a9ecc69a07c55a65d485f343f62d0b0e5dc68bfc003a18be62de2e1951e55e8bb07d6f5f2dc956e500cdeb39a8a808af
7
- data.tar.gz: c4a58ae8772825cc65672601ceb3056d747b7ea4daa9f284d936e101b64277cf06feaeb9931c7ad5c6e48236313de6e35f8339a443a8ed0c431059c4923b54f9
6
+ metadata.gz: 733000a6cd389bcfbdfd0865432cc45c4896d4564a653fa7ad5a14a123d1cf5eb28ab0202e13fa47daa4e5f2b4a64f88f753abf3046d71fc11d7198329468c02
7
+ data.tar.gz: cfd032408fe8d59f391577d029eb96880684eb93ff61f4920f6264ac9ac214bed9d869b52b81d612c95ac386f552e43d8c5055e2024aa82e52a4597a778285fc
@@ -2,6 +2,6 @@ require 'sinatra/base'
2
2
 
3
3
  module Opener
4
4
  class Webservice < Sinatra::Base
5
- VERSION = "1.2.0"
5
+ VERSION = "2.0.0"
6
6
  end
7
7
  end
@@ -25,7 +25,7 @@ module Opener
25
25
  set :raise_errors, true
26
26
  set :dump_errors, true
27
27
  end
28
-
28
+
29
29
  before %r{^((?!.css|.jpg|.png|.js|.ico).)+$} do
30
30
  extract_params
31
31
  authenticate! if Sinatra::Application.respond_to?(:authentication)
@@ -40,44 +40,49 @@ module Opener
40
40
  end
41
41
 
42
42
  ##
43
- # Puts the text through the primary processor
43
+ # Puts the text through the primary processor. This route either accepts
44
+ # regular POST fields or a JSON payload.
44
45
  #
45
46
  # @param [Hash] params The POST parameters.
46
47
  #
47
48
  # @option params [String] :input the input to send to the processor
49
+ #
48
50
  # @option params [Array<String>] :callbacks A collection of callback URLs
49
51
  # that act as a chain. The results are posted to the first URL which is
50
52
  # then shifted of the list.
53
+ #
51
54
  # @option params [String] :error_callback Callback URL to send errors to
52
55
  # when using the asynchronous setup.
53
56
  #
54
57
  post '/' do
55
- input = get_input(params)
56
-
58
+ input_params = get_input_params
59
+ input = get_input(input_params)
60
+
57
61
  if !input or input.strip.empty?
58
62
  logger.error('Failed to process the request: no input specified')
59
63
 
60
64
  halt(400, 'No input specified')
61
65
  end
62
-
63
- params[:input] = input
64
- callbacks = extract_callbacks(params[:callbacks])
65
- error_callback = params[:error_callback]
66
+
67
+ input_params[:input] = input
68
+
69
+ callbacks = extract_callbacks(input_params[:callbacks])
70
+ error_callback = input_params[:error_callback]
66
71
 
67
72
  if callbacks.empty?
68
- process_sync
73
+ process_sync(input_params)
69
74
  else
70
- process_async(callbacks, error_callback)
75
+ process_async(input_params, callbacks, error_callback)
71
76
  end
72
77
  end
73
-
78
+
74
79
  ##
75
80
  # @return [HTTPClient]
76
81
  #
77
82
  def self.http_client
78
83
  return @http_client || new_http_client
79
84
  end
80
-
85
+
81
86
  ##
82
87
  # @return [Opener::CallbackHandler]
83
88
  #
@@ -94,7 +99,7 @@ module Opener
94
99
 
95
100
  return client
96
101
  end
97
-
102
+
98
103
  ##
99
104
  # @return [Opener::CallbackHandler]
100
105
  #
@@ -150,23 +155,29 @@ module Opener
150
155
  ##
151
156
  # Processes the request synchronously.
152
157
  #
153
- def process_sync
154
- output, type = analyze(filtered_params)
158
+ # @param [Hash] input_params
159
+ #
160
+ def process_sync(input_params)
161
+ output, type = analyze(filtered_params(input_params))
155
162
  content_type(type)
163
+
156
164
  body(output)
157
165
  end
158
166
 
159
167
  ##
160
168
  # Filter the params hash based on the accepted_params
161
169
  #
162
- # @return [Hash] accepted params
170
+ # @param [Hash] input_params
171
+ # @return [Hash]
163
172
  #
164
- def filtered_params
165
- options = params.select{|k,v| accepted_params.include?(k.to_sym)}
173
+ def filtered_params(input_params)
174
+ options = input_params.select{|k,v| accepted_params.include?(k.to_sym)}
166
175
  cleaned = {}
167
- options.each_pair do |k, v|
176
+
177
+ options.each do |k, v|
168
178
  v = true if v == "true"
169
179
  v = false if v == "false"
180
+
170
181
  cleaned[k.to_sym] = v
171
182
  end
172
183
 
@@ -176,13 +187,21 @@ module Opener
176
187
  ##
177
188
  # Processes the request asynchronously.
178
189
  #
190
+ # @param [Hash] input_params
179
191
  # @param [Array] callbacks The callback URLs to use.
192
+ # @param [String] error_callback
180
193
  #
181
- def process_async(callbacks, error_callback)
194
+ def process_async(input_params, callbacks, error_callback)
182
195
  request_id = get_request_id
183
196
  output_url = callbacks.last
197
+
184
198
  Thread.new do
185
- analyze_async(filtered_params, request_id, callbacks, error_callback)
199
+ analyze_async(
200
+ filtered_params(input_params),
201
+ request_id,
202
+ callbacks,
203
+ error_callback
204
+ )
186
205
  end
187
206
 
188
207
  content_type :json
@@ -220,7 +239,7 @@ module Opener
220
239
  ##
221
240
  # Gets the NER of a KAF document and submits it to a callback URL.
222
241
  #
223
- # @param [String] text
242
+ # @param [Hash] options
224
243
  # @param [String] request_id
225
244
  # @param [Array] callbacks
226
245
  # @param [String] error_callback
@@ -239,7 +258,7 @@ module Opener
239
258
  logger.info("Submitting results to #{url}")
240
259
 
241
260
  begin
242
- process_callback(url, output, request_id, callbacks, error_callback)
261
+ process_callback(options, url, output, request_id, callbacks, error_callback)
243
262
  rescue => error
244
263
  logger.error("Failed to submit the results: #{error.inspect}")
245
264
 
@@ -248,30 +267,36 @@ module Opener
248
267
  end
249
268
 
250
269
  ##
270
+ # @param [Hash] options
251
271
  # @param [String] url
252
272
  # @param [String] text
253
273
  # @param [String] request_id
254
274
  # @param [Array] callbacks
255
275
  #
256
- def process_callback(url, text, request_id, callbacks, error_callback)
257
- # FIXME: this is a bit of a hack to prevent the webservice from clogging
258
- # Airbrake during the hackathon. For whatever reason somebody is posting
259
- # internal server errors from *somewhere*. Validation? What's that?
260
- return if text =~ /^internal server error/i
261
-
262
- output = {
263
- :input => text,
264
- :request_id => request_id,
265
- :'callbacks[]' => callbacks,
266
- :error_callback => error_callback
267
- }
268
-
276
+ def process_callback(options, url, text, request_id, callbacks, error_callback)
277
+ if request.content_type == 'application/json'
278
+ headers = {'Content-Type' => 'application/json'}
279
+ output = JSON.dump(
280
+ filtered_params(options).merge(
281
+ :input => text,
282
+ :request_id => request_id,
283
+ :callbacks => callbacks,
284
+ :error_callback => error_callback
285
+ )
286
+ )
287
+ else
288
+ headers = {}
289
+ output = filtered_params(options).merge(
290
+ :input => text,
291
+ :request_id => request_id,
292
+ :'callbacks[]' => callbacks,
293
+ :error_callback => error_callback
294
+ )
295
+ end
296
+
269
297
  extract_params
270
-
271
- callback_handler.post(
272
- url,
273
- :body => filtered_params.merge(output)
274
- )
298
+
299
+ callback_handler.post(url, :body => output, :header => headers)
275
300
  end
276
301
 
277
302
  ##
@@ -309,14 +334,14 @@ module Opener
309
334
  def http_client
310
335
  return self.class.http_client
311
336
  end
312
-
337
+
313
338
  ##
314
339
  # @see Opener::Webservice.callback_handler
315
340
  #
316
341
  def callback_handler
317
342
  return self.class.callback_handler
318
343
  end
319
-
344
+
320
345
  def authenticate!
321
346
  credentials = {
322
347
  secret_symbol => params[secret_symbol.to_s],
@@ -325,7 +350,27 @@ module Opener
325
350
  response = http_client.get(Sinatra::Application.authentication, credentials)
326
351
  halt response.body unless response.ok?
327
352
  end
328
-
353
+
354
+ ##
355
+ # Returns a Hash containing the input parameters to use. If a JSON payload
356
+ # is submitted the parameters will be based on the payload.
357
+ #
358
+ # @return [Hash]
359
+ #
360
+ def get_input_params
361
+ input = {}
362
+
363
+ if request.content_type == 'application/json'
364
+ JSON.load(request.body).each do |key, value|
365
+ input[key.to_sym] = value
366
+ end
367
+ else
368
+ input = params
369
+ end
370
+
371
+ return input
372
+ end
373
+
329
374
  def extract_params
330
375
  if request.referrer
331
376
  uri = URI.parse(request.referrer)
@@ -333,26 +378,41 @@ module Opener
333
378
  params.merge!(extracted)
334
379
  end
335
380
  end
336
-
381
+
337
382
  def self.secret_symbol
338
- Sinatra::Application.respond_to?(:secret)? Sinatra::Application.secret.to_sym : :secret
383
+ Sinatra::Application.respond_to?(:secret)? Sinatra::Application.secret.to_sym : :secret
339
384
  end
340
-
385
+
341
386
  def self.token_symbol
342
- Sinatra::Application.respond_to?(:token)? Sinatra::Application.token.to_sym : :token
387
+ Sinatra::Application.respond_to?(:token)? Sinatra::Application.token.to_sym : :token
343
388
  end
344
-
389
+
345
390
  def secret_symbol
346
391
  return self.class.secret_symbol
347
392
  end
348
-
393
+
349
394
  def token_symbol
350
395
  return self.class.token_symbol
351
396
  end
352
-
397
+
398
+ ##
399
+ # Returns the KAF/text input as a String.
400
+ #
401
+ # @param [Hash] params
402
+ # @return [String]
403
+ #
353
404
  def get_input(params)
354
- return params[:input] if params[:input]
355
- return HTTPClient.new.get(params[:input_url]).body if params[:input_url]
405
+ input = nil
406
+
407
+ if params[:input]
408
+ input = params[:input]
409
+
410
+ elsif params[:input_url]
411
+ resp = HTTPClient.get(params[:input_url], :follow_redirect => true)
412
+ input = resp.body if resp.ok?
413
+ end
414
+
415
+ return input
356
416
  end
357
417
  end
358
418
  end
@@ -16,12 +16,13 @@ Gem::Specification.new do |spec|
16
16
  'LICENSE.txt'
17
17
  ]).select { |file| File.file?(file) }
18
18
 
19
- spec.add_development_dependency "bundler", "~> 1.3"
20
- spec.add_development_dependency "rake"
21
- spec.add_development_dependency "pry"
22
-
23
19
  spec.add_dependency "sinatra", "~> 1.4.3"
24
20
  spec.add_dependency "uuidtools"
25
21
  spec.add_dependency "json"
26
- spec.add_dependency "opener-callback-handler"
22
+ spec.add_dependency "opener-callback-handler", '~> 1.0'
23
+ spec.add_dependency 'httpclient', ['~> 2.0', '>= 2.5.3.3']
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "pry"
27
28
  end
metadata CHANGED
@@ -1,147 +1,168 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opener-webservice
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - development@olery.com
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-19 00:00:00.000000000 Z
11
+ date: 2014-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: sinatra
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.3
20
+ type: :runtime
21
+ prerelease: false
15
22
  version_requirements: !ruby/object:Gem::Requirement
16
23
  requirements:
17
- - - ~>
24
+ - - "~>"
18
25
  - !ruby/object:Gem::Version
19
- version: '1.3'
26
+ version: 1.4.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: uuidtools
20
29
  requirement: !ruby/object:Gem::Requirement
21
30
  requirements:
22
- - - ~>
31
+ - - ">="
23
32
  - !ruby/object:Gem::Version
24
- version: '1.3'
33
+ version: '0'
34
+ type: :runtime
25
35
  prerelease: false
26
- type: :development
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
36
  version_requirements: !ruby/object:Gem::Requirement
30
37
  requirements:
31
- - - '>='
38
+ - - ">="
32
39
  - !ruby/object:Gem::Version
33
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
34
43
  requirement: !ruby/object:Gem::Requirement
35
44
  requirements:
36
- - - '>='
45
+ - - ">="
37
46
  - !ruby/object:Gem::Version
38
47
  version: '0'
48
+ type: :runtime
39
49
  prerelease: false
40
- type: :development
41
- - !ruby/object:Gem::Dependency
42
- name: pry
43
50
  version_requirements: !ruby/object:Gem::Requirement
44
51
  requirements:
45
- - - '>='
52
+ - - ">="
46
53
  - !ruby/object:Gem::Version
47
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: opener-callback-handler
48
57
  requirement: !ruby/object:Gem::Requirement
49
58
  requirements:
50
- - - '>='
59
+ - - "~>"
51
60
  - !ruby/object:Gem::Version
52
- version: '0'
61
+ version: '1.0'
62
+ type: :runtime
53
63
  prerelease: false
54
- type: :development
55
- - !ruby/object:Gem::Dependency
56
- name: sinatra
57
64
  version_requirements: !ruby/object:Gem::Requirement
58
65
  requirements:
59
- - - ~>
66
+ - - "~>"
60
67
  - !ruby/object:Gem::Version
61
- version: 1.4.3
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: httpclient
62
71
  requirement: !ruby/object:Gem::Requirement
63
72
  requirements:
64
- - - ~>
73
+ - - "~>"
65
74
  - !ruby/object:Gem::Version
66
- version: 1.4.3
67
- prerelease: false
75
+ version: '2.0'
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 2.5.3.3
68
79
  type: :runtime
69
- - !ruby/object:Gem::Dependency
70
- name: uuidtools
80
+ prerelease: false
71
81
  version_requirements: !ruby/object:Gem::Requirement
72
82
  requirements:
73
- - - '>='
83
+ - - "~>"
74
84
  - !ruby/object:Gem::Version
75
- version: '0'
85
+ version: '2.0'
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 2.5.3.3
89
+ - !ruby/object:Gem::Dependency
90
+ name: bundler
76
91
  requirement: !ruby/object:Gem::Requirement
77
92
  requirements:
78
- - - '>='
93
+ - - "~>"
79
94
  - !ruby/object:Gem::Version
80
- version: '0'
95
+ version: '1.3'
96
+ type: :development
81
97
  prerelease: false
82
- type: :runtime
83
- - !ruby/object:Gem::Dependency
84
- name: json
85
98
  version_requirements: !ruby/object:Gem::Requirement
86
99
  requirements:
87
- - - '>='
100
+ - - "~>"
88
101
  - !ruby/object:Gem::Version
89
- version: '0'
102
+ version: '1.3'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rake
90
105
  requirement: !ruby/object:Gem::Requirement
91
106
  requirements:
92
- - - '>='
107
+ - - ">="
93
108
  - !ruby/object:Gem::Version
94
109
  version: '0'
110
+ type: :development
95
111
  prerelease: false
96
- type: :runtime
97
- - !ruby/object:Gem::Dependency
98
- name: opener-callback-handler
99
112
  version_requirements: !ruby/object:Gem::Requirement
100
113
  requirements:
101
- - - '>='
114
+ - - ">="
102
115
  - !ruby/object:Gem::Version
103
116
  version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: pry
104
119
  requirement: !ruby/object:Gem::Requirement
105
120
  requirements:
106
- - - '>='
121
+ - - ">="
107
122
  - !ruby/object:Gem::Version
108
123
  version: '0'
124
+ type: :development
109
125
  prerelease: false
110
- type: :runtime
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
111
131
  description: Basic webservice hooks for the opener toolchain
112
- email:
132
+ email:
113
133
  executables: []
114
134
  extensions: []
115
135
  extra_rdoc_files: []
116
136
  files:
137
+ - LICENSE.txt
138
+ - README.md
117
139
  - lib/opener/webservice.rb
118
140
  - lib/opener/webservice/opt_parser.rb
119
141
  - lib/opener/webservice/version.rb
120
142
  - opener-webservice.gemspec
121
- - README.md
122
- - LICENSE.txt
123
- homepage:
143
+ homepage:
124
144
  licenses:
125
145
  - Apache 2.0
126
146
  metadata: {}
127
- post_install_message:
147
+ post_install_message:
128
148
  rdoc_options: []
129
149
  require_paths:
130
150
  - lib
131
151
  required_ruby_version: !ruby/object:Gem::Requirement
132
152
  requirements:
133
- - - '>='
153
+ - - ">="
134
154
  - !ruby/object:Gem::Version
135
155
  version: '0'
136
156
  required_rubygems_version: !ruby/object:Gem::Requirement
137
157
  requirements:
138
- - - '>='
158
+ - - ">="
139
159
  - !ruby/object:Gem::Version
140
160
  version: '0'
141
161
  requirements: []
142
- rubyforge_project:
143
- rubygems_version: 2.1.9
144
- signing_key:
162
+ rubyforge_project:
163
+ rubygems_version: 2.2.2
164
+ signing_key:
145
165
  specification_version: 4
146
166
  summary: Basic webservice hooks for the opener toolchain
147
167
  test_files: []
168
+ has_rdoc: