rosette_api 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3179104e400cf9675967df11a64722393758fe06
4
+ data.tar.gz: 31bb38876a1964f3d0d35d9b98ea7dfb955f66d4
5
+ SHA512:
6
+ metadata.gz: a16a11cb4cc29064a694f9878f81a74c4ceb861aec94509bb4fc33f981cefb439d09c4e55884f7a4a7333bf07854e70996c8f487f551b1898d9727dd49118b62
7
+ data.tar.gz: 4fd8a574f8c52f3502e8ed6dc3db810810c06cf10f081d4bef93d9339df2b774c88e3890e5966adb41d558ab91f4fc247cac579200b8061de37a911e0d7a548e
@@ -0,0 +1,8 @@
1
+ require_relative 'rosette_api_error'
2
+
3
+ # This class represents Rosette API errors with badRequest status_code.
4
+ class BadRequestError < RosetteAPIError
5
+ def initialize(message) #:notnew:
6
+ super 'badRequest', message
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require_relative 'rosette_api_error'
2
+
3
+ # This class represents Rosette API errors with badRequestFormat status_code.
4
+ class BadRequestFormatError < RosetteAPIError
5
+ def initialize(message) #:notnew:
6
+ super 'badRequestFormat', message
7
+ end
8
+ end
@@ -0,0 +1,66 @@
1
+ require_relative 'bad_request_format_error'
2
+
3
+ # This class encapsulates parameters that will be used by most of the endpoints
4
+ # with exclusion of name-similarity and name-translation.
5
+ class DocumentParameters
6
+ # Content to be analyzed (required if no content_uri and file_path)
7
+ attr_accessor :content
8
+ # URL to retrieve content from and analyze (required if no content and file_path)
9
+ attr_accessor :content_uri
10
+ # File path of the file to be analyzed (required if no content and content_uri)
11
+ attr_accessor :file_path
12
+ # genre to categorize the input data
13
+ attr_accessor :genre
14
+ # ISO 639-3 language code of the provided content (optional)
15
+ attr_accessor :language
16
+
17
+ def initialize(options = {}) #:notnew:
18
+ options = {
19
+ content: nil,
20
+ content_uri: nil,
21
+ file_path: nil,
22
+ genre: nil,
23
+ language: nil
24
+ }.update options
25
+ @content = options[:content]
26
+ @content_uri = options[:content_uri]
27
+ @file_path = options[:file_path]
28
+ @genre = options[:genre]
29
+ @language = options[:language]
30
+ end
31
+
32
+ # Validates the parameters by checking if there are multiple content sources
33
+ # set or no content provided at all.
34
+ def validate_params
35
+ if [@content, @content_uri, @file_path].compact.length > 1
36
+ raise BadRequestFormatError.new 'The format of the request is invalid: multiple content sources;' \
37
+ ' must be one of an attachment, an inline "content" field, or an external' \
38
+ '"contentUri"'
39
+ elsif [@content, @content_uri, @file_path].all?(&:nil?)
40
+ raise BadRequestFormatError.new 'The format of the request is invalid: no content provided; must' \
41
+ ' be one of an attachment, an inline "content" field, or an external "contentUri"'
42
+ end
43
+ end
44
+
45
+ # Converts this class to Hash with its keys in lower CamelCase.
46
+ #
47
+ # Returns the new Hash.
48
+ def load_params
49
+ self.validate_params
50
+ self.to_hash.select { |_key, value| !value.nil? }
51
+ .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
52
+ .to_h
53
+ end
54
+
55
+ # Converts this class to Hash.
56
+ #
57
+ # Returns the new Hash.
58
+ def to_hash
59
+ {
60
+ content: @content,
61
+ content_uri: @content_uri,
62
+ file_path: @file_path,
63
+ language: @language
64
+ }
65
+ end
66
+ end
@@ -0,0 +1,44 @@
1
+ # This class represents an entity name in Rosette API.
2
+ class NameParameter
3
+ # Name's entity type (PERSON, LOCATION, ORGANIZATION) (optional)
4
+ attr_accessor :entity_type
5
+ # ISO 639-3 code of the name's language (optional)
6
+ attr_accessor :language
7
+ # ISO 15924 code of the name's script (optional)
8
+ attr_accessor :script
9
+ # Name to be analyzed
10
+ attr_accessor :text
11
+
12
+ def initialize(text, options = {}) #:notnew:
13
+ options = {
14
+ entity_type: nil,
15
+ language: nil,
16
+ script: nil
17
+ }.update options
18
+ @text = text
19
+ @entity_type = options[:entity_type]
20
+ @language = options[:language]
21
+ @script = options[:script]
22
+ end
23
+
24
+ # Converts this class to Hash with its keys in lower CamelCase.
25
+ #
26
+ # Returns the new Hash.
27
+ def load_param
28
+ self.to_hash.select { |_key, value| !value.nil? }
29
+ .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
30
+ .to_h
31
+ end
32
+
33
+ # Converts this class to Hash.
34
+ #
35
+ # Returns the new Hash.
36
+ def to_hash
37
+ {
38
+ entity_type: @entity_type,
39
+ language: @language,
40
+ script: @script,
41
+ text: @text
42
+ }
43
+ end
44
+ end
@@ -0,0 +1,52 @@
1
+ require_relative 'bad_request_error'
2
+ require_relative 'name_parameter'
3
+
4
+ # This class encapsulates parameters that are needed for name-similarity in
5
+ # Rosette API.
6
+ class NameSimilarityParameters
7
+ # genre to categorize the input data
8
+ attr_accessor :genre
9
+ # Name to be compared to name2
10
+ attr_accessor :name1
11
+ # Name to be compared to name1
12
+ attr_accessor :name2
13
+
14
+ def initialize(name1, name2, options = {}) #:notnew:
15
+ options = {
16
+ genre: nil
17
+ }.update options
18
+ @genre = options[:genre]
19
+ @name1 = name1
20
+ @name2 = name2
21
+ end
22
+
23
+ # Validates the parameters by checking if name1 and name2 are instances of
24
+ # a String or NameParameter.
25
+ def validate_params
26
+ if [String, NameParameter].none? { |clazz| @name1.is_a? clazz }
27
+ raise BadRequestError.new('name1 option can only be an instance of a String or NameParameter')
28
+ elsif [String, NameParameter].none? { |clazz| @name2.is_a? clazz }
29
+ raise BadRequestError.new('name2 option can only be an instance of a String or NameParameter')
30
+ end
31
+ end
32
+
33
+ # Converts this class to Hash with its keys in lower CamelCase.
34
+ #
35
+ # Returns the new Hash.
36
+ def load_params
37
+ self.validate_params
38
+ self.to_hash.select { |_key, value| !value.nil? }
39
+ .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
40
+ .to_h
41
+ end
42
+
43
+ # Converts this class to Hash.
44
+ #
45
+ # Returns the new Hash.
46
+ def to_hash
47
+ {
48
+ name1: @name1.is_a?(NameParameter) ? @name1.load_param : @name1,
49
+ name2: @name2.is_a?(NameParameter) ? @name2.load_param : @name2
50
+ }
51
+ end
52
+ end
@@ -0,0 +1,70 @@
1
+ require_relative 'rosette_api_error'
2
+
3
+ # This class encapsulates parameters that are needed for name-translation in
4
+ # Rosette API.
5
+ class NameTranslationParameters
6
+ # Name's entity type (PERSON, LOCATION, ORGANIZATION) (optional)
7
+ attr_accessor :entity_type
8
+ # genre to categorize the input data
9
+ attr_accessor :genre
10
+ # Name to translate
11
+ attr_accessor :name
12
+ # ISO 693-3 code of the name's native language the name originates in (optional)
13
+ attr_accessor :source_language_of_origin
14
+ # ISO 693-3 code of the name's language of use (optional)
15
+ attr_accessor :source_language_of_use
16
+ # ISO 15924 code of the name's script (optional)
17
+ attr_accessor :source_script
18
+ # ISO 639-3 code of the translation language
19
+ attr_accessor :target_language
20
+ # Transliteration scheme for the translation (optional)
21
+ attr_accessor :target_scheme
22
+ # ISO 15924 code of name's script (optional)
23
+ attr_accessor :target_script
24
+
25
+ def initialize(name, target_language, options = {}) #:notnew:
26
+ options = {
27
+ entity_type: nil,
28
+ genre: nil,
29
+ source_language_of_origin: nil,
30
+ source_language_of_use: nil,
31
+ source_script: nil,
32
+ target_scheme: nil,
33
+ target_script: nil
34
+ }.update options
35
+ @name = name
36
+ @entity_type = options[:entity_type]
37
+ @genre = options[:genre]
38
+ @source_language_of_origin = options[:source_language_of_origin]
39
+ @source_language_of_use = options[:source_language_of_use]
40
+ @source_script = options[:source_script]
41
+ @target_language = target_language
42
+ @target_scheme = options[:target_scheme]
43
+ @target_script = options[:target_script]
44
+ end
45
+
46
+ # Converts this class to Hash with its keys in lower CamelCase.
47
+ #
48
+ # Returns the new Hash.
49
+ def load_params
50
+ self.to_hash.select { |_key, value| !value.nil? }
51
+ .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
52
+ .to_h
53
+ end
54
+
55
+ # Converts this class to Hash.
56
+ #
57
+ # Returns the new Hash.
58
+ def to_hash
59
+ {
60
+ entity_type: @entity_type,
61
+ name: @name,
62
+ source_language_of_origin: @source_language_of_origin,
63
+ source_language_of_use: @source_language_of_use,
64
+ source_script: @source_script,
65
+ target_language: @target_language,
66
+ target_scheme: @target_scheme,
67
+ target_script: @target_script
68
+ }
69
+ end
70
+ end
@@ -0,0 +1,155 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'json'
4
+ require 'securerandom'
5
+ require_relative 'rosette_api_error'
6
+
7
+ # This class handles all Rosette API requests.
8
+ class RequestBuilder
9
+ @retries = 5
10
+ # Alternate Rosette API URL
11
+ attr_reader :alternate_url
12
+ # Parameters to build the body of the request from
13
+ attr_accessor :params
14
+ # Rosette API key
15
+ attr_accessor :user_key
16
+
17
+ def initialize(user_key, alternate_url, params = {}) #:notnew:
18
+ @user_key = user_key
19
+ @alternate_url = alternate_url
20
+ @params = params
21
+ end
22
+
23
+ # Prepares a plain POST request for Rosette API.
24
+ #
25
+ # ==== Attributes
26
+ #
27
+ # * +params+ - Parameters to build the body of the request.
28
+ #
29
+ # Returns a HTTP connection and the built POST request.
30
+ def prepare_plain_request(params)
31
+ begin
32
+ uri = URI.parse @alternate_url
33
+ http = Net::HTTP.new uri.host, uri.port
34
+ http.use_ssl = uri.scheme == 'https'
35
+ request = Net::HTTP::Post.new uri.request_uri
36
+ rescue
37
+ raise RosetteAPIError.new 'connectionError', 'Failed to establish connection with Rosette API server.'
38
+ end
39
+
40
+ request['X-RosetteAPI-Key'] = @user_key
41
+ request['Content-Type'] = 'application/json'
42
+ request['Accept'] = 'application/json'
43
+ request.body = params.to_json
44
+
45
+ [http, request]
46
+ end
47
+
48
+ # Prepares a multipart/form-data POST request for Rosette API.
49
+ #
50
+ # ==== Attributes
51
+ #
52
+ # * +params+ - Parameters to build the body of the request.
53
+ #
54
+ # Returns a HTTP connection and the built POST request.
55
+ def prepare_multipart_request(params)
56
+ begin
57
+ file = File.open params['filePath'], 'r'
58
+ text = file.read
59
+ rescue => err
60
+ raise err
61
+ end
62
+
63
+ boundary = SecureRandom.hex
64
+ post_body = []
65
+ request_file = params.to_json
66
+
67
+ # Add the content data
68
+ post_body << "--#{boundary}\r\n"
69
+ post_body << "Content-Disposition: form-data; name=\"content\"; filename=\"#{File.basename(file)}\"\r\n"
70
+ post_body << "Content-Type: text/plain\r\n\r\n"
71
+ post_body << text
72
+
73
+ # Add the request data
74
+ post_body << "\r\n\r\n--#{boundary}\r\n"
75
+ post_body << "Content-Disposition: form-data; name=\"request\"\r\n"
76
+ post_body << "Content-Type: application/json\r\n\r\n"
77
+ post_body << request_file
78
+ post_body << "\r\n\r\n--#{boundary}--\r\n"
79
+
80
+ # Create the HTTP objects
81
+ uri = URI.parse @alternate_url
82
+ http = Net::HTTP.new uri.host, uri.port
83
+ http.use_ssl = uri.scheme == 'https'
84
+ request = Net::HTTP::Post.new uri.request_uri
85
+ request.add_field 'Content-Type', "multipart/form-data; boundary=#{boundary}"
86
+ request.add_field 'X-RosetteAPI-Key', @user_key
87
+ request.body = post_body.join
88
+
89
+ [http, request]
90
+ end
91
+
92
+ # Sends a GET request to Rosette API.
93
+ #
94
+ # Returns JSON response or raises RosetteAPIError if encountered.
95
+ def send_get_request
96
+ uri = URI.parse @alternate_url
97
+ http = Net::HTTP.new uri.host, uri.port
98
+ http.use_ssl = uri.scheme == 'https'
99
+
100
+ request = Net::HTTP::Get.new uri.request_uri
101
+ request['X-RosetteAPI-Key'] = @user_key
102
+
103
+ self.get_response http, request
104
+ end
105
+
106
+ # Sends a POST request to Rosette API.
107
+ #
108
+ # Returns JSON response or raises RosetteAPIError if encountered.
109
+ def send_post_request
110
+ params = (@alternate_url.to_s.include? '/info?clientVersion=') ? '{"body": "version check"}' : @params
111
+
112
+ if !params['filePath'].nil?
113
+ http, request = self.prepare_multipart_request params
114
+ else
115
+ http, request = self.prepare_plain_request params
116
+ end
117
+
118
+ self.get_response http, request
119
+ end
120
+
121
+ # Gets response from HTTP connection.
122
+ #
123
+ # ==== Attributes
124
+ #
125
+ # * +http+ - HTTP connection.
126
+ #
127
+ # * +request+ - Prepared Rosette API request.
128
+ #
129
+ # Returns JSON response or raises RosetteAPIError if encountered.
130
+ def get_response(http, request)
131
+ response = http.request request
132
+
133
+ if response.code != '200'
134
+ message = JSON.parse(response.body)['message']
135
+ code = JSON.parse(response.body)['code']
136
+ if response.code == '429'
137
+ if @retries != 0
138
+ @retries = @retries - 1
139
+ sleep 15
140
+ self.get_response(http, request)
141
+ else
142
+ raise RosetteAPIError.new code, message
143
+ end
144
+ else
145
+ raise RosetteAPIError.new code, message
146
+ end
147
+ else
148
+ response_headers = {}
149
+ response.header.each_header { |key, value| response_headers[key] = value }
150
+ response_headers = { responseHeaders: response_headers }
151
+
152
+ JSON.parse(response.body).merge(response_headers)
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,338 @@
1
+ require_relative 'request_builder'
2
+ require_relative 'document_parameters'
3
+ require_relative 'name_translation_parameters'
4
+ require_relative 'name_similarity_parameters'
5
+ require_relative 'rosette_api_error'
6
+ require_relative 'bad_request_error'
7
+ require_relative 'bad_request_format_error'
8
+
9
+ # This class allows you to access all Rosette API endpoints.
10
+ class RosetteAPI
11
+ # Version of Ruby binding
12
+ BINDING_VERSION = '1.0.2'
13
+ # Rosette API language endpoint
14
+ LANGUAGE_ENDPOINT = '/language'
15
+ # Rosette API morphology endpoint
16
+ MORPHOLOGY_ENDPOINT = '/morphology'
17
+ # Rosette API entities endpoint
18
+ ENTITIES_ENDPOINT = '/entities'
19
+ # Rosette API entities/linked endpoint
20
+ ENTITIES_LINKED_ENDPOINT = '/entities/linked'
21
+ # Rosette API categories endpoint
22
+ CATEGORIES_ENDPOINT = '/categories'
23
+ # Rosette API relationships endpoint
24
+ RELATIONSHIPS_ENDPOINT = '/relationships'
25
+ # Rosette API sentiment endpoint
26
+ SENTIMENT_ENDPOINT = '/sentiment'
27
+ # Rosette API name-translation endpoint
28
+ NAME_TRANSLATION_ENDPOINT = '/name-translation'
29
+ # Rosette API name-similarity endpoint
30
+ NAME_SIMILARITY_ENDPOINT = '/name-similarity'
31
+ # Rosette API tokens endpoint
32
+ TOKENS_ENDPOINT = '/tokens'
33
+ # Rosette API sentences endpoint
34
+ SENTENCES_ENDPOINT = '/sentences'
35
+ # Rosette API info endpoint
36
+ INFO = '/info'
37
+ # Rosette API version check endpoint
38
+ VERSION_CHECK = '/info?clientVersion=' + BINDING_VERSION
39
+ # Rosette API ping endpoint
40
+ PING = '/ping'
41
+
42
+ # Rosette API key
43
+ attr_accessor :user_key
44
+ # Alternate Rosette API URL
45
+ attr_accessor :alternate_url
46
+
47
+ def initialize(user_key, alternate_url = 'https://api.rosette.com/rest/v1') #:notnew:
48
+ @user_key = user_key
49
+ @alternate_url = alternate_url
50
+
51
+ if @alternate_url.to_s.end_with?('/')
52
+ @alternate_url = alternate_url.to_s.slice(0..-2)
53
+ end
54
+
55
+ self.check_version_compatibility
56
+ end
57
+
58
+ # Checks binding version compatibility against the Rosette API server.
59
+ def check_version_compatibility
60
+ response = RequestBuilder.new(@user_key, @alternate_url + VERSION_CHECK)
61
+ .send_post_request
62
+
63
+ unless response['versionChecked']
64
+ puts JSON.pretty_generate(response)
65
+
66
+ exit
67
+ end
68
+ end
69
+
70
+ # Identifies in which language(s) the input is written.
71
+ #
72
+ # ==== Attributes
73
+ #
74
+ # * +params+ - DocumentParameters helps to build the request body in RequestBuilder.
75
+ #
76
+ # Returns list of candidate languages in order of descending confidence.
77
+ def get_language(params)
78
+ check_params params
79
+
80
+ params = params.load_params
81
+
82
+ RequestBuilder.new(@user_key, @alternate_url + LANGUAGE_ENDPOINT, params)
83
+ .send_post_request
84
+ end
85
+
86
+ # Extracts parts-of-speech, lemmas (dictionary form), compound components,
87
+ # and Han-readings for each token in the input.
88
+ #
89
+ # ==== Attributes
90
+ #
91
+ # * +params+ - DocumentParameters helps to build the request body in RequestBuilder.
92
+ #
93
+ # Returns the lemmas, compound components, Han-readings, and parts-of-speech
94
+ # tags of the input for supported languages.
95
+ def get_morphology_complete(params)
96
+ check_params params
97
+
98
+ params = params.load_params
99
+
100
+ RequestBuilder.new(@user_key, @alternate_url + MORPHOLOGY_ENDPOINT + '/complete', params)
101
+ .send_post_request
102
+ end
103
+
104
+ # Extracts compound-components from the input.
105
+ #
106
+ # ==== Attributes
107
+ #
108
+ # * +params+ - DocumentParameters helps to build the request body in RequestBuilder.
109
+ #
110
+ # Returns list of components for each compound word of the input for supported
111
+ # languages.
112
+ def get_compound_components(params)
113
+ check_params params
114
+
115
+ params = params.load_params
116
+
117
+ RequestBuilder.new(@user_key, @alternate_url + MORPHOLOGY_ENDPOINT + '/compound-components', params)
118
+ .send_post_request
119
+ end
120
+
121
+ # Extracts Han-readings from the input.
122
+ #
123
+ # ==== Attributes
124
+ #
125
+ # * +params+ - DocumentParameters helps to build the request body in RequestBuilder.
126
+ #
127
+ # Returns list of Han-readings which provide pronunciation information for
128
+ # Han script, in both Chinese and Japanese input text.
129
+ def get_han_readings(params)
130
+ check_params params
131
+
132
+ params = params.load_params
133
+
134
+ RequestBuilder.new(@user_key, @alternate_url + MORPHOLOGY_ENDPOINT + '/han-readings', params)
135
+ .send_post_request
136
+ end
137
+
138
+ # Extracts lemmas from the input.
139
+ #
140
+ # ==== Attributes
141
+ #
142
+ # * +params+ - DocumentParameters helps to build the request body in RequestBuilder.
143
+ #
144
+ # Returns list of lemmas for each token of the input for supported languages.
145
+ def get_lemmas(params)
146
+ check_params params
147
+
148
+ params = params.load_params
149
+
150
+ RequestBuilder.new(@user_key, @alternate_url + MORPHOLOGY_ENDPOINT + '/lemmas', params)
151
+ .send_post_request
152
+ end
153
+
154
+ # Extracts parts-of-speech from the input.
155
+ #
156
+ # ==== Attributes
157
+ #
158
+ # * +params+ - DocumentParameters helps to build the request body in RequestBuilder.
159
+ #
160
+ # Returns list of part-of-speech (POS) tags for each of the words of the
161
+ # input, depending on the context of how it is used.
162
+ def get_parts_of_speech(params)
163
+ check_params params
164
+
165
+ params = params.load_params
166
+
167
+ RequestBuilder.new(@user_key, @alternate_url + MORPHOLOGY_ENDPOINT + '/parts-of-speech', params)
168
+ .send_post_request
169
+ end
170
+
171
+ # Extracts entities from the input.
172
+ #
173
+ # ==== Attributes
174
+ #
175
+ # * +params+ - DocumentParameters helps to build the request body in RequestBuilder.
176
+ #
177
+ # Returns each entity extracted from the input.
178
+ def get_entities(params)
179
+ check_params params
180
+
181
+ params = params.load_params
182
+
183
+ RequestBuilder.new(@user_key, @alternate_url + ENTITIES_ENDPOINT, params)
184
+ .send_post_request
185
+ end
186
+
187
+ # Extracts entities from the input.
188
+ #
189
+ # ==== Attributes
190
+ #
191
+ # * +params+ - DocumentParameters helps to build the request body in RequestBuilder.
192
+ #
193
+ # Returns list of entities that have been linked to entities in the knowledge
194
+ # base.
195
+ def get_entities_linked(params)
196
+ check_params params
197
+
198
+ params = params.load_params
199
+
200
+ RequestBuilder.new(@user_key, @alternate_url + ENTITIES_LINKED_ENDPOINT, params)
201
+ .send_post_request
202
+ end
203
+
204
+ # Extracts Tier 1 contextual categories from the input.
205
+ #
206
+ # ==== Attributes
207
+ #
208
+ # * +params+ - DocumentParameters helps to build the request body in RequestBuilder.
209
+ #
210
+ # Returns the contextual categories identified in the input.
211
+ def get_categories(params)
212
+ check_params params
213
+
214
+ params = params.load_params
215
+
216
+ RequestBuilder.new(@user_key, @alternate_url + CATEGORIES_ENDPOINT, params)
217
+ .send_post_request
218
+ end
219
+
220
+ # Extracts relationships from the input.
221
+ #
222
+ # ==== Attributes
223
+ #
224
+ # * +params+ - DocumentParameters helps to build the request body in RequestBuilder.
225
+ #
226
+ # Returns each relationship extracted from the input.
227
+ def get_relationships(params)
228
+ check_params params
229
+
230
+ params = params.load_params
231
+
232
+ RequestBuilder.new(@user_key, @alternate_url + RELATIONSHIPS_ENDPOINT, params)
233
+ .send_post_request
234
+ end
235
+
236
+ # Analyzes the positive and negative sentiment expressed by the input.
237
+ #
238
+ # ==== Attributes
239
+ #
240
+ # * +params+ - DocumentParameters helps to build the request body in RequestBuilder.
241
+ #
242
+ # Returns sentiment analysis results.
243
+ def get_sentiment(params)
244
+ check_params params
245
+
246
+ params = params.load_params
247
+
248
+ RequestBuilder.new(@user_key, @alternate_url + SENTIMENT_ENDPOINT, params)
249
+ .send_post_request
250
+ end
251
+
252
+ # Translates a given name to a supported specified language.
253
+ #
254
+ # ==== Attributes
255
+ #
256
+ # * +params+ - NameTranslationParameters helps to build the request body in RequestBuilder.
257
+ #
258
+ # Returns the translation of a name.
259
+ def name_translation(params)
260
+ check_params params, 'Expects a NameTranslationParameters type as an argument', NameTranslationParameters
261
+
262
+ params = params.load_params
263
+
264
+ RequestBuilder.new(@user_key, @alternate_url + NAME_TRANSLATION_ENDPOINT, params)
265
+ .send_post_request
266
+ end
267
+
268
+ # Compares two entity names (person, location, or organization) and returns a
269
+ # match score from 0 to 1.
270
+ #
271
+ # ==== Attributes
272
+ #
273
+ # * +params+ - NameSimilarityParameters helps to build the request body in RequestBuilder.
274
+ #
275
+ # Returns the confidence score of matching 2 names.
276
+ def name_similarity(params)
277
+ check_params params, 'Expects a NameSimilarityParameters type as an argument', NameSimilarityParameters
278
+
279
+ params = params.load_params
280
+
281
+ RequestBuilder.new(@user_key, @alternate_url + NAME_SIMILARITY_ENDPOINT, params)
282
+ .send_post_request
283
+ end
284
+
285
+ # Divides the input into tokens.
286
+ #
287
+ # ==== Attributes
288
+ #
289
+ # * +params+ - DocumentParameters helps to build the request body in RequestBuilder.
290
+ #
291
+ # Returns list of tokens of the input.
292
+ def get_tokens(params)
293
+ check_params params
294
+
295
+ params = params.load_params
296
+
297
+ RequestBuilder.new(@user_key, @alternate_url + TOKENS_ENDPOINT, params)
298
+ .send_post_request
299
+ end
300
+
301
+ # Divides the input into sentences.
302
+ #
303
+ # ==== Attributes
304
+ #
305
+ # * +params+ - DocumentParameters helps to build the request body in RequestBuilder.
306
+ #
307
+ # Returns list of linguistic sentences of the input.
308
+ def get_sentences(params)
309
+ check_params params
310
+
311
+ params = params.load_params
312
+
313
+ RequestBuilder.new(@user_key, @alternate_url + SENTENCES_ENDPOINT, params)
314
+ .send_post_request
315
+ end
316
+
317
+ # Gets information about the Rosette API, returns name, version, build number
318
+ # and build time.
319
+ def info
320
+ RequestBuilder.new(@user_key, @alternate_url + INFO)
321
+ .send_get_request
322
+ end
323
+
324
+ # Pings the Rosette API for a response indicting that the service is
325
+ # available.
326
+ def ping
327
+ RequestBuilder.new(@user_key, @alternate_url + PING)
328
+ .send_get_request
329
+ end
330
+
331
+ private
332
+
333
+ # Checks that the right parameter type is being passed in.
334
+ def check_params(params, message = 'Expects a DocumentParameters type as an argument', type = DocumentParameters)
335
+ raise BadRequest.new message unless params.is_a? type
336
+ end
337
+ end
338
+
@@ -0,0 +1,13 @@
1
+ # This class encapsulates all Rosette API server errors encountered during
2
+ # requests.
3
+ class RosetteAPIError < StandardError
4
+ # Rosette API error's status code
5
+ attr_accessor :status_code
6
+ # Rosette API error's message
7
+ attr_accessor :message
8
+
9
+ def initialize(status_code, message) #:notnew:
10
+ @status_code = status_code
11
+ @message = message
12
+ end
13
+ end
@@ -0,0 +1,470 @@
1
+ # encoding: UTF-8
2
+ require 'rosette_api'
3
+ require 'rspec'
4
+ require 'webmock/rspec'
5
+ require 'json'
6
+ WebMock.disable_net_connect!(allow_localhost: true)
7
+
8
+ describe RosetteAPI do
9
+
10
+ describe '.get_language' do
11
+ request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/language.json'))
12
+ before do
13
+ stub_request(:post, 'https://api.rosette.com/rest/v1/info?clientVersion=1.0.2').
14
+ with(body: "\"{\\\"body\\\": \\\"version check\\\"}\"",
15
+ headers: {'Accept' => 'application/json',
16
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
17
+ 'Content-Type' => 'application/json',
18
+ 'User-Agent' => 'Ruby',
19
+ 'X-Rosetteapi-Key' => '0123456789'}).
20
+ to_return(status: 200, body: {'versionChecked': true}.to_json, headers: {})
21
+ stub_request(:post, 'https://api.rosette.com/rest/v1/language').
22
+ with(body: request_file,
23
+ headers: {'Accept' => 'application/json',
24
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
25
+ 'Content-Type' => 'application/json',
26
+ 'User-Agent' => 'Ruby',
27
+ 'X-Rosetteapi-Key' => '0123456789'}).
28
+ to_return(status: 200, body: {'test': 'language'}.to_json, headers: {})
29
+ end
30
+ it 'test language' do
31
+ params = DocumentParameters.new
32
+ params.content = 'Por favor Senorita, says the man.?'
33
+ response = RosetteAPI.new('0123456789').get_language(params)
34
+ expect(response).instance_of? Hash
35
+ end
36
+
37
+ it 'badRequestFormat: the format of the request is invalid: multiple content sources' do
38
+ params = DocumentParameters.new
39
+ params.content = 'Por favor Senorita, says the man.?'
40
+ params.content_uri = 'Por favor Senorita, says the man.?'
41
+ expect { RosetteAPI.new('0123456789').get_language(params) }.to raise_error(BadRequestFormatError)
42
+ end
43
+
44
+ it 'badRequestFormat: the format of the request is invalid: no content provided;' do
45
+ params = DocumentParameters.new
46
+ expect { RosetteAPI.new('0123456789')
47
+ .get_language(params) }
48
+ .to raise_error(BadRequestFormatError)
49
+ end
50
+ end
51
+
52
+ describe '.get_morphology_complete' do
53
+ request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/morphology_complete.json'))
54
+ before do
55
+ stub_request(:post, 'https://api.rosette.com/rest/v1/info?clientVersion=1.0.2').
56
+ with(body: "\"{\\\"body\\\": \\\"version check\\\"}\"",
57
+ headers: {'Accept' => 'application/json',
58
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
59
+ 'Content-Type' => 'application/json',
60
+ 'User-Agent' => 'Ruby',
61
+ 'X-Rosetteapi-Key' => '0123456789'}).
62
+ to_return(status: 200, body: {'versionChecked': true}.to_json, headers: {})
63
+ stub_request(:post, 'https://api.rosette.com/rest/v1/morphology/complete').
64
+ with(body: request_file,
65
+ headers: {'Accept' => 'application/json',
66
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
67
+ 'Content-Type' => 'application/json',
68
+ 'User-Agent' => 'Ruby',
69
+ 'X-Rosetteapi-Key' => '0123456789'}).
70
+ to_return(status: 200, body: {'test': 'morphology/complete'}.to_json, headers: {})
71
+ end
72
+ it 'test morphology complete' do
73
+ params = DocumentParameters.new
74
+ params.content = 'The quick brown fox jumped over the lazy dog. Yes he did.'
75
+ response = RosetteAPI.new('0123456789').get_morphology_complete(params)
76
+ expect(response).instance_of? Hash
77
+ end
78
+ end
79
+
80
+ describe '.get_compound_components' do
81
+ request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/morphology_compound_components.json'))
82
+ before do
83
+ stub_request(:post, 'https://api.rosette.com/rest/v1/info?clientVersion=1.0.2').
84
+ with(body: "\"{\\\"body\\\": \\\"version check\\\"}\"",
85
+ headers: {'Accept' => 'application/json',
86
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
87
+ 'Content-Type' => 'application/json',
88
+ 'User-Agent' => 'Ruby',
89
+ 'X-Rosetteapi-Key' => '0123456789'}).
90
+ to_return(status: 200, body: {'versionChecked': true}.to_json, headers: {})
91
+ stub_request(:post, 'https://api.rosette.com/rest/v1/morphology/compound-components').
92
+ with(body: request_file,
93
+ headers: {'Accept' => 'application/json',
94
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
95
+ 'Content-Type' => 'application/json',
96
+ 'User-Agent' => 'Ruby',
97
+ 'X-Rosetteapi-Key' => '0123456789'}).
98
+ to_return(status: 200, body: {'test': 'morphology/compound-components'}.to_json, headers: {})
99
+ end
100
+ it 'test morphology compound components' do
101
+ params = DocumentParameters.new
102
+ params.content = 'Rechtsschutzversicherungsgesellschaften'
103
+ response = RosetteAPI.new('0123456789').get_compound_components(params)
104
+ expect(response).instance_of? Hash
105
+ end
106
+ end
107
+
108
+ describe '.get_han_readings' do
109
+ request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/morphology_han_readings.json')), encoding: 'utf-8'
110
+ before do
111
+ stub_request(:post, 'https://api.rosette.com/rest/v1/info?clientVersion=1.0.2').
112
+ with(body: "\"{\\\"body\\\": \\\"version check\\\"}\"",
113
+ headers: {'Accept' => 'application/json',
114
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
115
+ 'Content-Type' => 'application/json',
116
+ 'User-Agent' => 'Ruby',
117
+ 'X-Rosetteapi-Key' => '0123456789'}).
118
+ to_return(status: 200, body: {'versionChecked': true}.to_json, headers: {})
119
+ stub_request(:post, 'https://api.rosette.com/rest/v1/morphology/han-readings').
120
+ with(body: request_file,
121
+ headers: {'Accept' => 'application/json',
122
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
123
+ 'Content-Type' => 'application/json',
124
+ 'User-Agent' => 'Ruby',
125
+ 'X-Rosetteapi-Key' => '0123456789'}).
126
+ to_return(status: 200, body: {'test': 'morphology/han-readings'}.to_json, headers: {})
127
+ end
128
+ it 'test morphology han readings' do
129
+ params = DocumentParameters.new
130
+ params.content = '北京大学生物系主任办公室内部会议'
131
+ response = RosetteAPI.new('0123456789').get_han_readings(params)
132
+ expect(response).instance_of? Hash
133
+ end
134
+ end
135
+
136
+ describe '.get_parts_of_speech' do
137
+ request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/morphology_parts_of_speech.json'))
138
+ before do
139
+ stub_request(:post, 'https://api.rosette.com/rest/v1/info?clientVersion=1.0.2').
140
+ with(body: "\"{\\\"body\\\": \\\"version check\\\"}\"",
141
+ headers: {'Accept' => 'application/json',
142
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
143
+ 'Content-Type' => 'application/json',
144
+ 'User-Agent' => 'Ruby',
145
+ 'X-Rosetteapi-Key' => '0123456789'}).
146
+ to_return(status: 200, body: {'versionChecked': true}.to_json, headers: {})
147
+ stub_request(:post, 'https://api.rosette.com/rest/v1/morphology/parts-of-speech').
148
+ with(body: request_file,
149
+ headers: {'Accept' => 'application/json',
150
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
151
+ 'Content-Type' => 'application/json',
152
+ 'User-Agent' => 'Ruby',
153
+ 'X-Rosetteapi-Key' => '0123456789'}).
154
+ to_return(status: 200, body: {'test': 'morphology/parts-of-speech'}.to_json, headers: {})
155
+ end
156
+ it 'test morphology parts of speech' do
157
+ params = DocumentParameters.new
158
+ params.content = "The fact is that the geese just went back to get a rest and I'm not banking on their return soon"
159
+ response = RosetteAPI.new('0123456789').get_parts_of_speech(params)
160
+ expect(response).instance_of? Hash
161
+ end
162
+ end
163
+
164
+ describe '.get_lemmas' do
165
+ request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/morphology_lemmas.json'))
166
+ before do
167
+ stub_request(:post, 'https://api.rosette.com/rest/v1/info?clientVersion=1.0.2').
168
+ with(body: "\"{\\\"body\\\": \\\"version check\\\"}\"",
169
+ headers: {'Accept' => 'application/json',
170
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
171
+ 'Content-Type' => 'application/json',
172
+ 'User-Agent' => 'Ruby',
173
+ 'X-Rosetteapi-Key' => '0123456789'}).
174
+ to_return(status: 200, body: {'versionChecked': true}.to_json, headers: {})
175
+ stub_request(:post, 'https://api.rosette.com/rest/v1/morphology/lemmas').
176
+ with(body: request_file,
177
+ headers: {'Accept' => 'application/json',
178
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
179
+ 'Content-Type' => 'application/json',
180
+ 'User-Agent' => 'Ruby',
181
+ 'X-Rosetteapi-Key' => '0123456789'}).
182
+ to_return(status: 200, body: {'test': 'morphology/lemmas'}.to_json, headers: {})
183
+ end
184
+ it 'test morphology lemmas' do
185
+ params = DocumentParameters.new
186
+ params.content = "The fact is that the geese just went back to get a rest and I'm not banking on their return soon"
187
+ response = RosetteAPI.new('0123456789').get_lemmas(params)
188
+ expect(response).instance_of? Hash
189
+ end
190
+ end
191
+
192
+ describe '.get_entities' do
193
+ request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/entities.json')), encoding: 'utf-8'
194
+ before do
195
+ stub_request(:post, 'https://api.rosette.com/rest/v1/info?clientVersion=1.0.2').
196
+ with(body: "\"{\\\"body\\\": \\\"version check\\\"}\"",
197
+ headers: {'Accept' => 'application/json',
198
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
199
+ 'Content-Type' => 'application/json',
200
+ 'User-Agent' => 'Ruby',
201
+ 'X-Rosetteapi-Key' => '0123456789'}).
202
+ to_return(status: 200, body: {'versionChecked': true}.to_json, headers: {})
203
+ stub_request(:post, 'https://api.rosette.com/rest/v1/entities').
204
+ with(body: request_file,
205
+ headers: {'Accept' => 'application/json',
206
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
207
+ 'Content-Type' => 'application/json',
208
+ 'User-Agent' => 'Ruby',
209
+ 'X-Rosetteapi-Key' => '0123456789'}).
210
+ to_return(status: 200, body: {'test': 'entities'}.to_json, headers: {})
211
+ end
212
+ it 'test entities' do
213
+ params = DocumentParameters.new
214
+ params.content = 'Bill Murray will appear in new Ghostbusters film: Dr. Peter Venkman was spotted filming a' \
215
+ ' cameo in Boston this… http://dlvr.it/BnsFfS'
216
+ response = RosetteAPI.new('0123456789').get_entities(params)
217
+ expect(response).instance_of? Hash
218
+ end
219
+ end
220
+
221
+ describe '.get_entities_linked' do
222
+ request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/entities_linked.json'))
223
+ before do
224
+ stub_request(:post, 'https://api.rosette.com/rest/v1/info?clientVersion=1.0.2').
225
+ with(body: "\"{\\\"body\\\": \\\"version check\\\"}\"",
226
+ headers: {'Accept' => 'application/json',
227
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
228
+ 'Content-Type' => 'application/json',
229
+ 'User-Agent' => 'Ruby',
230
+ 'X-Rosetteapi-Key' => '0123456789'}).
231
+ to_return(status: 200, body: {'versionChecked': true}.to_json, headers: {})
232
+ stub_request(:post, 'https://api.rosette.com/rest/v1/entities/linked').
233
+ with(body: request_file,
234
+ headers: {'Accept' => 'application/json',
235
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
236
+ 'Content-Type' => 'application/json',
237
+ 'User-Agent' => 'Ruby',
238
+ 'X-Rosetteapi-Key' => '0123456789'}).
239
+ to_return(status: 200, body: {'test': 'entities/linked'}.to_json, headers: {})
240
+ end
241
+ it 'test entities linked' do
242
+ params = DocumentParameters.new
243
+ params.content = 'Last month director Paul Feig announced the movie will have an all-star female cast including' \
244
+ ' Kristen Wiig, Melissa McCarthy, Leslie Jones and Kate McKinnon.'
245
+ response = RosetteAPI.new('0123456789').get_entities_linked(params)
246
+ expect(response).instance_of? Hash
247
+ end
248
+ end
249
+
250
+ describe '.get_categories' do
251
+ request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/categories.json'))
252
+ before do
253
+ stub_request(:post, 'https://api.rosette.com/rest/v1/info?clientVersion=1.0.2').
254
+ with(body: "\"{\\\"body\\\": \\\"version check\\\"}\"",
255
+ headers: {'Accept' => 'application/json',
256
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
257
+ 'Content-Type' => 'application/json',
258
+ 'User-Agent' => 'Ruby',
259
+ 'X-Rosetteapi-Key' => '0123456789'}).
260
+ to_return(status: 200, body: {'versionChecked': true}.to_json, headers: {})
261
+ stub_request(:post, 'https://api.rosette.com/rest/v1/categories').
262
+ with(body: request_file,
263
+ headers: {'Accept' => 'application/json',
264
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
265
+ 'Content-Type' => 'application/json',
266
+ 'User-Agent' => 'Ruby',
267
+ 'X-Rosetteapi-Key' => '0123456789'}).
268
+ to_return(status: 200, body: {'test': 'categories'}.to_json, headers: {})
269
+ end
270
+ it 'test categories' do
271
+ params = DocumentParameters.new
272
+ params.content_uri = 'http://www.onlocationvacations.com/2015/03/05/the-new-ghostbusters-movie-begins-filming-in-boston-in-june/'
273
+ response = RosetteAPI.new('0123456789').get_categories(params)
274
+ expect(response).instance_of? Hash
275
+ end
276
+ end
277
+
278
+ describe '.get_relationships' do
279
+ request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/relationships.json'))
280
+ before do
281
+ stub_request(:post, 'https://api.rosette.com/rest/v1/info?clientVersion=1.0.2').
282
+ with(body: "\"{\\\"body\\\": \\\"version check\\\"}\"",
283
+ headers: {'Accept' => 'application/json',
284
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
285
+ 'Content-Type' => 'application/json',
286
+ 'User-Agent' => 'Ruby',
287
+ 'X-Rosetteapi-Key' => '0123456789'}).
288
+ to_return(status: 200, body: {'versionChecked': true}.to_json, headers: {})
289
+ stub_request(:post, 'https://api.rosette.com/rest/v1/relationships').
290
+ with(body: request_file,
291
+ headers: {'Accept' => 'application/json',
292
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
293
+ 'Content-Type' => 'application/json',
294
+ 'User-Agent' => 'Ruby',
295
+ 'X-Rosetteapi-Key' => '0123456789'}).
296
+ to_return(status: 200, body: {'test': 'relationships'}.to_json, headers: {})
297
+ end
298
+ it 'test relationships' do
299
+ params = DocumentParameters.new
300
+ params.content = 'The Ghostbusters movie was filmed in Boston.'
301
+ response = RosetteAPI.new('0123456789').get_relationships(params)
302
+ expect(response).instance_of? Hash
303
+ end
304
+ end
305
+
306
+ describe '.name_translation' do
307
+ request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/name_translation.json')), encoding: 'utf-8'
308
+ before do
309
+ stub_request(:post, 'https://api.rosette.com/rest/v1/info?clientVersion=1.0.2').
310
+ with(body: "\"{\\\"body\\\": \\\"version check\\\"}\"",
311
+ headers: {'Accept' => 'application/json',
312
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
313
+ 'Content-Type' => 'application/json',
314
+ 'User-Agent' => 'Ruby',
315
+ 'X-Rosetteapi-Key' => '0123456789'}).
316
+ to_return(status: 200, body: {'versionChecked': true}.to_json, headers: {})
317
+ stub_request(:post, 'https://api.rosette.com/rest/v1/name-translation').
318
+ with(body: request_file,
319
+ headers: {'Accept' => 'application/json',
320
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
321
+ 'Content-Type' => 'application/json',
322
+ 'User-Agent' => 'Ruby',
323
+ 'X-Rosetteapi-Key' => '0123456789'}).
324
+ to_return(status: 200, body: {'test': 'name-translation'}.to_json, headers: {})
325
+ end
326
+ it 'test name translation' do
327
+ params = NameTranslationParameters.new('معمر محمد أبو منيار القذاف'.encode('UTF-8'), 'eng')
328
+ params.target_script = 'Latn'
329
+ response = RosetteAPI.new('0123456789').name_translation(params)
330
+ expect(response).instance_of? Hash
331
+ end
332
+ end
333
+
334
+ describe '.name_similarity' do
335
+ request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/name_similarity.json')), encoding: 'utf-8'
336
+ before do
337
+ stub_request(:post, 'https://api.rosette.com/rest/v1/info?clientVersion=1.0.2').
338
+ with(body: "\"{\\\"body\\\": \\\"version check\\\"}\"",
339
+ headers: {'Accept' => 'application/json',
340
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
341
+ 'Content-Type' => 'application/json',
342
+ 'User-Agent' => 'Ruby',
343
+ 'X-Rosetteapi-Key' => '0123456789'}).
344
+ to_return(status: 200, body: {'versionChecked': true}.to_json, headers: {})
345
+ stub_request(:post, 'https://api.rosette.com/rest/v1/name-similarity').
346
+ with(body: request_file,
347
+ headers: {'Accept' => 'application/json',
348
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
349
+ 'Content-Type' => 'application/json',
350
+ 'User-Agent' => 'Ruby',
351
+ 'X-Rosetteapi-Key' => '0123456789'}).
352
+ to_return(status: 200, body: {'test': 'name-similarity'}.to_json, headers: {})
353
+ end
354
+ it 'test name similarity' do
355
+ params = NameSimilarityParameters.new('Michael Jackson', '迈克尔·杰克逊')
356
+ response = RosetteAPI.new('0123456789').name_similarity(params)
357
+ expect(response).instance_of? Hash
358
+ end
359
+
360
+ it 'badRequestFormat: name2 option can only be an instance of a String or NameParameter' do
361
+ params = NameSimilarityParameters.new('Michael Jackson', 123)
362
+ expect { RosetteAPI.new('0123456789').name_similarity(params) }.to raise_error(BadRequestError)
363
+ end
364
+ end
365
+
366
+ describe '.get_tokens' do
367
+ request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/tokens.json')), encoding: 'utf-8'
368
+ before do
369
+ stub_request(:post, 'https://api.rosette.com/rest/v1/info?clientVersion=1.0.2').
370
+ with(body: "\"{\\\"body\\\": \\\"version check\\\"}\"",
371
+ headers: {'Accept' => 'application/json',
372
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
373
+ 'Content-Type' => 'application/json',
374
+ 'User-Agent' => 'Ruby',
375
+ 'X-Rosetteapi-Key' => '0123456789'}).
376
+ to_return(status: 200, body: {'versionChecked': true}.to_json, headers: {})
377
+ stub_request(:post, 'https://api.rosette.com/rest/v1/tokens').
378
+ with(body: request_file,
379
+ headers: {'Accept' => 'application/json',
380
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
381
+ 'Content-Type' => 'application/json',
382
+ 'User-Agent' => 'Ruby',
383
+ 'X-Rosetteapi-Key' => '0123456789'}).
384
+ to_return(status: 200, body: {'test': 'tokens'}.to_json, headers: {})
385
+ end
386
+ it 'test tokens' do
387
+ params = DocumentParameters.new
388
+ params.content = '北京大学生物系主任办公室内部会议'
389
+ response = RosetteAPI.new('0123456789').get_tokens(params)
390
+ expect(response).instance_of? Hash
391
+ end
392
+ end
393
+
394
+ describe '.get_sentences' do
395
+ request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/sentences.json'))
396
+ before do
397
+ stub_request(:post, 'https://api.rosette.com/rest/v1/info?clientVersion=1.0.2').
398
+ with(body: "\"{\\\"body\\\": \\\"version check\\\"}\"",
399
+ headers: {'Accept' => 'application/json',
400
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
401
+ 'Content-Type' => 'application/json',
402
+ 'User-Agent' => 'Ruby',
403
+ 'X-Rosetteapi-Key' => '0123456789'}).
404
+ to_return(status: 200, body: {'versionChecked': true}.to_json, headers: {})
405
+ stub_request(:post, 'https://api.rosette.com/rest/v1/sentences').
406
+ with(body: request_file,
407
+ headers: {'Accept' => 'application/json',
408
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
409
+ 'Content-Type' => 'application/json',
410
+ 'User-Agent' => 'Ruby',
411
+ 'X-Rosetteapi-Key' => '0123456789'}).
412
+ to_return(status: 200, body: {'test': 'sentences'}.to_json, headers: {})
413
+ end
414
+ it 'test sentences' do
415
+ params = DocumentParameters.new
416
+ params.content = 'This land is your land. This land is my land\nFrom California to the New York island;\nFrom' \
417
+ ' the wood forest to the Gulf Stream waters\n\nThis land was made for you and Me.\n\nAs I was' \
418
+ ' walking that ribbon of highway,\nI saw above me that endless skyway:\nI saw below me that' \
419
+ ' golden valley:\nThis land was made for you and me.'
420
+ response = RosetteAPI.new('0123456789').get_sentences(params)
421
+ expect(response).instance_of? Hash
422
+ end
423
+ end
424
+
425
+ describe '.info' do
426
+ before do
427
+ stub_request(:post, 'https://api.rosette.com/rest/v1/info?clientVersion=1.0.2').
428
+ with(body: "\"{\\\"body\\\": \\\"version check\\\"}\"",
429
+ headers: {'Accept' => 'application/json',
430
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
431
+ 'Content-Type' => 'application/json',
432
+ 'User-Agent' => 'Ruby',
433
+ 'X-Rosetteapi-Key' => '0123456789'}).
434
+ to_return(status: 200, body: {'versionChecked': true}.to_json, headers: {})
435
+ stub_request(:get, 'https://api.rosette.com/rest/v1/info').
436
+ with(headers: {'Accept' => '*/*',
437
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
438
+ 'User-Agent' => 'Ruby',
439
+ 'X-Rosetteapi-Key' => '0123456789'}).
440
+ to_return(status: 200, body: {'test': 'info'}.to_json, headers: {})
441
+ end
442
+ it 'test info' do
443
+ response = RosetteAPI.new('0123456789').info
444
+ expect(response).instance_of? Hash
445
+ end
446
+ end
447
+
448
+ describe '.ping' do
449
+ before do
450
+ stub_request(:post, 'https://api.rosette.com/rest/v1/info?clientVersion=1.0.2').
451
+ with(body: "\"{\\\"body\\\": \\\"version check\\\"}\"",
452
+ headers: {'Accept' => 'application/json',
453
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
454
+ 'Content-Type' => 'application/json',
455
+ 'User-Agent' => 'Ruby',
456
+ 'X-Rosetteapi-Key' => '0123456789'}).
457
+ to_return(status: 200, body: {'versionChecked': true}.to_json, headers: {})
458
+ stub_request(:get, 'https://api.rosette.com/rest/v1/ping').
459
+ with(headers: {'Accept' => '*/*',
460
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
461
+ 'User-Agent' => 'Ruby',
462
+ 'X-Rosetteapi-Key' => '0123456789'}).
463
+ to_return(status: 200, body: {'test': 'ping'}.to_json, headers: {})
464
+ end
465
+ it 'test ping' do
466
+ response = RosetteAPI.new('0123456789').ping
467
+ expect(response).instance_of? Hash
468
+ end
469
+ end
470
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rosette_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Basis Technology Corp
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubysl-securerandom
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: Rosette API gem
28
+ email: support@rosette.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/bad_request_error.rb
34
+ - lib/bad_request_format_error.rb
35
+ - lib/document_parameters.rb
36
+ - lib/name_parameter.rb
37
+ - lib/name_similarity_parameters.rb
38
+ - lib/name_translation_parameters.rb
39
+ - lib/request_builder.rb
40
+ - lib/rosette_api.rb
41
+ - lib/rosette_api_error.rb
42
+ - tests/tests_spec.rb
43
+ homepage: https://github.com/rosette-api/ruby
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: 2.3.0
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.6.4
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: Rosette API gem that supports multilingual text-analytics.
67
+ test_files:
68
+ - tests/tests_spec.rb