rosette_api 1.1.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a933956e30bc2d5f25da1185986b4d15213ed8c6
4
- data.tar.gz: d15ced2b62c5bf6c120fba6751e6bdd2b5719d0a
3
+ metadata.gz: 4104193d91e0db035f76f3b5662d6d0d96ba6094
4
+ data.tar.gz: eedfda26a8b17f548159297e2ec8e40ec5b921d9
5
5
  SHA512:
6
- metadata.gz: 4ec8d75ea02529e81810389c1f2007c070118b063596394940bd594c4b021edbb477f2e9b4ead8359fa82ca370f7c4f3070fab017f434848ca26eb5f65a6ec11
7
- data.tar.gz: dc1bf7cac6789d0fe273a2081e8b53440037a2b55abfadf69df0f3a8857ae922d3e99b6c5d4f8cb85beca86c7d38e589a997da3258a60e04a0d9c8c32dd95186
6
+ metadata.gz: 34165ed9c5648635851d61fd922e6fb5cb512371e5bc85ef430f48f7a199f7b445d3fba0c3c02d931971a3c401e66c0ca008d2086686edc382477bfc55e2b8b9
7
+ data.tar.gz: 677d79b77789d0463fad837d1407f0561c537fa784e71e0183020b2e6b28f3534d310bcef99d1030c1df441a2149ed8ba4feb299e707bfa2e097c026a2767780
File without changes
File without changes
@@ -15,6 +15,8 @@ class DocumentParameters
15
15
  attr_accessor :language
16
16
  # Rosette API options (optional, should be a hash)
17
17
  attr_accessor :rosette_options
18
+ # custom Rosette API headers
19
+ attr_accessor :custom_headers
18
20
 
19
21
  def initialize(options = {}) #:notnew:
20
22
  options = {
@@ -23,7 +25,8 @@ class DocumentParameters
23
25
  file_path: nil,
24
26
  genre: nil,
25
27
  language: nil,
26
- rosette_options: nil
28
+ rosette_options: nil,
29
+ custom_headers: nil
27
30
  }.update options
28
31
  @content = options[:content]
29
32
  @content_uri = options[:content_uri]
@@ -31,6 +34,7 @@ class DocumentParameters
31
34
  @genre = options[:genre]
32
35
  @language = options[:language]
33
36
  @rosette_options = options[:rosette_options]
37
+ @custom_headers = options[:custom_headers]
34
38
  end
35
39
 
36
40
  # Validates the parameters by checking if there are multiple content sources
@@ -43,6 +47,8 @@ class DocumentParameters
43
47
  elsif [@content, @content_uri, @file_path].all?(&:nil?)
44
48
  raise BadRequestFormatError.new 'The format of the request is invalid: no content provided; must' \
45
49
  ' be one of an attachment, an inline "content" field, or an external "contentUri"'
50
+ elsif !@rosette_options.nil?
51
+ raise BadRequestError.new('rosette_options can only be an instance of a Hash') unless @rosette_options.is_a? Hash
46
52
  end
47
53
  end
48
54
 
@@ -66,7 +72,8 @@ class DocumentParameters
66
72
  file_path: @file_path,
67
73
  genre: @genre,
68
74
  language: @language,
69
- options: @rosette_options
75
+ options: @rosette_options,
76
+ custom_headers: @custom_headers
70
77
  }
71
78
  end
72
79
  end
File without changes
@@ -21,6 +21,7 @@ class NameSimilarityParameters
21
21
  @genre = options[:genre]
22
22
  @name1 = name1
23
23
  @name2 = name2
24
+ @rosette_options = options[:rosette_options]
24
25
  end
25
26
 
26
27
  # Validates the parameters by checking if name1 and name2 are instances of
@@ -30,6 +31,8 @@ class NameSimilarityParameters
30
31
  raise BadRequestError.new('name1 option can only be an instance of a String or NameParameter')
31
32
  elsif [String, NameParameter].none? { |clazz| @name2.is_a? clazz }
32
33
  raise BadRequestError.new('name2 option can only be an instance of a String or NameParameter')
34
+ elsif !@rosette_options.nil?
35
+ raise BadRequestError.new('rosette_options can only be an instance of a Hash') unless @rosette_options.is_a? Hash
33
36
  end
34
37
  end
35
38
 
@@ -47,10 +47,18 @@ class NameTranslationParameters
47
47
  @target_script = options[:target_script]
