Class: NameTranslationParameters

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, target_language, options = {}) ⇒ NameTranslationParameters

:notnew:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/name_translation_parameters.rb', line 32

def initialize(name, target_language, options = {}) # :notnew:
  options = {
    entity_type: nil,
    genre: nil,
    maximum_results: nil,
    rosette_options: nil,
    source_language_of_origin: nil,
    source_language_of_use: nil,
    source_script: nil,
    target_scheme: nil,
    target_script: nil
  }.update options
  @name = name
  @entity_type = options[:entity_type]
  @genre = options[:genre]
  @maximum_results = options[:maximum_results]
  @rosette_options = options[:rosette_options]
  @source_language_of_origin = options[:source_language_of_origin]
  @source_language_of_use = options[:source_language_of_use]
  @source_script = options[:source_script]
  @target_language = target_language
  @target_scheme = options[:target_scheme]
  @target_script = options[:target_script]
end

Instance Attribute Details

#entity_typeObject

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



9
10
11
# File 'lib/name_translation_parameters.rb', line 9

def entity_type
  @entity_type
end

#genreObject

genre to categorize the input data



11
12
13
# File 'lib/name_translation_parameters.rb', line 11

def genre
  @genre
end

#maximum_resultsObject

Maximum number of results to return (optional)



30
31
32
# File 'lib/name_translation_parameters.rb', line 30

def maximum_results
  @maximum_results
end

#nameObject

Name to translate



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

def name
  @name
end

#rosette_optionsObject

API options (optional, should be a hash)



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

def rosette_options
  @rosette_options
end

#source_language_of_originObject

ISO 693-3 code of the name’s native language the name originates in (optional)



18
19
20
# File 'lib/name_translation_parameters.rb', line 18

def source_language_of_origin
  @source_language_of_origin
end

#source_language_of_useObject

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



20
21
22
# File 'lib/name_translation_parameters.rb', line 20

def source_language_of_use
  @source_language_of_use
end

#source_scriptObject

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



22
23
24
# File 'lib/name_translation_parameters.rb', line 22

def source_script
  @source_script
end

#target_languageObject

ISO 639-3 code of the translation language



24
25
26
# File 'lib/name_translation_parameters.rb', line 24

def target_language
  @target_language
end

#target_schemeObject

Transliteration scheme for the translation (optional)



26
27
28
# File 'lib/name_translation_parameters.rb', line 26

def target_scheme
  @target_scheme
end

#target_scriptObject

ISO 15924 code of name’s script (optional)



28
29
30
# File 'lib/name_translation_parameters.rb', line 28

def target_script
  @target_script
end

Instance Method Details

#load_paramsObject

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

Returns the new Hash.



73
74
75
76
77
78
# File 'lib/name_translation_parameters.rb', line 73

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.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/name_translation_parameters.rb', line 83

def to_hash
  {
    entity_type: @entity_type,
    name: @name,
    options: @rosette_options,
    source_language_of_origin: @source_language_of_origin,
    source_language_of_use: @source_language_of_use,
    source_script: @source_script,
    target_language: @target_language,
    target_scheme: @target_scheme,
    target_script: @target_script,
    maximum_results: @maximum_results
  }
end

#validate_paramsObject

Validates the parameters by checking if rosette_options is an instance of a Hash.

Raises:



59
60
61
62
63
64
65
66
67
68
# File 'lib/name_translation_parameters.rb', line 59

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

  max_msg = 'maximum_results can only be an instance of an Integer'
  raise BadRequestError.new(max_msg) if @maximum_results && !(@maximum_results.is_a? Integer)

  max_range_msg = 'maximum_results must be greater than or equal to 0'
  raise BadRequestError.new(max_range_msg) if @maximum_results&.negative?
end