Class: NameDeduplicationParameters

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(names, threshold, options = {}) ⇒ NameDeduplicationParameters

:notnew:



16
17
18
19
20
21
22
23
# File 'lib/name_deduplication_parameters.rb', line 16

def initialize(names, threshold, options = {}) # :notnew:
  options = {
    rosette_options: nil
  }.update options
  @names = names
  @threshold = threshold
  @rosette_options = options[:rosette_options]
end

Instance Attribute Details

#namesObject

List of Name objects to be de-duplicated



12
13
14
# File 'lib/name_deduplication_parameters.rb', line 12

def names
  @names
end

#rosette_optionsObject

API options (optional, should be a hash)



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

def rosette_options
  @rosette_options
end

#thresholdObject

Threshold for determining cluster size



14
15
16
# File 'lib/name_deduplication_parameters.rb', line 14

def threshold
  @threshold
end

Instance Method Details

#load_paramsObject

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

Returns the new Hash.



44
45
46
47
48
49
# File 'lib/name_deduplication_parameters.rb', line 44

def load_params
  validate_params
  to_hash
    .select { |_key, value| value }
    .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.



54
55
56
57
58
59
60
# File 'lib/name_deduplication_parameters.rb', line 54

def to_hash
  {
    names: @names.map(&:load_param),
    threshold: @threshold,
    options: @rosette_options
  }
end

#validate_paramsObject

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

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/name_deduplication_parameters.rb', line 27

def validate_params
  param_msg = 'names must be an array of name_parameter'
  raise BadRequestError.new(param_msg) unless @names.instance_of? Array

  float_msg = 'threshold must be a float'
  thresh_msg = 'threshold must be in the range of 0 to 1'
  if @threshold
    raise BadRequestError.new(float_msg) unless @threshold.is_a?(Float)
    raise BadRequestError.new(thresh_msg) if @threshold.negative? || @threshold > 1
  end
  opt_msg = 'rosette_options can only be an instance of a Hash'
  raise BadRequestError.new(opt_msg) if @rosette_options && !(@rosette_options.is_a? Hash)
end