48
48
  end
49
49
 
50
+ # Validates the parameters by checking if rosette_options is an instance of a Hash.
51
+ def validate_params
52
+ if !@rosette_options.nil?
53
+ raise BadRequestError.new('rosette_options can only be an instance of a Hash') unless @rosette_options.is_a? Hash
54
+ end
55
+ end
56
+
50
57
  # Converts this class to Hash with its keys in lower CamelCase.
51
58
  #
52
59
  # Returns the new Hash.
53
60
  def load_params
61
+ self.validate_params
54
62
  self.to_hash.select { |_key, value| !value.nil? }
55
63
  .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
56
64
  .to_h
@@ -41,6 +41,18 @@ class RequestBuilder
41
41
  raise RosetteAPIError.new 'connectionError', 'Failed to establish connection with Rosette API server.'
42
42
  end
43
43
 
44
+ if params['customHeaders'] != nil
45
+ keys_array = params['customHeaders'].keys
46
+ for k in keys_array
47
+ if k.to_s =~ /^X-RosetteAPI-/
48
+ request[k] = params['customHeaders'][k]
49
+ else
50
+ raise RosetteAPIError.new 'invalidHeader', 'Custom header must begin with "X-RosetteAPI-"'
51
+ end
52
+ end
53
+ params.delete 'customHeaders'
54
+ end
55
+
44
56
  request['X-RosetteAPI-Key'] = @user_key
45
57
  request['Content-Type'] = 'application/json'
46
58
  request['Accept'] = 'application/json'
@@ -68,6 +80,7 @@ class RequestBuilder
68
80
 
69
81
  boundary = SecureRandom.hex
70
82
  post_body = []
83
+ params.delete 'filePath'
71
84
  request_file = params.to_json
72
85
 
73
86
  # Add the content data
@@ -92,6 +105,19 @@ class RequestBuilder
92
105
  rescue
93
106
  raise RosetteAPIError.new 'connectionError', 'Failed to establish connection with Rosette API server.'
94
107
  end
108
+
109
+ # add any custom headers from the user
110
+ if params['customHeaders'] != nil
111
+ keys_array = params['customHeaders'].keys
112
+ for k in keys_array
113
+ if k.to_s =~ /^X-RosetteAPI-/
114
+ request.add_field k, params['customHeaders'][k]
115
+ else
116
+ raise RosetteAPIError.new 'invalidHeader', 'Custom header must begin with "X-RosetteAPI-"'
117
+ end
118
+ end
119
+ params.delete 'customHeaders'
120
+ end
95
121
 
96
122
  request.add_field 'Content-Type', "multipart/form-data; boundary=#{boundary}"
97
123
  request.add_field 'X-RosetteAPI-Key', @user_key
data/lib/rosette_api.rb CHANGED
@@ -9,7 +9,7 @@ require_relative 'bad_request_format_error'
9
9
  # This class allows you to access all Rosette API endpoints.
10
10
  class RosetteAPI
11
11
  # Version of Ruby binding
12
- BINDING_VERSION = '1.1.1'
12
+ BINDING_VERSION = '1.2.0'
13
13
  # Rosette API language endpoint
14
14
  LANGUAGE_ENDPOINT = '/language'
15
15
  # Rosette API morphology endpoint
@@ -39,6 +39,8 @@ class RosetteAPI
39
39
  attr_accessor :user_key
40
40
  # Alternate Rosette API URL
41
41
  attr_accessor :alternate_url
42
+ # custom Rosette API headers
43
+ attr_accessor :custom_headers
42
44
 
43
45
  def initialize(user_key, alternate_url = 'https://api.rosette.com/rest/v1') #:notnew:
44
46
  @user_key = user_key
@@ -166,7 +168,6 @@ class RosetteAPI
166
168
  params = params.load_params
167
169
 
168
170
  endpoint = resolve_entities ? (ENTITIES_ENDPOINT + '/linked') : ENTITIES_ENDPOINT
169
-
170
171
  RequestBuilder.new(@user_key, @alternate_url + endpoint, params, BINDING_VERSION)
171
172
  .send_post_request
172
173
  end
@@ -181,10 +182,12 @@ class RosetteAPI
181
182
  # base.
182
183
  def get_entities_linked(params)
183
184
  warn '[DEPRECATION] `get_entities_linked` is deprecated. Please use ' \
