Class: NameParameter
- Inherits:
-
Object
- Object
- NameParameter
- 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
-
#entity_type ⇒ Object
Name’s entity type (PERSON, LOCATION, ORGANIZATION) (optional).
-
#gender ⇒ Object
Name’s gender (male, female, nonbinary) (optional).
-
#language ⇒ Object
ISO 639-3 code of the name’s language (optional).
-
#script ⇒ Object
ISO 15924 code of the name’s script (optional).
-
#text ⇒ Object
Name to be analyzed.
Instance Method Summary collapse
-
#initialize(text, options = {}) ⇒ NameParameter
constructor
:notnew:.
-
#load_param ⇒ Object
Converts this class to Hash with its keys in lower CamelCase.
-
#to_hash ⇒ Object
Converts this class to Hash.
- #validate_gender ⇒ Object
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, = {}) # :notnew: = { entity_type: nil, language: nil, script: nil, gender: nil }.update @text = text @entity_type = [:entity_type] @language = [:language] @script = [:script] @gender = [:gender] validate_gender end |
Instance Attribute Details
#entity_type ⇒ Object
Name’s entity type (PERSON, LOCATION, ORGANIZATION) (optional)
6 7 8 |
# File 'lib/name_parameter.rb', line 6 def entity_type @entity_type end |
#gender ⇒ Object
Name’s gender (male, female, nonbinary) (optional)
12 13 14 |
# File 'lib/name_parameter.rb', line 12 def gender @gender end |
#language ⇒ Object
ISO 639-3 code of the name’s language (optional)
8 9 10 |
# File 'lib/name_parameter.rb', line 8 def language @language end |
#script ⇒ Object
ISO 15924 code of the name’s script (optional)
10 11 12 |
# File 'lib/name_parameter.rb', line 10 def script @script end |
#text ⇒ Object
Name to be analyzed
14 15 16 |
# File 'lib/name_parameter.rb', line 14 def text @text end |
Instance Method Details
#load_param ⇒ Object
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_hash ⇒ Object
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_gender ⇒ Object
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 |