rosette_api 1.27.1 → 1.37.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.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/README.md +22 -14
  4. data/examples/README.md +2 -2
  5. data/examples/address_similarity.rb +7 -6
  6. data/examples/categories.rb +7 -6
  7. data/examples/entities.rb +7 -6
  8. data/examples/events.rb +23 -0
  9. data/examples/info.rb +7 -6
  10. data/examples/language.rb +7 -6
  11. data/examples/language_multilingual.rb +7 -6
  12. data/examples/morphology_complete.rb +7 -6
  13. data/examples/morphology_compound-components.rb +7 -6
  14. data/examples/morphology_han-readings.rb +7 -6
  15. data/examples/morphology_lemmas.rb +7 -6
  16. data/examples/morphology_parts-of-speech.rb +7 -6
  17. data/examples/multipart_language_file.rb +29 -0
  18. data/examples/name_deduplication.rb +7 -6
  19. data/examples/name_similarity.rb +9 -7
  20. data/examples/name_translation.rb +7 -6
  21. data/examples/ping.rb +7 -6
  22. data/examples/record_similarity.rb +114 -0
  23. data/examples/relationships.rb +7 -6
  24. data/examples/semantic_vectors.rb +7 -6
  25. data/examples/sentences.rb +7 -6
  26. data/examples/sentiment.rb +8 -7
  27. data/examples/similar_terms.rb +7 -6
  28. data/examples/syntax_dependencies.rb +7 -6
  29. data/examples/tokens.rb +7 -6
  30. data/examples/topics.rb +7 -6
  31. data/examples/transliteration.rb +7 -6
  32. data/lib/address_parameter.rb +1 -1
  33. data/lib/address_similarity_parameters.rb +11 -3
  34. data/lib/bad_request_error.rb +1 -1
  35. data/lib/bad_request_format_error.rb +1 -1
  36. data/lib/document_parameters.rb +4 -5
  37. data/lib/name_deduplication_parameters.rb +2 -2
  38. data/lib/name_parameter.rb +20 -2
  39. data/lib/name_similarity_parameters.rb +49 -16
  40. data/lib/name_translation_parameters.rb +14 -4
  41. data/lib/record_similarity_parameters.rb +68 -0
  42. data/lib/request_builder.rb +89 -49
  43. data/lib/rosette_api.rb +78 -42
  44. data/lib/rosette_api_error.rb +3 -3
  45. metadata +25 -31
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'rosette_api'
5
+
6
+ api_key, url = ARGV
7
+
8
+ analytics_api = if url
9
+ RosetteAPI.new(api_key, url)
10
+ else
11
+ RosetteAPI.new(api_key)
12
+ end
13
+
14
+ begin
15
+ # Field names
16
+ primary_name_field = 'primaryName'
17
+ dob_field = 'dob'
18
+ dob2_field = 'dob2'
19
+ addr_field = 'addr'
20
+ str_field = 'jobTitle'
21
+ number_field = 'age'
22
+ bool_field = 'isRetired'
23
+
24
+ dob_hyphen = '1993-04-16'
25
+
26
+ # Request fields definition
27
+ fields = {
28
+ primary_name_field => { type: 'rni_name', weight: 0.5 },
29
+ dob_field => { type: 'rni_date', weight: 0.2 },
30
+ dob2_field => { type: 'rni_date', weight: 0.1 },
31
+ addr_field => { type: 'rni_address', weight: 0.5 },
32
+ str_field => { type: 'rni_string', weight: 0.2 },
33
+ number_field => { type: 'rni_number', weight: 0.4 },
34
+ bool_field => { type: 'rni_boolean', weight: 0.05 }
35
+ }
36
+
37
+ # Request properties
38
+ properties = {
39
+ threshold: 0.7,
40
+ includeExplainInfo: true
41
+ }
42
+
43
+ # Records payload. This shape mirrors the Java example's left/right lists.
44
+ #
45
+ # NOTE: The exact record field object schemas are API-specific. This example
46
+ # uses descriptive hashes that are intended to serialize cleanly to JSON.
47
+ records = {
48
+ left: [
49
+ {
50
+ primary_name_field => {
51
+ text: 'Ethan R',
52
+ entityType: 'PERSON',
53
+ language: 'eng',
54
+ languageOfOrigin: 'eng',
55
+ script: 'Latn'
56
+ },
57
+ dob_field => dob_hyphen,
58
+ dob2_field => {
59
+ date: '04161993',
60
+ format: 'MMddyyyy'
61
+ },
62
+ addr_field => '123 Roadlane Ave',
63
+ str_field => 'software engineer'
64
+ },
65
+ {
66
+ primary_name_field => {
67
+ text: 'Evan R'
68
+ },
69
+ dob_field => {
70
+ date: dob_hyphen
71
+ },
72
+ number_field => 47,
73
+ bool_field => false
74
+ }
75
+ ],
76
+ right: [
77
+ {
78
+ primary_name_field => {
79
+ text: 'Seth R',
80
+ language: 'eng'
81
+ },
82
+ dob_field => {
83
+ date: dob_hyphen
84
+ },
85
+ str_field => 'manager',
86
+ bool_field => true
87
+ },
88
+ {
89
+ primary_name_field => 'Ivan R',
90
+ dob_field => {
91
+ date: dob_hyphen
92
+ },
93
+ dob2_field => {
94
+ date: '1993/04/16'
95
+ },
96
+ addr_field => {
97
+ houseNumber: '123',
98
+ road: 'Roadlane Ave',
99
+ cityDistrict: 'Alpha'
100
+ },
101
+ number_field => 72,
102
+ bool_field => true
103
+ }
104
+ ]
105
+ }
106
+
107
+ params = RecordSimilarityParameters.new(fields, records, properties)
108
+ response = analytics_api.get_record_similarity(params)
109
+ puts JSON.pretty_generate(response)
110
+ rescue RosetteAPIError => e
111
+ printf('Rosette API Error (%<status_code>s): %<message>s',
112
+ status_code: e.status_code,
113
+ message: e.message)
114
+ end
@@ -1,20 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'json'
3
4
  require 'rosette_api'