184
- '`get_entities` instead with `resolve_entities` param set to true.'
185
+ '`get_entities` instead.'
185
186
  get_entities(params, true)
186
187
  end
187
188
 
189
+
190
+
188
191
  # Extracts Tier 1 contextual categories from the input.
189
192
  #
190
193
  # ==== Attributes
File without changes
data/tests/tests_spec.rb CHANGED
@@ -18,7 +18,7 @@ describe RosetteAPI do
18
18
  'User-Agent' => 'Ruby',
19
19
  'X-Rosetteapi-Key' => '0123456789',
20
20
  'X-Rosetteapi-Binding' => 'ruby',
21
- 'X-Rosetteapi-Binding-Version' => '1.1.0'}).
21
+ 'X-Rosetteapi-Binding-Version' => '1.1.1'}).
22
22
  to_return(status: 200, body: {'test': 'language'}.to_json, headers: {})
23
23
  end
24
24
  it 'test language' do
@@ -55,7 +55,7 @@ describe RosetteAPI do
55
55
  'User-Agent' => 'Ruby',
56
56
  'X-Rosetteapi-Key' => '0123456789',
57
57
  'X-Rosetteapi-Binding' => 'ruby',
58
- 'X-Rosetteapi-Binding-Version' => '1.1.0'}).
58
+ 'X-Rosetteapi-Binding-Version' => '1.1.1'}).
59
59
  to_return(status: 200, body: {'test': 'morphology/complete'}.to_json, headers: {})
60
60
  end
61
61
  it 'test morphology complete' do
@@ -77,7 +77,7 @@ describe RosetteAPI do
77
77
  'User-Agent' => 'Ruby',
78
78
  'X-Rosetteapi-Key' => '0123456789',
79
79
  'X-Rosetteapi-Binding' => 'ruby',
80
- 'X-Rosetteapi-Binding-Version' => '1.1.0'}).
80
+ 'X-Rosetteapi-Binding-Version' => '1.1.1'}).
81
81
  to_return(status: 200, body: {'test': 'morphology/compound-components'}.to_json, headers: {})
82
82
  end
83
83
  it 'test morphology compound components' do
@@ -99,7 +99,7 @@ describe RosetteAPI do
99
99
  'User-Agent' => 'Ruby',
100
100
  'X-Rosetteapi-Key' => '0123456789',
101
101
  'X-Rosetteapi-Binding' => 'ruby',
102
- 'X-Rosetteapi-Binding-Version' => '1.1.0'}).
102
+ 'X-Rosetteapi-Binding-Version' => '1.1.1'}).
103
103
  to_return(status: 200, body: {'test': 'morphology/han-readings'}.to_json, headers: {})
104
104
  end
105
105
  it 'test morphology han readings' do
@@ -121,7 +121,7 @@ describe RosetteAPI do
121
121
  'User-Agent' => 'Ruby',
122
122
  'X-Rosetteapi-Key' => '0123456789',
123
123
  'X-Rosetteapi-Binding' => 'ruby',
124
- 'X-Rosetteapi-Binding-Version' => '1.1.0'}).
124
+ 'X-Rosetteapi-Binding-Version' => '1.1.1'}).
125
125
  to_return(status: 200, body: {'test': 'morphology/parts-of-speech'}.to_json, headers: {})
126
126
  end
127
127
  it 'test morphology parts of speech' do
@@ -143,7 +143,7 @@ describe RosetteAPI do
143
143
  'User-Agent' => 'Ruby',
144
144
  'X-Rosetteapi-Key' => '0123456789',
145
145
  'X-Rosetteapi-Binding' => 'ruby',
146
- 'X-Rosetteapi-Binding-Version' => '1.1.0'}).
146
+ 'X-Rosetteapi-Binding-Version' => '1.1.1'}).
147
147
  to_return(status: 200, body: {'test': 'morphology/lemmas'}.to_json, headers: {})
148
148
  end
149
149
  it 'test morphology lemmas' do
@@ -165,7 +165,7 @@ describe RosetteAPI do
165
165
  'User-Agent' => 'Ruby',
166
166
  'X-Rosetteapi-Key' => '0123456789',
167
167
  'X-Rosetteapi-Binding' => 'ruby',
