diff --git a/examples/categories.rb b/examples/categories.rb index 8ec4971..a2e6328 100644 --- a/examples/categories.rb +++ b/examples/categories.rb @@ -8,11 +8,11 @@ rosette_api = RosetteAPI.new(api_key, url) end -categories_url_data = "http://www.onlocationvacations.com/2015/03/05/the-new-ghostbusters-movie-begins-filming-in-boston-in-june/" +categories_url_data = 'http://www.onlocationvacations.com/2015/03/05/the-new-ghostbusters-movie-begins-filming-in-boston-in-june/' begin - params = DocumentParameters.new(content_uri: categories_url_data) - response = rosette_api.get_categories(params) - puts JSON.pretty_generate(response) + params = DocumentParameters.new(content_uri: categories_url_data) + response = rosette_api.get_categories(params) + puts JSON.pretty_generate(response) rescue RosetteAPIError => rosette_api_error - printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message) + printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message) end diff --git a/examples/name_deduplication.rb b/examples/name_deduplication.rb new file mode 100644 index 0000000..d022949 --- /dev/null +++ b/examples/name_deduplication.rb @@ -0,0 +1,21 @@ +require 'rosette_api' + +api_key, url = ARGV + +if !url + rosette_api = RosetteAPI.new(api_key) +else + rosette_api = RosetteAPI.new(api_key, url) +end + +name_dedupe_data = 'John Smith,Johnathon Smith,Fred Jones' + +threshold = 0.75 +names = name_dedupe_data.split(',').map { |n| NameParameter.new(n) } +begin + params = NameDeduplicationParameters.new(names, threshold) + response = rosette_api.get_name_deduplication(params) + puts JSON.pretty_generate(response) +rescue RosetteAPIError => rosette_api_error + printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message) +end diff --git a/examples/name_similarity.rb b/examples/name_similarity.rb index f776a49..f03683a 100644 --- a/examples/name_similarity.rb +++ b/examples/name_similarity.rb @@ -8,13 +8,13 @@ rosette_api = RosetteAPI.new(api_key, url) end -matched_name_data1 = "Michael Jackson" -matched_name_data2 = "迈克尔·杰克逊" +matched_name_data1 = 'Michael Jackson' +matched_name_data2 = '迈克尔·杰克逊' begin - name1 = NameParameter.new(matched_name_data1, entity_type: 'PERSON', language:'eng') - params = NameSimilarityParameters.new(name1, matched_name_data2) - response = rosette_api.name_similarity(params) - puts JSON.pretty_generate(response) + name1 = NameParameter.new(matched_name_data1, entity_type: 'PERSON', language: 'eng') + params = NameSimilarityParameters.new(name1, matched_name_data2) + response = rosette_api.get_name_similarity(params) + puts JSON.pretty_generate(response) rescue RosetteAPIError => rosette_api_error - printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message) + printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message) end diff --git a/examples/name_translation.rb b/examples/name_translation.rb index b48ba1d..0d3ca25 100644 --- a/examples/name_translation.rb +++ b/examples/name_translation.rb @@ -8,11 +8,11 @@ rosette_api = RosetteAPI.new(api_key, url) end -translated_name_data = "معمر محمد أبو منيار القذاف" +translated_name_data = 'معمر محمد أبو منيار القذاف' begin - params = NameTranslationParameters.new(translated_name_data, 'eng', target_script: 'Latn') - response = rosette_api.name_translation(params) - puts JSON.pretty_generate(response) + params = NameTranslationParameters.new(translated_name_data, 'eng', target_script: 'Latn') + response = rosette_api.get_name_translation(params) + puts JSON.pretty_generate(response) rescue RosetteAPIError => rosette_api_error - printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message) + printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message) end diff --git a/examples/ping.rb b/examples/ping.rb index b96bcdc..8c9150c 100644 --- a/examples/ping.rb +++ b/examples/ping.rb @@ -8,8 +8,8 @@ rosette_api = RosetteAPI.new(api_key, url) end begin - response = rosette_api.ping - puts JSON.pretty_generate(response) + response = rosette_api.ping + puts JSON.pretty_generate(response) rescue RosetteAPIError => rosette_api_error - printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message) + printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message) end diff --git a/examples/relationships.rb b/examples/relationships.rb index 913a81d..d7b1e0b 100644 --- a/examples/relationships.rb +++ b/examples/relationships.rb @@ -10,10 +10,10 @@ relationships_text_data = "Bill Gates, Microsoft's former CEO, is a philanthropist." begin - params = DocumentParameters.new(content: relationships_text_data) - params.rosette_options = { accuracyMode: 'PRECISION' } - response = rosette_api.get_relationships(params) - puts JSON.pretty_generate(response) + params = DocumentParameters.new(content: relationships_text_data) + params.rosette_options = { accuracyMode: 'PRECISION' } + response = rosette_api.get_relationships(params) + puts JSON.pretty_generate(response) rescue RosetteAPIError => rosette_api_error - printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message) + printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message) end diff --git a/examples/sentences.rb b/examples/sentences.rb index 86f3734..afdb362 100644 --- a/examples/sentences.rb +++ b/examples/sentences.rb @@ -11,10 +11,10 @@ sentences_data = "This land is your land. This land is my land\nFrom California to the New York island;\nFrom the red wood forest to the Gulf Stream waters\n\nThis land was made for you and Me.\n\nAs I was walking that ribbon of highway,\nI saw above me that endless skyway:\nI saw below me that golden valley:\nThis land was made for you and me." begin - params = DocumentParameters.new - params.content = sentences_data - response = rosette_api.get_sentences(params) - puts JSON.pretty_generate(response) + params = DocumentParameters.new + params.content = sentences_data + response = rosette_api.get_sentences(params) + puts JSON.pretty_generate(response) rescue RosetteAPIError => rosette_api_error - printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message) + printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message) end diff --git a/examples/sentiment.rb b/examples/sentiment.rb index b0c6fba..016017a 100644 --- a/examples/sentiment.rb +++ b/examples/sentiment.rb @@ -9,14 +9,14 @@ rosette_api = RosetteAPI.new(api_key, url) end -file = Tempfile.new(%w(foo .html)) +file = Tempfile.new(%w[foo .html]) sentiment_file_data = '
Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, “The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.”
' file.write(sentiment_file_data) file.close begin - params = DocumentParameters.new(file_path: file.path, language: 'eng') - response = rosette_api.get_sentiment(params) - puts JSON.pretty_generate(response) + params = DocumentParameters.new(file_path: file.path, language: 'eng') + response = rosette_api.get_sentiment(params) + puts JSON.pretty_generate(response) rescue RosetteAPIError => rosette_api_error - printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message) + printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message) end diff --git a/examples/syntax_dependencies.rb b/examples/syntax_dependencies.rb index 95c36c9..4b40327 100644 --- a/examples/syntax_dependencies.rb +++ b/examples/syntax_dependencies.rb @@ -8,11 +8,11 @@ rosette_api = RosetteAPI.new(api_key, url) end -syntax_dependencies_data = "Yoshinori Ohsumi, a Japanese cell biologist, was awarded the Nobel Prize in Physiology or Medicine on Monday." +syntax_dependencies_data = 'Yoshinori Ohsumi, a Japanese cell biologist, was awarded the Nobel Prize in Physiology or Medicine on Monday.' begin - params = DocumentParameters.new(content: syntax_dependencies_data, genre: 'social-media') - response = rosette_api.get_syntax_dependencies(params) - puts JSON.pretty_generate(response) + params = DocumentParameters.new(content: syntax_dependencies_data) + response = rosette_api.get_syntax_dependencies(params) + puts JSON.pretty_generate(response) rescue RosetteAPIError => rosette_api_error - printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message) + printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message) end diff --git a/examples/text_embedding.rb b/examples/text_embedding.rb index 882760a..35e3cc4 100644 --- a/examples/text_embedding.rb +++ b/examples/text_embedding.rb @@ -10,9 +10,9 @@ embeddings_data = 'Cambridge, Massachusetts' begin - params = DocumentParameters.new(content: embeddings_data) - response = rosette_api.get_text_embedding(params) - puts JSON.pretty_generate(response) + params = DocumentParameters.new(content: embeddings_data) + response = rosette_api.get_text_embedding(params) + puts JSON.pretty_generate(response) rescue RosetteAPIError => rosette_api_error - printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message) + printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message) end diff --git a/examples/tokens.rb b/examples/tokens.rb index cebf3fc..de53adb 100644 --- a/examples/tokens.rb +++ b/examples/tokens.rb @@ -8,11 +8,11 @@ rosette_api = RosetteAPI.new(api_key, url) end -tokens_data = "北京大学生物系主任办公室内部会议" +tokens_data = '北京大学生物系主任办公室内部会议' begin - params = DocumentParameters.new(content: tokens_data) - response = rosette_api.get_tokens(params) - puts JSON.pretty_generate(response) + params = DocumentParameters.new(content: tokens_data) + response = rosette_api.get_tokens(params) + puts JSON.pretty_generate(response) rescue RosetteAPIError => rosette_api_error - printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message) + printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message) end diff --git a/examples/transliteration.rb b/examples/transliteration.rb new file mode 100644 index 0000000..8a4b2f1 --- /dev/null +++ b/examples/transliteration.rb @@ -0,0 +1,20 @@ +require 'rosette_api' + +api_key, url = ARGV + +if !url + rosette_api = RosetteAPI.new(api_key) +else + rosette_api = RosetteAPI.new(api_key, url) +end + +transliteration_content_data = 'Kareem Abdul Jabbar holds the records for most points in the NBA' + +begin + params = DocumentParameters.new + params.content = transliteration_content_data + response = rosette_api.get_transliteration(params) + puts JSON.pretty_generate(response) +rescue RosetteAPIError => rosette_api_error + printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message) +end diff --git a/lib/document_parameters.rb b/lib/document_parameters.rb index 71fa99a..edbfd68 100644 --- a/lib/document_parameters.rb +++ b/lib/document_parameters.rb @@ -47,7 +47,7 @@ def validate_params elsif [@content, @content_uri, @file_path].all?(&:nil?) raise BadRequestFormatError.new 'The format of the request is invalid: no content provided; must' \ ' be one of an attachment, an inline "content" field, or an external "contentUri"' - elsif !@rosette_options.nil? + elsif @rosette_options raise BadRequestError.new('rosette_options can only be an instance of a Hash') unless @rosette_options.is_a? Hash end end @@ -57,7 +57,7 @@ def validate_params # Returns the new Hash. def load_params validate_params - to_hash.select { |_key, value| !value.nil? } + to_hash.select { |_key, value| value } .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] } .to_h end diff --git a/lib/name_deduplication_parameters.rb b/lib/name_deduplication_parameters.rb new file mode 100644 index 0000000..c9e47e3 --- /dev/null +++ b/lib/name_deduplication_parameters.rb @@ -0,0 +1,56 @@ +require_relative 'bad_request_error' +require_relative 'name_parameter' + +# This class encapsulates parameters that are needed for name-deduplication in +# Rosette API. +class NameDeduplicationParameters + # Rosette API options (optional, should be a hash) + attr_accessor :rosette_options + # List of Name objects to be de-duplicated + attr_accessor :names + # Threshold for determining cluster size + attr_accessor :threshold + + def initialize(names, threshold, options = {}) #:notnew: + options = { + rosette_options: nil + }.update options + @names = names + @threshold = threshold + @rosette_options = options[:rosette_options] + end + + # Validates the parameters by checking if name1 and name2 are instances of + # a String or NameParameter. + def validate_params + raise BadRequestError.new('names must be an array of name_parameter') unless @names.instance_of? Array + if @threshold + raise BadRequestError.new('threshold must be a float') unless @threshold.is_a?(Float) + raise BadRequestError.new('threshold must be in the range of 0 to 1') if @threshold.negative? || @threshold > 1 + end + if @rosette_options + raise BadRequestError.new('rosette_options can only be an instance of a Hash') unless @rosette_options.is_a? Hash + end + end + + # Converts this class to Hash with its keys in lower CamelCase. + # + # Returns the new Hash. + def load_params + validate_params + to_hash.select { |_key, value| value } + .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] } + .to_h + end + + # Converts this class to Hash. + # + # Returns the new Hash. + def to_hash + { + names: @names.map(&:load_param), + threshold: @threshold, + options: @rosette_options + } + end +end diff --git a/lib/name_parameter.rb b/lib/name_parameter.rb index a67b484..421d5ed 100644 --- a/lib/name_parameter.rb +++ b/lib/name_parameter.rb @@ -25,9 +25,9 @@ def initialize(text, options = {}) #:notnew: # # Returns the new Hash. def load_param - self.to_hash.select { |_key, value| !value.nil? } - .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] } - .to_h + to_hash.select { |_key, value| value } + .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] } + .to_h end # Converts this class to Hash. diff --git a/lib/name_similarity_parameters.rb b/lib/name_similarity_parameters.rb index d6c4607..c3e69dd 100644 --- a/lib/name_similarity_parameters.rb +++ b/lib/name_similarity_parameters.rb @@ -27,11 +27,9 @@ def initialize(name1, name2, options = {}) #:notnew: # Validates the parameters by checking if name1 and name2 are instances of # a String or NameParameter. def validate_params - if [String, NameParameter].none? { |clazz| @name1.is_a? clazz } - raise BadRequestError.new('name1 option can only be an instance of a String or NameParameter') - elsif [String, NameParameter].none? { |clazz| @name2.is_a? clazz } - raise BadRequestError.new('name2 option can only be an instance of a String or NameParameter') - elsif !@rosette_options.nil? + raise BadRequestError.new('name1 option can only be an instance of a String or NameParameter') if [String, NameParameter].none? { |clazz| @name1.is_a? clazz } + raise BadRequestError.new('name2 option can only be an instance of a String or NameParameter') if [String, NameParameter].none? { |clazz| @name2.is_a? clazz } + if @rosette_options raise BadRequestError.new('rosette_options can only be an instance of a Hash') unless @rosette_options.is_a? Hash end end @@ -40,10 +38,10 @@ def validate_params # # Returns the new Hash. def load_params - self.validate_params - self.to_hash.select { |_key, value| !value.nil? } - .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] } - .to_h + validate_params + to_hash.reject { |_key, value| value.nil? } + .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] } + .to_h end # Converts this class to Hash. diff --git a/lib/name_translation_parameters.rb b/lib/name_translation_parameters.rb index f8135b0..9dc0265 100644 --- a/lib/name_translation_parameters.rb +++ b/lib/name_translation_parameters.rb @@ -49,7 +49,7 @@ def initialize(name, target_language, options = {}) #:notnew: # Validates the parameters by checking if rosette_options is an instance of a Hash. def validate_params - if !@rosette_options.nil? + if @rosette_options raise BadRequestError.new('rosette_options can only be an instance of a Hash') unless @rosette_options.is_a? Hash end end @@ -58,10 +58,10 @@ def validate_params # # Returns the new Hash. def load_params - self.validate_params - self.to_hash.select { |_key, value| !value.nil? } - .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] } - .to_h + validate_params + to_hash.select { |_key, value| value } + .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] } + .to_h end # Converts this class to Hash. diff --git a/lib/request_builder.rb b/lib/request_builder.rb index 1735440..12c8bd8 100644 --- a/lib/request_builder.rb +++ b/lib/request_builder.rb @@ -18,7 +18,6 @@ class RequestBuilder # Rosette API binding version attr_accessor :binding_version - def initialize(user_key, alternate_url, http_client, params = {}, url_parameters = nil, binding_version) @user_key = user_key @alternate_url = alternate_url @@ -28,7 +27,6 @@ def initialize(user_key, alternate_url, http_client, params = {}, url_parameters return unless url_parameters @alternate_url = @alternate_url + '?' + URI.encode_www_form(url_parameters) - end # Prepares a plain POST request for Rosette API. @@ -46,13 +44,15 @@ def prepare_plain_request(params) raise RosetteAPIError.new 'connectionError', 'Failed to establish connection with Rosette API server.' end - if params['customHeaders'] - keys_array = params['customHeaders'].keys - for k in keys_array - if k.to_s =~ /^X-RosetteAPI-/ - request[k] = params['customHeaders'][k] + custom_headers = params['customHeaders'] + + if custom_headers + keys_array = custom_headers.keys + for key in keys_array + if key.to_s =~ /^X-RosetteAPI-/ + request[key] = custom_headers[key] else - raise RosetteAPIError.new 'invalidHeader', 'Custom header must begin with "X-RosetteAPI-"' + raise RosetteAPIError.new 'invalidHeader', 'Custom header must begin with "X-RosetteAPI-"' end end params.delete 'customHeaders' @@ -108,15 +108,15 @@ def prepare_multipart_request(params) rescue raise RosetteAPIError.new 'connectionError', 'Failed to establish connection with Rosette API server.' end - + # add any custom headers from the user - if params['customHeaders'] != nil + unless params['customHeaders'].nil? keys_array = params['customHeaders'].keys for k in keys_array if k.to_s =~ /^X-RosetteAPI-/ request.add_field k, params['customHeaders'][k] else - raise RosetteAPIError.new 'invalidHeader', 'Custom header must begin with "X-RosetteAPI-"' + raise RosetteAPIError.new 'invalidHeader', 'Custom header must begin with "X-RosetteAPI-"' end end params.delete 'customHeaders' diff --git a/lib/rosette_api.rb b/lib/rosette_api.rb index df2627f..2384354 100644 --- a/lib/rosette_api.rb +++ b/lib/rosette_api.rb @@ -1,5 +1,6 @@ require_relative 'request_builder' require_relative 'document_parameters' +require_relative 'name_deduplication_parameters' require_relative 'name_translation_parameters' require_relative 'name_similarity_parameters' require_relative 'rosette_api_error' @@ -9,7 +10,7 @@ # This class allows you to access all Rosette API endpoints. class RosetteAPI # Version of Ruby binding - BINDING_VERSION = '1.5.0' + BINDING_VERSION = '1.7.0' # Rosette API language endpoint LANGUAGE_ENDPOINT = '/language'.freeze # Rosette API morphology endpoint @@ -22,6 +23,8 @@ class RosetteAPI RELATIONSHIPS_ENDPOINT = '/relationships'.freeze # Rosette API sentiment endpoint SENTIMENT_ENDPOINT = '/sentiment'.freeze + # Name Deduplication endpoint + NAME_DEDUPLICATION_ENDPOINT = '/name-deduplication'.freeze # Rosette API name-translation endpoint NAME_TRANSLATION_ENDPOINT = '/name-translation'.freeze # Rosette API name-similarity endpoint @@ -38,6 +41,8 @@ class RosetteAPI TEXT_EMBEDDING = '/text-embedding'.freeze # Syntactic Dependencies endpoint SYNTACTIC_DEPENDENCIES_ENDPOINT = '/syntax/dependencies'.freeze + # Transliteration endpoint + TRANSLITERATION_ENDPOINT = '/transliteration'.freeze # Rosette API key attr_accessor :user_key @@ -228,6 +233,22 @@ def get_sentiment(params) .send_post_request end + # De-duplicates a list of names. + # + # ==== Attributes + # + # * +params+ - NameDeduplicationParameters helps to build the request body in RequestBuilder. + # + # Returns the list of deduplicated names. + def get_name_deduplication(params) + check_params params, 'Expects a NameDeduplicationParameters type as an argument', NameDeduplicationParameters + + params = params.load_params + + RequestBuilder.new(@user_key, @alternate_url + NAME_DEDUPLICATION_ENDPOINT, @http_client, params, @url_parameters, BINDING_VERSION) + .send_post_request + end + # Translates a given name to a supported specified language. # # ==== Attributes @@ -235,7 +256,7 @@ def get_sentiment(params) # * +params+ - NameTranslationParameters helps to build the request body in RequestBuilder. # # Returns the translation of a name. - def name_translation(params) + def get_name_translation(params) check_params params, 'Expects a NameTranslationParameters type as an argument', NameTranslationParameters params = params.load_params @@ -252,7 +273,7 @@ def name_translation(params) # * +params+ - NameSimilarityParameters helps to build the request body in RequestBuilder. # # Returns the confidence score of matching 2 names. - def name_similarity(params) + def get_name_similarity(params) check_params params, 'Expects a NameSimilarityParameters type as an argument', NameSimilarityParameters params = params.load_params @@ -327,6 +348,23 @@ def get_syntax_dependencies(params) .send_post_request end + # + # Returns the transliteration of the content + # + # ==== Attributes + # + # * +params+ - DocumentParameters helps to build the request body in RequestBuilder. + # + # Returns the transliteration of the input. + def get_transliteration(params) + check_params params, 'Expects a DocumentParameters type as an argument', DocumentParameters + + params = params.load_params + + RequestBuilder.new(@user_key, @alternate_url + TRANSLITERATION_ENDPOINT, @http_client, params, @url_parameters, BINDING_VERSION) + .send_post_request + end + # Gets information about the Rosette API, returns name, build number # and build time. def info diff --git a/rosette_api.gemspec b/rosette_api.gemspec index 202c2c0..5f39c71 100644 --- a/rosette_api.gemspec +++ b/rosette_api.gemspec @@ -8,7 +8,7 @@ Gem::Specification.new do |s| s.required_ruby_version = '>= 2.0.0' s.name = 'rosette_api' - s.version = '1.5.0' + s.version = '1.7.0' s.license = 'MIT' s.summary = 'Rosette API gem that supports multilingual text-analytics.' @@ -19,7 +19,7 @@ Gem::Specification.new do |s| s.authors = ['Basis Technology Corp'] s.email = %q{support@rosette.com} s.homepage = %q{https://developer.rosette.com/} - s.date = %q{2016-12-19} + s.date = %q{2017-06-14} all_files = `git ls-files -z`.split("\x0") s.files = all_files.grep(%r{^(bin|lib)/|^.rubocop.yml$}) diff --git a/tests/tests_spec.rb b/tests/tests_spec.rb index 4ab6629..336870c 100644 --- a/tests/tests_spec.rb +++ b/tests/tests_spec.rb @@ -7,22 +7,22 @@ describe RosetteAPI do RSpec.configure do |config| - config.before(:example) { @content = 'Sample Content' } - config.before(:example) { @json = {:content => "Sample Content"}.to_json } + config.before(:example) { @content = 'Sample Content' } + config.before(:example) { @json = {content: 'Sample Content'}.to_json } end describe '.get_language' do before do - stub_request(:post, 'https://api.rosette.com/rest/v1/language'). - with(:body => @json, - :headers => {'Accept' => 'application/json', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Content-Type' => 'application/json', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789', - 'X-Rosetteapi-Binding' => 'ruby', - 'X-Rosetteapi-Binding-Version' => '1.5.0'}). - to_return(:status => 200, :body => "{\"test\": \"language\"}", :headers => {}) + stub_request(:post, 'https://api.rosette.com/rest/v1/language') + .with(body: @json, + headers: { 'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "language"}', headers: {}) end it 'test language' do params = DocumentParameters.new @@ -40,25 +40,23 @@ it 'badRequestFormat: the format of the request is invalid: no content provided;' do params = DocumentParameters.new - expect { RosetteAPI.new('0123456789') - .get_language(params) } - .to raise_error(BadRequestFormatError) + expect { RosetteAPI.new('0123456789').get_language(params) }.to raise_error(BadRequestFormatError) end end describe '.get_morphology_complete' do before do - stub_request(:post, 'https://api.rosette.com/rest/v1/morphology/complete'). - with(:body => @json, - headers: {'Accept' => 'application/json', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Content-Type' => 'application/json', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789', - 'X-Rosetteapi-Binding' => 'ruby', - 'X-Rosetteapi-Binding-Version' => '1.5.0'}). - to_return(:status => 200, :body => "{\"test\": \"morphology/complete\"}", :headers => {}) + stub_request(:post, 'https://api.rosette.com/rest/v1/morphology/complete') + .with(body: @json, + headers: { 'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "morphology/complete"}', headers: {}) end it 'test morphology complete' do params = DocumentParameters.new @@ -70,16 +68,16 @@ describe '.get_compound_components' do before do - stub_request(:post, 'https://api.rosette.com/rest/v1/morphology/compound-components'). - with(:body => @json, - headers: {'Accept' => 'application/json', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Content-Type' => 'application/json', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789', - 'X-Rosetteapi-Binding' => 'ruby', - 'X-Rosetteapi-Binding-Version' => '1.5.0'}). - to_return(:status => 200, :body => "{\"test\": \"morphology/compound-components\"}", :headers => {}) + stub_request(:post, 'https://api.rosette.com/rest/v1/morphology/compound-components') + .with(body: @json, + headers: { 'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "morphology/compound-components"}', headers: {}) end it 'test morphology compound components' do params = DocumentParameters.new @@ -91,16 +89,16 @@ describe '.get_han_readings' do before do - stub_request(:post, 'https://api.rosette.com/rest/v1/morphology/han-readings'). - with(:body => @json, - headers: {'Accept' => 'application/json', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Content-Type' => 'application/json', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789', - 'X-Rosetteapi-Binding' => 'ruby', - 'X-Rosetteapi-Binding-Version' => '1.5.0'}). - to_return(:status => 200, :body => "{\"test\": \"morphology/han-readings\"}", :headers => {}) + stub_request(:post, 'https://api.rosette.com/rest/v1/morphology/han-readings') + .with(body: @json, + headers: { 'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "morphology/han-readings"}', headers: {}) end it 'test morphology han readings' do params = DocumentParameters.new @@ -112,16 +110,16 @@ describe '.get_parts_of_speech' do before do - stub_request(:post, 'https://api.rosette.com/rest/v1/morphology/parts-of-speech'). - with(:body => @json, - headers: {'Accept' => 'application/json', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Content-Type' => 'application/json', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789', - 'X-Rosetteapi-Binding' => 'ruby', - 'X-Rosetteapi-Binding-Version' => '1.5.0'}). - to_return(:status => 200, :body => "{\"test\": \"morphology/parts-of-speech\"}", :headers => {}) + stub_request(:post, 'https://api.rosette.com/rest/v1/morphology/parts-of-speech') + .with(body: @json, + headers: { 'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "morphology/parts-of-speech"}', headers: {}) end it 'test morphology parts of speech' do params = DocumentParameters.new @@ -133,16 +131,16 @@ describe '.get_lemmas' do before do - stub_request(:post, 'https://api.rosette.com/rest/v1/morphology/lemmas'). - with(:body => @json, - headers: {'Accept' => 'application/json', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Content-Type' => 'application/json', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789', - 'X-Rosetteapi-Binding' => 'ruby', - 'X-Rosetteapi-Binding-Version' => '1.5.0'}). - to_return(:status => 200, :body => "{\"test\": \"morphology/lemmas\"}", :headers => {}) + stub_request(:post, 'https://api.rosette.com/rest/v1/morphology/lemmas') + .with(body: @json, + headers: { 'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "morphology/lemmas"}', headers: {}) end it 'test morphology lemmas' do params = DocumentParameters.new @@ -154,16 +152,16 @@ describe '.get_entities' do before do - stub_request(:post, 'https://api.rosette.com/rest/v1/entities'). - with(:body => @json, - headers: {'Accept' => 'application/json', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Content-Type' => 'application/json', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789', - 'X-Rosetteapi-Binding' => 'ruby', - 'X-Rosetteapi-Binding-Version' => '1.5.0'}). - to_return(:status => 200, :body => "{\"test\": \"entities\"}", :headers => {}) + stub_request(:post, 'https://api.rosette.com/rest/v1/entities') + .with(body: @json, + headers: { 'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "entities"}', headers: {}) end it 'test entities' do params = DocumentParameters.new @@ -175,17 +173,17 @@ describe '.get_entities_no_qids' do before do - no_qids_json = {:content => "Sample Content", :options => {:linkEntities => false}}.to_json - stub_request(:post, 'https://api.rosette.com/rest/v1/entities'). - with(:body => no_qids_json, - headers: {'Accept' => 'application/json', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Content-Type' => 'application/json', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789', - 'X-Rosetteapi-Binding' => 'ruby', - 'X-Rosetteapi-Binding-Version' => '1.5.0'}). - to_return(:status => 200, :body => "{\"test\": \"entities\"}", :headers => {}) + no_qids_json = { content: 'Sample Content', options: { linkEntities: false } }.to_json + stub_request(:post, 'https://api.rosette.com/rest/v1/entities') + .with(body: no_qids_json, + headers: { 'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "entities"}', headers: {}) end it 'test entities without qids' do params = DocumentParameters.new @@ -206,21 +204,21 @@ describe '.get_categories' do before do - categories_json = {:contentUri => "http://google.com"}.to_json - stub_request(:post, 'https://api.rosette.com/rest/v1/categories'). - with(:body => categories_json, - headers: {'Accept' => 'application/json', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Content-Type' => 'application/json', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789', - 'X-Rosetteapi-Binding' => 'ruby', - 'X-Rosetteapi-Binding-Version' => '1.5.0'}). - to_return(:status => 200, :body => "{\"test\": \"categories\"}", :headers => {}) + categories_json = { contentUri: 'http://google.com' }.to_json + stub_request(:post, 'https://api.rosette.com/rest/v1/categories') + .with(body: categories_json, + headers: { 'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "categories"}', headers: {}) end it 'test categories' do params = DocumentParameters.new - params.content_uri = "http://google.com" + params.content_uri = 'http://google.com' response = RosetteAPI.new('0123456789').get_categories(params) expect(response).instance_of? Hash end @@ -228,16 +226,16 @@ describe '.get_relationships' do before do - stub_request(:post, 'https://api.rosette.com/rest/v1/relationships'). - with(:body => @json, - headers: {'Accept' => 'application/json', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Content-Type' => 'application/json', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789', - 'X-Rosetteapi-Binding' => 'ruby', - 'X-Rosetteapi-Binding-Version' => '1.5.0'}). - to_return(:status => 200, :body => "{\"test\": \"relationships\"}", :headers => {}) + stub_request(:post, 'https://api.rosette.com/rest/v1/relationships') + .with(body: @json, + headers: { 'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "relationships"}', headers: {}) end it 'test relationships' do params = DocumentParameters.new @@ -249,79 +247,179 @@ describe '.name_translation' do before do - name_translation_json = {:name => "معمر محمد أبو منيار القذاف", :targetLanguage => "eng", :targetScript => "Latn"}.to_json - stub_request(:post, 'https://api.rosette.com/rest/v1/name-translation'). - with(:body => name_translation_json, - headers: {'Accept' => 'application/json', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Content-Type' => 'application/json', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789', - 'X-Rosetteapi-Binding' => 'ruby', - 'X-Rosetteapi-Binding-Version' => '1.5.0'}). - to_return(:status => 200, :body => "{\"test\": \"name-translation\"}", :headers => {}) + name_translation_json = { name: 'معمر محمد أبو منيار القذاف', targetLanguage: 'eng', targetScript: 'Latn' }.to_json + stub_request(:post, 'https://api.rosette.com/rest/v1/name-translation') + .with(body: name_translation_json, + headers: { 'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "name-translation"}', headers: {}) end it 'test name translation' do params = NameTranslationParameters.new('معمر محمد أبو منيار القذاف'.encode('UTF-8'), 'eng') params.target_script = 'Latn' - response = RosetteAPI.new('0123456789').name_translation(params) + response = RosetteAPI.new('0123456789').get_name_translation(params) expect(response).instance_of? Hash end - it 'badRequest: Expects NameTransaltionParameters type as an argument' do + it 'badRequest: Expects NameTranslationParameters type as an argument' do params = NameSimilarityParameters.new('Michael Jackson', '迈克尔·杰克逊') - expect { RosetteAPI.new('0123456789').name_translation(params) }.to raise_error(BadRequestError) + expect { RosetteAPI.new('0123456789').get_name_translation(params) }.to raise_error(BadRequestError) end end describe '.name_similarity' do before do - name_similarity_json = {:name1 => "Michael Jackson", :name2 => "迈克尔·杰克逊"}.to_json - stub_request(:post, 'https://api.rosette.com/rest/v1/name-similarity'). - with(:body => name_similarity_json, - headers: {'Accept' => 'application/json', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Content-Type' => 'application/json', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789', - 'X-Rosetteapi-Binding' => 'ruby', - 'X-Rosetteapi-Binding-Version' => '1.5.0'}). - to_return(:status => 200, :body => "{\"test\": \"name-similarity\"}", :headers => {}) + name_similarity_json = { name1: 'Michael Jackson', name2: '迈克尔·杰克逊' }.to_json + stub_request(:post, 'https://api.rosette.com/rest/v1/name-similarity') + .with(body: name_similarity_json, + headers: { 'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "name-similarity"}', headers: {}) end it 'test name similarity' do params = NameSimilarityParameters.new('Michael Jackson', '迈克尔·杰克逊') - response = RosetteAPI.new('0123456789').name_similarity(params) + response = RosetteAPI.new('0123456789').get_name_similarity(params) expect(response).instance_of? Hash end it 'badRequestFormat: name1 option can only be an instance of a String or NameParameter' do params = NameSimilarityParameters.new(123, 'Michael Jackson') - expect { RosetteAPI.new('0123456789').name_similarity(params) }.to raise_error(BadRequestError) + expect { RosetteAPI.new('0123456789').get_name_similarity(params) }.to raise_error(BadRequestError) end it 'badRequestFormat: name2 option can only be an instance of a String or NameParameter' do params = NameSimilarityParameters.new('Michael Jackson', 123) - expect { RosetteAPI.new('0123456789').name_similarity(params) }.to raise_error(BadRequestError) + expect { RosetteAPI.new('0123456789').get_name_similarity(params) }.to raise_error(BadRequestError) end it 'badRequest: Expects NameSimilarityParameters type as an argument' do params = NameTranslationParameters.new('معمر محمد أبو منيار القذاف'.encode('UTF-8'), 'eng') - expect { RosetteAPI.new('0123456789').name_similarity(params) }.to raise_error(BadRequestError) + expect { RosetteAPI.new('0123456789').get_name_similarity(params) }.to raise_error(BadRequestError) + end + end + + describe '.name_deduplication' do + names = ['John Smith', 'Johnathon Smith', 'Fred Jones'].map { |n| NameParameter.new(n) } + before do + names_json = { names: names.map(&:load_param), threshold: 0.75 }.to_json + + stub_request(:post, 'https://api.rosette.com/rest/v1/name-deduplication') + .with(body: names_json, + headers: {'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "name-deduplication"}', headers: {}) + + nothresh_json = { names: names.map(&:load_param) }.to_json + + stub_request(:post, 'https://api.rosette.com/rest/v1/name-deduplication') + .with(body: nothresh_json, + headers: {'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "name-deduplication"}', headers: {}) + end + it 'test name deduplication' do + params = NameDeduplicationParameters.new(names, 0.75) + response = RosetteAPI.new('0123456789').get_name_deduplication(params) + expect(response).instance_of? Hash + end + + it 'test null threshold' do + params = NameDeduplicationParameters.new(names, nil) + response = RosetteAPI.new('0123456789').get_name_deduplication(params) + expect(response).instance_of? Hash + end + + it 'badRequestFormat: names must be an array of name_parameter' do + params = NameDeduplicationParameters.new('Michael Jackson', 0.75) + expect { RosetteAPI.new('0123456789').get_name_deduplication(params) }.to raise_error(BadRequestError) + end + + it 'badRequestFormat: threshold must be a float' do + params = NameDeduplicationParameters.new(names, 123) + expect { RosetteAPI.new('0123456789').get_name_deduplication(params) }.to raise_error(BadRequestError) + end + + it 'badRequest: threshold must be in the range of 0 to 1' do + params = NameDeduplicationParameters.new(names, 1.5) + expect { RosetteAPI.new('0123456789').get_name_deduplication(params) }.to raise_error(BadRequestError) + end + + it 'badRequest: rosette_options can only be an instance of a Hash' do + params = NameDeduplicationParameters.new(names, 0.5) + params.rosette_options = 1 + expect { RosetteAPI.new('0123456789').get_name_deduplication(params) }.to raise_error(BadRequestError) + end + end + + describe '.transliteration' do + content = 'Kareem Abdul Jabbar holds the record for most points in the NBA' + + before do + transliteration_json = { content: content }.to_json + + stub_request(:post, 'https://api.rosette.com/rest/v1/transliteration') + .with(body: transliteration_json, + headers: {'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "transliteration"}', headers: {}) + end + it 'test transliteration' do + params = DocumentParameters.new + params.content = content + response = RosetteAPI.new('0123456789').get_transliteration(params) + expect(response).instance_of? Hash + end + + it 'badRequest: content must be provided' do + params = DocumentParameters.new + expect { RosetteAPI.new('0123456789').get_transliteration(params) }.to raise_error(BadRequestFormatError) + end + + it 'badRequest: rosette_options can only be an instance of a Hash' do + params = DocumentParameters.new + params.content = content + params.rosette_options = 1 + expect { RosetteAPI.new('0123456789').get_transliteration(params) }.to raise_error(BadRequestError) end end describe '.get_tokens' do before do - stub_request(:post, 'https://api.rosette.com/rest/v1/tokens'). - with(:body => @json, - headers: {'Accept' => 'application/json', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Content-Type' => 'application/json', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789', - 'X-Rosetteapi-Binding' => 'ruby', - 'X-Rosetteapi-Binding-Version' => '1.5.0'}). - to_return(:status => 200, :body => "{\"test\": \"tokens\"}", :headers => {}) + stub_request(:post, 'https://api.rosette.com/rest/v1/tokens') + .with(body: @json, + headers: { 'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "tokens"}', headers: {}) end it 'test tokens' do params = DocumentParameters.new @@ -333,16 +431,16 @@ describe '.get_sentences' do before do - stub_request(:post, 'https://api.rosette.com/rest/v1/sentences'). - with(:body => @json, - headers: {'Accept' => 'application/json', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Content-Type' => 'application/json', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789', - 'X-Rosetteapi-Binding' => 'ruby', - 'X-Rosetteapi-Binding-Version' => '1.5.0'}). - to_return(:status => 200, :body => "{\"test\": \"sentences\"}", :headers => {}) + stub_request(:post, 'https://api.rosette.com/rest/v1/sentences') + .with(body: @json, + headers: { 'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "sentences"}', headers: {}) end it 'test sentences' do params = DocumentParameters.new @@ -354,12 +452,12 @@ describe '.info' do before do - stub_request(:get, 'https://api.rosette.com/rest/v1/info'). - with(headers: {'Accept' => '*/*', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789'}). - to_return(:status => 200, :body => "{\"test\": \"info\"}", :headers => {}) + stub_request(:get, 'https://api.rosette.com/rest/v1/info') + .with(headers: { 'Accept' => '*/*', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789' }) + .to_return(status: 200, body: '{"test": "info"}', headers: {}) end it 'test info' do response = RosetteAPI.new('0123456789').info @@ -369,12 +467,12 @@ describe '.ping' do before do - stub_request(:get, 'https://api.rosette.com/rest/v1/ping'). - with(headers: {'Accept' => '*/*', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789'}). - to_return(:status => 200, :body => "{\"test\": \"ping\"}", :headers => {}) + stub_request(:get, 'https://api.rosette.com/rest/v1/ping') + .with(headers: { 'Accept' => '*/*', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789' }) + .to_return(status: 200, body: '{"test": "ping"}', headers: {}) end it 'test ping' do response = RosetteAPI.new('0123456789').ping @@ -384,35 +482,35 @@ describe '.get_language_custom_header' do before do - stub_request(:post, 'https://api.rosette.com/rest/v1/language'). - with(:body => @json, - headers: {'Accept' => 'application/json', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Content-Type' => 'application/json', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789', - 'X-Rosetteapi-Binding' => 'ruby', - 'X-Rosetteapi-Binding-Version' => '1.5.0', - 'X-RosetteApi-App' => 'ruby-app'}). - to_return(:status => 200, :body => "{\"test\": \"language\"}", :headers => {}) + stub_request(:post, 'https://api.rosette.com/rest/v1/language') + .with(body: @json, + headers: { 'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0', + 'X-RosetteApi-App' => 'ruby-app' }) + .to_return(status: 200, body: '{"test": "language"}', headers: {}) end it 'test custom_headers is invalid' do params = DocumentParameters.new params.content = 'Por favor Senorita, says the man.?' - params.custom_headers = {"test" => "ruby-app"} + params.custom_headers = {'test' => 'ruby-app'} expect { RosetteAPI.new('0123456789').get_language(params) }.to raise_error(RosetteAPIError) end end describe '.error_409_incompatible_client_version' do before do - stub_request(:get, 'https://api.rosette.com/rest/v1/info'). - with(headers: {'Accept' => '*/*', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789'}). - to_return(:status => 409, :body => "{\"code\": \"incompatibleClientVersion\"}", :headers => {}) + stub_request(:get, 'https://api.rosette.com/rest/v1/info') + .with(headers: { 'Accept' => '*/*', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789' }) + .to_return(status: 409, body: '{"code": "incompatibleClientVersion"}', headers: {}) end it 'test error 409 properly handled' do expect { RosetteAPI.new('0123456789').info }.to raise_error(RosetteAPIError) @@ -421,16 +519,16 @@ describe '.get_text_embedding' do before do - stub_request(:post, 'https://api.rosette.com/rest/v1/text-embedding'). - with(:body => @json, - :headers => {'Accept' => 'application/json', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Content-Type' => 'application/json', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789', - 'X-Rosetteapi-Binding' => 'ruby', - 'X-Rosetteapi-Binding-Version' => '1.5.0'}). - to_return(:status => 200, :body => "{\"test\": \"language\"}", :headers => {}) + stub_request(:post, 'https://api.rosette.com/rest/v1/text-embedding') + .with(body: @json, + headers: { 'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "language"}', headers: {}) end it 'test text_embedding' do params = DocumentParameters.new @@ -442,16 +540,16 @@ describe '.get_syntax_dependencies' do before do - stub_request(:post, 'https://api.rosette.com/rest/v1/syntax/dependencies'). - with(:body => @json, - :headers => {'Accept' => 'application/json', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Content-Type' => 'application/json', - 'User-Agent' => 'Ruby', - 'X-Rosetteapi-Key' => '0123456789', - 'X-Rosetteapi-Binding' => 'ruby', - 'X-Rosetteapi-Binding-Version' => '1.5.0'}). - to_return(:status => 200, :body => "{\"test\": \"language\"}", :headers => {}) + stub_request(:post, 'https://api.rosette.com/rest/v1/syntax/dependencies') + .with(body: @json, + headers: { 'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.7.0' }) + .to_return(status: 200, body: '{"test": "language"}', headers: {}) end it 'test syntax_dependencies' do params = DocumentParameters.new