Class: NameParameter

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

Overview

This class represents an entity name in Analytics API.

Constant Summary collapse

VALID_GENDERS =
%w[male female nonbinary].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, options = {}) ⇒ NameParameter

:notnew:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/name_parameter.rb', line 18

def initialize(text, options = {}) # :notnew:
  options = {
    entity_type: nil,
    language: nil,
    script: nil,
    gender: nil
  }.update options
  @text = text
  @entity_type = options[:entity_type]
  @language = options[:language]
  @script = options[:script]
  @gender = options[:gender]

  validate_gender
end

Instance Attribute Details

#entity_typeObject

Name’s entity type (PERSON, LOCATION, ORGANIZATION) (optional)



6
7
8
# File 'lib/name_parameter.rb', line 6

def entity_type
  @entity_type
end

#genderObject

Name’s gender (male, female, nonbinary) (optional)



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

def gender
  @gender
end

#languageObject

ISO 639-3 code of the name’s language (optional)



8
9
10
# File 'lib/name_parameter.rb', line 8

def language
  @language
end

#scriptObject

ISO 15924 code of the name’s script (optional)



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

def script
  @script
end

#textObject

Name to be analyzed



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

def text
  @text
end

Instance Method Details

#load_paramObject

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

Returns the new Hash.



46
47
48
49
50
# File 'lib/name_parameter.rb', line 46

def load_param
  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.



55
56
57
58
59
60
61
62
63
# File 'lib/name_parameter.rb', line 55

def to_hash
  {
    entity_type: @entity_type,
    language: @language,
    script: @script,
    gender: @gender,
    text: @text
  }
end

#validate_genderObject

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
# File 'lib/name_parameter.rb', line 34

def validate_gender
  return if @gender.nil?

  normalized = @gender.to_s.downcase
  return if VALID_GENDERS.include?(normalized)

  raise ArgumentError.new("gender must be one of: #{VALID_GENDERS.join(', ')}")
end