168
- 'X-Rosetteapi-Binding-Version' => '1.1.0'}).
168
+ 'X-Rosetteapi-Binding-Version' => '1.1.1'}).
169
169
  to_return(status: 200, body: {'test': 'entities'}.to_json, headers: {})
170
170
  end
171
171
  it 'test entities' do
@@ -177,10 +177,10 @@ describe RosetteAPI do
177
177
  end
178
178
  end
179
179
 
180
- describe '.get_entities_linked' do
181
- request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/entities_linked.json'))
180
+ describe '.get_entities_no_qids' do
181
+ request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/entities_no_qids.json'))
182
182
  before do
183
- stub_request(:post, 'https://api.rosette.com/rest/v1/entities/linked').
183
+ stub_request(:post, 'https://api.rosette.com/rest/v1/entities').
184
184
  with(body: request_file,
185
185
  headers: {'Accept' => 'application/json',
186
186
  'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
@@ -188,22 +188,24 @@ describe RosetteAPI do
188
188
  'User-Agent' => 'Ruby',
189
189
  'X-Rosetteapi-Key' => '0123456789',
190
190
  'X-Rosetteapi-Binding' => 'ruby',
191
- 'X-Rosetteapi-Binding-Version' => '1.1.0'}).
192
- to_return(status: 200, body: {'test': 'entities/linked'}.to_json, headers: {})
191
+ 'X-Rosetteapi-Binding-Version' => '1.1.1'}).
192
+ to_return(status: 200, body: {'test': 'entities'}.to_json, headers: {})
193
193
  end
194
- it 'test entities linked' do
194
+ it 'test entities without qids' do
195
195
  params = DocumentParameters.new
196
196
  params.content = 'Last month director Paul Feig announced the movie will have an all-star female cast including' \
197
197
  ' Kristen Wiig, Melissa McCarthy, Leslie Jones and Kate McKinnon.'
198
- response = RosetteAPI.new('0123456789').get_entities(params, true)
198
+ params.rosette_options = { linkEntities: false}
199
+ response = RosetteAPI.new('0123456789').get_entities(params)
199
200
  expect(response).instance_of? Hash
200
201
  end
201
202
 
202
- it 'test entities linked for resolve_entities is not a boolean' do
203
+ it 'test rosette_options is not a Hash' do
203
204
  params = DocumentParameters.new
204
205
  params.content = 'Last month director Paul Feig announced the movie will have an all-star female cast including' \
205
206
  ' Kristen Wiig, Melissa McCarthy, Leslie Jones and Kate McKinnon.'
206
- expect { RosetteAPI.new('0123456789').get_entities(params, 'smth') }.to raise_error(BadRequestError)
207
+ params.rosette_options = 1
208
+ expect { RosetteAPI.new('0123456789').get_entities(params) }.to raise_error(BadRequestError)
207
209
  end
208
210
  end
209
211
 
@@ -218,7 +220,7 @@ describe RosetteAPI do
218
220
  'User-Agent' => 'Ruby',
219
221
  'X-Rosetteapi-Key' => '0123456789',
220
222
  'X-Rosetteapi-Binding' => 'ruby',
221
- 'X-Rosetteapi-Binding-Version' => '1.1.0'}).
223
+ 'X-Rosetteapi-Binding-Version' => '1.1.1'}).
222
224
  to_return(status: 200, body: {'test': 'categories'}.to_json, headers: {})
223
225
  end
224
226
  it 'test categories' do
@@ -240,7 +242,7 @@ describe RosetteAPI do
240
242
  'User-Agent' => 'Ruby',
241
243
  'X-Rosetteapi-Key' => '0123456789',
242
244
  'X-Rosetteapi-Binding' => 'ruby',
243
- 'X-Rosetteapi-Binding-Version' => '1.1.0'}).
245
+ 'X-Rosetteapi-Binding-Version' => '1.1.1'}).
244
246
  to_return(status: 200, body: {'test': 'relationships'}.to_json, headers: {})
245
247
  end
246
248
  it 'test relationships' do
@@ -262,7 +264,7 @@ describe RosetteAPI do
262
264
  'User-Agent' => 'Ruby',
263
265
  'X-Rosetteapi-Key' => '0123456789',
264
266
  'X-Rosetteapi-Binding' => 'ruby',
265
- 'X-Rosetteapi-Binding-Version' => '1.1.0'}).
267
+ 'X-Rosetteapi-Binding-Version' => '1.1.1'}).
266
268
  to_return(status: 200, body: {'test': 'name-translation'}.to_json, headers: {})