4
5
 
5
6
  api_key, url = ARGV
6
7
 
7
- rosette_api = if url
8
- RosetteAPI.new(api_key, url)
9
- else
10
- RosetteAPI.new(api_key)
11
- end
8
+ analytics_api = if url
9
+ RosetteAPI.new(api_key, url)
10
+ else
11
+ RosetteAPI.new(api_key)
12
+ end
12
13
 
13
14
  relationships_text_data = 'FLIR Systems is headquartered in Oregon and produces thermal imaging, night vision, and infrared cameras and sensor systems. According to the SEC\'s order instituting a settled administrative proceeding, FLIR entered into a multi-million dollar contract to provide thermal binoculars to the Saudi government in November 2008. Timms and Ramahi were the primary sales employees responsible for the contract, and also were involved in negotiations to sell FLIR\'s security cameras to the same government officials. At the time, Timms was the head of FLIR\'s Middle East office in Dubai.'
14
15
 
15
16
  begin
16
17
  params = DocumentParameters.new(content: relationships_text_data)
17
- response = rosette_api.get_relationships(params)
18
+ response = analytics_api.get_relationships(params)
18
19
  puts JSON.pretty_generate(response)
19
20
  rescue RosetteAPIError => e
20
21
  printf('Rosette API Error (%<status_code>s): %<message>s',
@@ -1,19 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'json'
3
4
  require 'rosette_api'
4
5
 
5
6
  api_key, url = ARGV
6
7
 
7
- rosette_api = if url
8
- RosetteAPI.new(api_key, url)
9
- else
10
- RosetteAPI.new(api_key)
11
- end
8
+ analytics_api = if url
9
+ RosetteAPI.new(api_key, url)
10
+ else
11
+ RosetteAPI.new(api_key)
12
+ end
12
13
 
13
14
  semantic_vectors_data = 'Cambridge, Massachusetts'
14
15
  begin
15
16
  params = DocumentParameters.new(content: semantic_vectors_data)
16
- response = rosette_api.get_semantic_vectors(params)
17
+ response = analytics_api.get_semantic_vectors(params)
17
18
  puts JSON.pretty_generate(response)
18
19
  rescue RosetteAPIError => e
19
20
  printf('Rosette API Error (%<status_code>s): %<message>s',
@@ -1,21 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'json'
3
4
  require 'rosette_api'
4
5
 
5
6
  api_key, url = ARGV
6
7
 
7
- rosette_api = if url
8
- RosetteAPI.new(api_key, url)
9
- else
10
- RosetteAPI.new(api_key)
11
- end
8
+ analytics_api = if url
9
+ RosetteAPI.new(api_key, url)
10
+ else
11
+ RosetteAPI.new(api_key)
12
+ end
12
13
 
13
14
  sentences_data = 'This land is your land. This land is my land, from California to the New York island; from the red wood forest to the Gulf Stream waters. This land was made for you and Me. As I was walking that ribbon of highway, I saw above me that endless skyway: I saw below me that golden valley: This land was made for you and me.'
14
15
 
15
16
  begin
16
17
  params = DocumentParameters.new
17
18
  params.content = sentences_data
18
- response = rosette_api.get_sentences(params)
19
+ response = analytics_api.get_sentences(params)
19
20
  puts JSON.pretty_generate(response)
20
21
  rescue RosetteAPIError => e
21
22
  printf('Rosette API Error (%<status_code>s): %<message>s',
@@ -1,15 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'tempfile'
3
+ require 'json'
4
4
  require 'rosette_api'
5
+ require 'tempfile'
5
6
 
6
7
  api_key, url = ARGV
7
8
 
8
- rosette_api = if url
9
- RosetteAPI.new(api_key, url)
10
- else
11
- RosetteAPI.new(api_key)
12
- end
9
+ analytics_api = if url
10
+ RosetteAPI.new(api_key, url)
11
+ else
12
+ RosetteAPI.new(api_key)
13
+ end
13
14
 
14
15
  file = Tempfile.new(%w[foo .html])
15
16
  sentiment_file_data = '<html><head><title>New Ghostbusters Film</title></head><body><p>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."</p></body></html>'
@@ -17,7 +18,7 @@ file.write(sentiment_file_data)
17
18
  file.close
18
19
  begin
19
20
  params = DocumentParameters.new(file_path: file.path, language: 'eng')
20
- response = rosette_api.get_sentiment(params)
21
+ response = analytics_api.get_sentiment(params)
21
22
  puts JSON.pretty_generate(response)
22
23
  rescue RosetteAPIError => e
23
24
  printf('Rosette API Error (%<status_code>s): %<message>s',
@@ -1,20 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'json'
3
4
  require 'rosette_api'
4
5
 
5
6
  api_key, url = ARGV
6
7
 
7
- rosette_api = if url
8
- RosetteAPI.new(api_key, url)
9
- else
10
- RosetteAPI.new(api_key)
11
- end
8
+ analytics_api = if url
9
+ RosetteAPI.new(api_key, url)
10
+ else
11
+ RosetteAPI.new(api_key)
12
+ end
12
13
 
13
14
  similar_terms_data = 'spy'
14
15
  begin
15
16
  params = DocumentParameters.new(content: similar_terms_data)
16
17
  params.rosette_options = { 'resultLanguages' => %w[spa deu jpn] }
17
- response = rosette_api.get_similar_terms(params)
18
+ response = analytics_api.get_similar_terms(params)
18
19
  puts JSON.pretty_generate(response)
19
20
  rescue RosetteAPIError => e
20
21
  printf('Rosette API Error (%<status_code>s): %<message>s',
@@ -1,20 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'json'
3
4
  require 'rosette_api'
4
5
 
5
6
  api_key, url = ARGV
6
7
 
7
- rosette_api = if url
8
- RosetteAPI.new(api_key, url)
9
- else
10
- RosetteAPI.new(api_key)
11
- end
8
+ analytics_api = if url
9
+ RosetteAPI.new(api_key, url)
10
+ else
11
+ RosetteAPI.new(api_key)
12
+ end
12
13
 
13
14
  syntax_dependencies_data = 'Yoshinori Ohsumi, a Japanese cell biologist, was awarded the Nobel Prize in Physiology or Medicine on Monday.'
14
15
 
15
16
  begin
16
17
  params = DocumentParameters.new(content: syntax_dependencies_data)
17
- response = rosette_api.get_syntax_dependencies(params)
18
+ response = analytics_api.get_syntax_dependencies(params)
18
19
  puts JSON.pretty_generate(response)
19
20
  rescue RosetteAPIError => e
20
21
  printf('Rosette API Error (%<status_code>s): %<message>s',
data/examples/tokens.rb CHANGED
@@ -1,19 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'json'
3
4
  require 'rosette_api'
4
5
 
5
6
  api_key, url = ARGV
6
7
 
7
- rosette_api = if url
8
- RosetteAPI.new(api_key, url)
9
- else
10
- RosetteAPI.new(api_key)
11
- end
8
+ analytics_api = if url
9
+ RosetteAPI.new(api_key, url)
10
+ else
11
+ RosetteAPI.new(api_key)
12
+ end
12
13
 
13
14
  tokens_data = '北京大学生物系主任办公室内部会议'
14
15
  begin
15
16
  params = DocumentParameters.new(content: tokens_data)
16
- response = rosette_api.get_tokens(params)
17
+ response = analytics_api.get_tokens(params)
17
18
  puts JSON.pretty_generate(response)
18
19
  rescue RosetteAPIError => e
19
20
  printf('Rosette API Error (%<status_code>s): %<message>s',
data/examples/topics.rb CHANGED
@@ -1,20 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'json'
3
4
  require 'rosette_api'
4
5
 
5
6
  api_key, url = ARGV
6
7
 
7
- rosette_api = if url
8
- RosetteAPI.new(api_key, url)
9
- else
10
- RosetteAPI.new(api_key)
11
- end
8
+ analytics_api = if url
9
+ RosetteAPI.new(api_key, url)
10
+ else
11
+ RosetteAPI.new(api_key)
12
+ end
12
13
 
13
14
  topics_data = 'Lily Collins is in talks to join Nicholas Hoult in Chernin Entertainment and Fox Searchlight\'s J.R.R. Tolkien biopic Tolkien. Anthony Boyle, known for playing Scorpius Malfoy in the British play Harry Potter and the Cursed Child, also has signed on for the film centered on the famed author. In Tolkien, Hoult will play the author of the Hobbit and Lord of the Rings book series that were later adapted into two Hollywood trilogies from Peter Jackson. Dome Karukoski is directing the project.'
14
15
 
15
16
  begin
16
17
  params = DocumentParameters.new(content: topics_data)
17
- response = rosette_api.get_topics(params)
18
+ response = analytics_api.get_topics(params)
18
19
  puts JSON.pretty_generate(response)
19
20
  rescue RosetteAPIError => e
20
21
  printf('Rosette API Error (%<status_code>s): %<message>s',
@@ -1,21 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'json'
3
4
  require 'rosette_api'
4
5
 
5
6
  api_key, url = ARGV
6
7
 
7
- rosette_api = if url
8
- RosetteAPI.new(api_key, url)
9
- else
10
- RosetteAPI.new(api_key)
11
- end
8
+ analytics_api = if url
9
+ RosetteAPI.new(api_key, url)
10
+ else
11
+ RosetteAPI.new(api_key)
12
+ end
12
13
 
13
14
  transliteration_content_data = 'ana r2ye7 el gam3a el sa3a 3 el 3asr'
14
15
 
15
16
  begin
16
17
  params = DocumentParameters.new
17
18
  params.content = transliteration_content_data
18
- response = rosette_api.get_transliteration(params)
19
+ response = analytics_api.get_transliteration(params)
19
20
  puts JSON.pretty_generate(response)
20
21
  rescue RosetteAPIError => e
21
22
  printf('Rosette API Error (%<status_code>s): %<message>s',
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # This class represents an address in Rosette API.
3
+ # This class represents an address in Analytics API.
4
4
  class AddressParameter
5
5
  # house (optional)
6
6
  attr_accessor :house
@@ -4,16 +4,20 @@ require_relative 'bad_request_error'
4
4
  require_relative 'address_parameter'
5
5
 
6
6
  # This class encapsulates parameters that are needed for address-similarity in
7
- # Rosette API.
7
+ # Analytics API.
8
8
  class AddressSimilarityParameters
9
9
  # Address to be compared to address2
10
10
  attr_accessor :address1
11
11
  # Address to be compared to address1
12
12
  attr_accessor :address2
13
13
 
14
- def initialize(address1, address2) # :notnew:
14
+ # Parameters map sent to the API (optional, should be a hash)
15
+ attr_accessor :parameters
16
+
17
+ def initialize(address1, address2, address_parameters = nil) # :notnew:
15
18
  @address1 = address1
16
19
  @address2 = address2
20
+ @parameters = address_parameters
17
21
  end
18
22
 
19
23
  # Validates the parameters by checking if address1 and address2 are instances
@@ -24,6 +28,9 @@ class AddressSimilarityParameters
24
28
 
25
29
  a2_msg = 'address2 option can only be an instance of an AddressParameter or a String'
26
30
  raise BadRequestError.new(a2_msg) if [String, AddressParameter].none? { |clazz| @address2.is_a? clazz }
31
+
32
+ opt_msg = 'parameters can only be an instance of a Hash'
33
+ raise BadRequestError.new(opt_msg) if @parameters && !(@parameters.is_a? Hash)
27
34
  end
28
35
 
29
36
  # Converts this class to Hash with its keys in lower CamelCase.
@@ -42,7 +49,8 @@ class AddressSimilarityParameters
42
49
  def to_hash
43
50
  {
44
51
  address1: @address1.is_a?(AddressParameter) ? @address1.load_param : @address1,
45
- address2: @address2.is_a?(AddressParameter) ? @address2.load_param : @address2
52
+ address2: @address2.is_a?(AddressParameter) ? @address2.load_param : @address2,
53
+ parameters: @parameters
46
54
  }
47
55
  end
48
56
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  require_relative 'rosette_api_error'
4
4
 
5
- # This class represents Rosette API errors with badRequest status_code.
5
+ # This class represents Analytics API errors with badRequest status_code.
6
6
  class BadRequestError < RosetteAPIError
7
7
  def initialize(message) # :notnew:
8
8
  super('badRequest', message)
@@ -2,7 +2,7 @@
2
2
 
3
3
  require_relative 'rosette_api_error'
4
4
 
5
- # This class represents Rosette API errors with badRequestFormat status_code.
5
+ # This class represents Analytics API errors with badRequestFormat status_code.
6
6
  class BadRequestFormatError < RosetteAPIError
7
7
  def initialize(message) # :notnew:
8
8
  super('badRequestFormat', message)
@@ -17,9 +17,9 @@ class DocumentParameters
17
17
  attr_accessor :genre
18
18
  # ISO 639-3 language code of the provided content (optional)
19
19
  attr_accessor :language
20
- # Rosette API options (optional, should be a hash)
20
+ # API options (optional, should be a hash)
21
21
  attr_accessor :rosette_options
22
- # custom Rosette API headers
22
+ # custom API headers
23
23
  attr_accessor :custom_headers
24
24
 
25
25
  def initialize(options = {}) # :notnew:
@@ -55,8 +55,8 @@ class DocumentParameters
55
55
  raise BadRequestFormatError.new(content_msg)
56
56
  elsif [@content, @content_uri, @file_path].all?(&:nil?)
57
57
  raise BadRequestFormatError.new(no_content_msg)
58
- elsif @rosette_options
59
- raise BadRequestError.new(opt_msg) unless @rosette_options.is_a? Hash
58
+ elsif @rosette_options && !@rosette_options.is_a?(Hash)
59
+ raise BadRequestError.new(opt_msg)
60
60
  end
61
61
  end
62
62
 
@@ -78,7 +78,6 @@ class DocumentParameters
78
78
  content: @content,
79
79
  content_uri: @content_uri,
80
80
  file_path: @file_path,
81
- genre: @genre,
82
81
  language: @language,
83
82
  options: @rosette_options,
84
83
  custom_headers: @custom_headers
@@ -4,9 +4,9 @@ require_relative 'bad_request_error'
4
4
  require_relative 'name_parameter'
5
5
 
6
6
  # This class encapsulates parameters that are needed for name-deduplication in
7
- # Rosette API.
7
+ # Analytics API.
8
8
  class NameDeduplicationParameters
9
- # Rosette API options (optional, should be a hash)
9
+ # API options (optional, should be a hash)
10
10
  attr_accessor :rosette_options
11
11
  # List of Name objects to be de-duplicated
12
12
  attr_accessor :names
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # This class represents an entity name in Rosette API.
3
+ # This class represents an entity name in Analytics API.
4
4
  class NameParameter
5
5
  # Name's entity type (PERSON, LOCATION, ORGANIZATION) (optional)
6
6
  attr_accessor :entity_type
@@ -8,19 +8,36 @@ class NameParameter
8
8
  attr_accessor :language
9
9
  # ISO 15924 code of the name's script (optional)
10
10
  attr_accessor :script
11
+ # Name's gender (male, female, nonbinary) (optional)
12
+ attr_accessor :gender
11
13
  # Name to be analyzed
12
14
  attr_accessor :text
13
15
 
16
+ VALID_GENDERS = %w[male female nonbinary].freeze
17
+
14
18
  def initialize(text, options = {}) # :notnew:
15
19
  options = {
16
20
  entity_type: nil,
17
21
  language: nil,
18
- script: nil
22
+ script: nil,
23
+ gender: nil
19
24
  }.update options
20
25
  @text = text
21
26
  @entity_type = options[:entity_type]
22
27
  @language = options[:language]
23
28
  @script = options[:script]
29
+ @gender = options[:gender]
30
+
31
+ validate_gender
32
+ end
33
+
34
+ def validate_gender
35
+ return if @gender.nil?
36
+
37
+ normalized = @gender.to_s.downcase
38
+ return if VALID_GENDERS.include?(normalized)
39
+
40
+ raise ArgumentError.new("gender must be one of: #{VALID_GENDERS.join(', ')}")
24
41
  end
25
42
 
26
43
  # Converts this class to Hash with its keys in lower CamelCase.
@@ -40,6 +57,7 @@ class NameParameter
40
57
  entity_type: @entity_type,
41
58
  language: @language,
42
59
  script: @script,
60
+ gender: @gender,
43
61
  text: @text
44
62
  }
45
63
  end
@@ -4,26 +4,28 @@ require_relative 'bad_request_error'
4
4
  require_relative 'name_parameter'
5
5
 
6
6
  # This class encapsulates parameters that are needed for name-similarity in
7
- # Rosette API.
7
+ # Analytics API.
8
8
  class NameSimilarityParameters
9
9
  # genre to categorize the input data
10
10
  attr_accessor :genre
11
+ # Deprecated: Retained for backward compatibility. Use 'parameters' instead.
11
12
  # Rosette API options (optional, should be a hash)
12
13
  attr_accessor :rosette_options
14
+ # Parameters map sent to the API (optional, should be a hash)
15
+ attr_accessor :parameters
13
16
  # Name to be compared to name2
14
17
  attr_accessor :name1
15
18
  # Name to be compared to name1
16
19
  attr_accessor :name2
17
20
 
18
- def initialize(name1, name2, options = {}) # :notnew:
19
- options = {
20
- genre: nil,
21
- rosette_options: nil
22
- }.update options
23
- @genre = options[:genre]
21
+ def initialize(name1, name2, match_parameters = nil) # :notnew:
24
22
  @name1 = name1
25
23
  @name2 = name2
26
- @rosette_options = options[:rosette_options]
24
+ @genre = nil
25
+ @parameters = nil
26
+ @rosette_options = nil
27
+
28
+ handle_match_parameters(match_parameters) if match_parameters
27
29
  end
28
30
 
29
31
  # Validates the parameters by checking if name1 and name2 are instances of
@@ -35,8 +37,11 @@ class NameSimilarityParameters
35
37
  n2_msg = 'name2 option can only be an instance of a String or NameParameter'
36
38
  raise BadRequestError.new(n2_msg) if [String, NameParameter].none? { |clazz| @name2.is_a? clazz }
37
39
 
38
- opt_msg = 'rosette_options can only be an instance of a Hash'
39
- raise BadRequestError.new(opt_msg) if @rosette_options && !(@rosette_options.is_a? Hash)
40
+ opt_msg = 'parameters can only be an instance of a Hash'
41
+ raise BadRequestError.new(opt_msg) if @parameters && !(@parameters.is_a? Hash)
42
+
43
+ rosette_opt_msg = 'rosette_options can only be an instance of a Hash'
44
+ raise BadRequestError.new(rosette_opt_msg) if @rosette_options && !(@rosette_options.is_a? Hash)
40
45
  end
41
46
 
42
47
  # Converts this class to Hash with its keys in lower CamelCase.
@@ -53,11 +58,39 @@ class NameSimilarityParameters
53
58
  #
54
59
  # Returns the new Hash.
55
60
  def to_hash
56
- {
57
- genre: @genre,
58
- name1: @name1.is_a?(NameParameter) ? @name1.load_param : @name1,
59
- name2: @name2.is_a?(NameParameter) ? @name2.load_param : @name2,
60
- options: @rosette_options
61
- }
61
+ if @parameters
62
+ {
63
+ name1: @name1.is_a?(NameParameter) ? @name1.load_param : @name1,
64
+ name2: @name2.is_a?(NameParameter) ? @name2.load_param : @name2,
65
+ parameters: @parameters
66
+ }
67
+ else
68
+ {
69
+ genre: @genre,
70
+ name1: @name1.is_a?(NameParameter) ? @name1.load_param : @name1,
71
+ name2: @name2.is_a?(NameParameter) ? @name2.load_param : @name2,
72
+ options: @rosette_options
73
+ }
74
+ end
75
+ end
76
+
77
+ private
78
+
79
+ # Processes the 3rd argument to determine if it is using the legacy 'options' signature
80
+ # or the new 'match_parameters' signature, and assigns instance variables accordingly.
81
+ def handle_match_parameters(match_parameters)
82
+ if legacy_options?(match_parameters)
83
+ warn 'DEPRECATION WARNING: Passing `options` dynamically via NameSimilarityParameters.new is deprecated. Please pass `parameters` instead.'
84
+ @genre = match_parameters[:genre] || match_parameters['genre']
85
+ @rosette_options = match_parameters[:rosette_options] || match_parameters['rosette_options']
86
+ else
87
+ @parameters = match_parameters
88
+ end
89
+ end
90
+
91
+ def legacy_options?(params)
92
+ return false unless params.is_a?(Hash)
93
+
94
+ params.key?(:genre) || params.key?('genre') || params.key?(:rosette_options) || params.key?('rosette_options')
62
95
  end
63
96
  end