Class: RecordSimilarityParameters
- Inherits:
-
Object
- Object
- RecordSimilarityParameters
- Defined in:
- lib/record_similarity_parameters.rb
Overview
This class encapsulates parameters that are needed for record-similarity in Analytics API.
Instance Attribute Summary collapse
-
#fields ⇒ Object
Field names/definitions used for similarity scoring (required).
-
#properties ⇒ Object
Optional properties map sent to the API (optional, should be a hash).
-
#records ⇒ Object
Records to be compared (required).
Instance Method Summary collapse
-
#initialize(fields, records, properties = nil) ⇒ RecordSimilarityParameters
constructor
:notnew:.
-
#load_params ⇒ Object
Converts this class to Hash with its keys in lower CamelCase.
-
#to_hash ⇒ Object
Converts this class to Hash.
-
#validate_params ⇒ Object
Validates the parameters by checking if required fields are present and optional properties is a Hash if provided.
Constructor Details
#initialize(fields, records, properties = nil) ⇒ RecordSimilarityParameters
:notnew:
17 18 19 20 21 |
# File 'lib/record_similarity_parameters.rb', line 17 def initialize(fields, records, properties = nil) # :notnew: @fields = fields @records = records @properties = properties end |
Instance Attribute Details
#fields ⇒ Object
Field names/definitions used for similarity scoring (required)
12 13 14 |
# File 'lib/record_similarity_parameters.rb', line 12 def fields @fields end |
#properties ⇒ Object
Optional properties map sent to the API (optional, should be a hash)
15 16 17 |
# File 'lib/record_similarity_parameters.rb', line 15 def properties @properties end |
#records ⇒ Object
Records to be compared (required)
9 10 11 |
# File 'lib/record_similarity_parameters.rb', line 9 def records @records end |
Instance Method Details
#load_params ⇒ Object
Converts this class to Hash with its keys in lower CamelCase.
Returns the new Hash.
51 52 53 54 55 56 |
# File 'lib/record_similarity_parameters.rb', line 51 def load_params validate_params to_hash .compact .transform_keys { |key| key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase) } end |
#to_hash ⇒ Object
Converts this class to Hash.
Returns the new Hash.
61 62 63 64 65 66 67 |
# File 'lib/record_similarity_parameters.rb', line 61 def to_hash { fields: @fields, records: @records, properties: @properties } end |
#validate_params ⇒ Object
Validates the parameters by checking if required fields are present and optional properties is a Hash if provided.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/record_similarity_parameters.rb', line 25 def validate_params f_msg = 'fields option is required' raise BadRequestError.new(f_msg) if @fields.nil? f_type_msg = 'fields can only be an instance of a Hash' raise BadRequestError.new(f_type_msg) unless @fields.is_a? Hash f_empty_msg = 'fields must not be empty' raise BadRequestError.new(f_empty_msg) if @fields.empty? r_msg = 'records option is required' raise BadRequestError.new(r_msg) if @records.nil? r_type_msg = 'records can only be an instance of a Hash' raise BadRequestError.new(r_type_msg) unless @records.is_a? Hash r_empty_msg = 'records must not be empty' raise BadRequestError.new(r_empty_msg) if @records.empty? p_msg = 'properties can only be an instance of a Hash' raise BadRequestError.new(p_msg) if @properties && !(@properties.is_a? Hash) end |