Class: NameSimilarityParameters

Inherits:
Object
  • Object
show all
Defined in:
lib/name_similarity_parameters.rb

Overview

This class encapsulates parameters that are needed for name-similarity in Analytics API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name1, name2, match_parameters = nil) ⇒ NameSimilarityParameters

:notnew:



21
22
23
24
25
26
27
28
29
# File 'lib/name_similarity_parameters.rb', line 21

def initialize(name1, name2, match_parameters = nil) # :notnew:
  @name1 = name1
  @name2 = name2
  @genre = nil
  @parameters = nil
  @rosette_options = nil

  handle_match_parameters(match_parameters) if match_parameters
end

Instance Attribute Details

#genreObject

genre to categorize the input data



10
11
12
# File 'lib/name_similarity_parameters.rb', line 10

def genre
  @genre
end

#name1Object

Name to be compared to name2



17
18
19
# File 'lib/name_similarity_parameters.rb', line 17

def name1
  @name1
end

#name2Object

Name to be compared to name1



19
20
21
# File 'lib/name_similarity_parameters.rb', line 19

def name2
  @name2
end

#parametersObject

Parameters map sent to the API (optional, should be a hash)



15
16
17
# File 'lib/name_similarity_parameters.rb', line 15

def parameters
  @parameters
end

#rosette_optionsObject

Deprecated: Retained for backward compatibility. Use ‘parameters’ instead. Rosette API options (optional, should be a hash)



13
14
15
# File 'lib/name_similarity_parameters.rb', line 13

def rosette_options
  @rosette_options
end

Instance Method Details

#load_paramsObject

Converts this class to Hash with its keys in lower CamelCase.

Returns the new Hash.



50
51
52
53
54
55
# File 'lib/name_similarity_parameters.rb', line 50

def load_params
  validate_params
  to_hash
    .compact
    .transform_keys { |key| key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase) }
end

#to_hashObject

Converts this class to Hash.

Returns the new Hash.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/name_similarity_parameters.rb', line 60

def to_hash
  if @parameters
    {
      name1: @name1.is_a?(NameParameter) ? @name1.load_param : @name1,
      name2: @name2.is_a?(NameParameter) ? @name2.load_param : @name2,
      parameters: @parameters
    }
  else
    {
      genre: @genre,
      name1: @name1.is_a?(NameParameter) ? @name1.load_param : @name1,
      name2: @name2.is_a?(NameParameter) ? @name2.load_param : @name2,
      options: @rosette_options
    }
  end
end

#validate_paramsObject

Validates the parameters by checking if name1 and name2 are instances of a String or NameParameter.

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/name_similarity_parameters.rb', line 33

def validate_params
  n1_msg = 'name1 option can only be an instance of a String or NameParameter'
  raise BadRequestError.new(n1_msg) if [String, NameParameter].none? { |clazz| @name1.is_a? clazz }

  n2_msg = 'name2 option can only be an instance of a String or NameParameter'
  raise BadRequestError.new(n2_msg) if [String, NameParameter].none? { |clazz| @name2.is_a? clazz }

  opt_msg = 'parameters can only be an instance of a Hash'
  raise BadRequestError.new(opt_msg) if @parameters && !(@parameters.is_a? Hash)

  rosette_opt_msg = 'rosette_options can only be an instance of a Hash'
  raise BadRequestError.new(rosette_opt_msg) if @rosette_options && !(@rosette_options.is_a? Hash)
end