267
269
  end
268
270
  it 'test name translation' do
@@ -289,7 +291,7 @@ describe RosetteAPI do
289
291
  'User-Agent' => 'Ruby',
290
292
  'X-Rosetteapi-Key' => '0123456789',
291
293
  'X-Rosetteapi-Binding' => 'ruby',
292
- 'X-Rosetteapi-Binding-Version' => '1.1.0'}).
294
+ 'X-Rosetteapi-Binding-Version' => '1.1.1'}).
293
295
  to_return(status: 200, body: {'test': 'name-similarity'}.to_json, headers: {})
294
296
  end
295
297
  it 'test name similarity' do
@@ -325,7 +327,7 @@ describe RosetteAPI do
325
327
  'User-Agent' => 'Ruby',
326
328
  'X-Rosetteapi-Key' => '0123456789',
327
329
  'X-Rosetteapi-Binding' => 'ruby',
328
- 'X-Rosetteapi-Binding-Version' => '1.1.0'}).
330
+ 'X-Rosetteapi-Binding-Version' => '1.1.1'}).
329
331
  to_return(status: 200, body: {'test': 'tokens'}.to_json, headers: {})
330
332
  end
331
333
  it 'test tokens' do
@@ -347,7 +349,7 @@ describe RosetteAPI do
347
349
  'User-Agent' => 'Ruby',
348
350
  'X-Rosetteapi-Key' => '0123456789',
349
351
  'X-Rosetteapi-Binding' => 'ruby',
350
- 'X-Rosetteapi-Binding-Version' => '1.1.0'}).
352
+ 'X-Rosetteapi-Binding-Version' => '1.1.1'}).
351
353
  to_return(status: 200, body: {'test': 'sentences'}.to_json, headers: {})
352
354
  end
353
355
  it 'test sentences' do
@@ -391,5 +393,43 @@ describe RosetteAPI do
391
393
  end
392
394
  end
393
395
 
396
+ describe '.get_language_custom_header' do
397
+ request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/language.json'))
398
+ before do
399
+ stub_request(:post, 'https://api.rosette.com/rest/v1/language').
400
+ with(body: request_file,
401
+ headers: {'Accept' => 'application/json',
402
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
403
+ 'Content-Type' => 'application/json',
404
+ 'User-Agent' => 'Ruby',
405
+ 'X-Rosetteapi-Key' => '0123456789',
406
+ 'X-Rosetteapi-Binding' => 'ruby',
407
+ 'X-Rosetteapi-Binding-Version' => '1.1.1',
408
+ 'X-RosetteApi-App' => 'ruby-app'}).
409
+ to_return(status: 200, body: {'test': 'language'}.to_json, headers: {})
410
+ end
411
+
412
+ it 'test custom_headers is invalid' do
413
+ params = DocumentParameters.new
414
+ params.content = 'Por favor Senorita, says the man.?'
415
+ params.custom_headers = { 'test': 'ruby-app'}
416
+ expect { RosetteAPI.new('0123456789').get_language(params) }.to raise_error(RosetteAPIError)
417
+ end
418
+ end
419
+
420
+ describe '.error_409_incompatible_client_version' do
421
+ before do
422
+ stub_request(:get, 'https://api.rosette.com/rest/v1/info').
423
+ with(headers: {'Accept' => '*/*',
424
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
425
+ 'User-Agent' => 'Ruby',
426
+ 'X-Rosetteapi-Key' => '0123456789'}).
427
+ to_return(status: 409, body: {'code': 'incompatibleClientVersion'}.to_json, headers: {})
428
+ end
429
+ it 'test error 409 properly handled' do
430
+ expect { RosetteAPI.new('0123456789').info }.to raise_error(RosetteAPIError)
431
+ end
432
+ end
433
+
394
434
 
395
435
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rosette_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Basis Technology Corp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-02 00:00:00.000000000 Z
11
+ date: 2016-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubysl-securerandom
@@ -61,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  version: '0'
62
62
  requirements: []
63
63
  rubyforge_project:
64
- rubygems_version: 2.6.4
64
+ rubygems_version: 2.6.6
65
65
  signing_key:
66
66
  specification_version: 2
67
67
  summary: Rosette API gem that supports multilingual text-analytics.