Class: DocumentParameters

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

Overview

This class encapsulates parameters that will be used by most of the endpoints with exclusion of name-similarity and name-translation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DocumentParameters

:notnew:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/document_parameters.rb', line 25

def initialize(options = {}) #:notnew:
  options = {
    content: nil,
    content_uri: nil,
    file_path: nil,
    genre: nil,
    language: nil,
    rosette_options: nil,
    custom_headers: nil
  }.update options
  @content = options[:content]
  @content_uri = options[:content_uri]
  @file_path = options[:file_path]
  @genre = options[:genre]
  @language = options[:language]
  @rosette_options = options[:rosette_options]
  @custom_headers = options[:custom_headers]
end

Instance Attribute Details

#contentObject

Content to be analyzed (required if no content_uri and file_path)



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

def content
  @content
end

#content_uriObject

URL to retrieve content from and analyze (required if no content and file_path)



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

def content_uri
  @content_uri
end

#custom_headersObject

custom Rosette API headers



23
24
25
# File 'lib/document_parameters.rb', line 23

def custom_headers
  @custom_headers
end

#file_pathObject

File path of the file to be analyzed (required if no content and content_uri)



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

def file_path
  @file_path
end

#genreObject

genre to categorize the input data



17
18
19
# File 'lib/document_parameters.rb', line 17

def genre
  @genre
end

#languageObject

ISO 639-3 language code of the provided content (optional)



19
20
21
# File 'lib/document_parameters.rb', line 19

def language
  @language
end

#rosette_optionsObject

Rosette API options (optional, should be a hash)



21
22
23
# File 'lib/document_parameters.rb', line 21

def rosette_options
  @rosette_options
end

Instance Method Details

#load_paramsObject

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

Returns the new Hash.



66
67
68
69
70
71
72
# File 'lib/document_parameters.rb', line 66

def load_params
  validate_params
  to_hash
    .select { |_key, value| value }
    .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
    .to_h
end

#to_hashObject

Converts this class to Hash.

Returns the new Hash.



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/document_parameters.rb', line 77

def to_hash
  {
    content: @content,
    content_uri: @content_uri,
    file_path: @file_path,
    genre: @genre,
    language: @language,
    options: @rosette_options,
    custom_headers: @custom_headers
  }
end

#validate_paramsObject

Validates the parameters by checking if there are multiple content sources set or no content provided at all.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/document_parameters.rb', line 46

def validate_params
  content_msg = 'The format of the request is invalid: multiple content ' \
    'sources; must be one of an attachment, an inline "content" field, or ' \
    'an external "contentUri"'
  no_content_msg = 'The format of the request is invalid: no content ' \
    'provided; must be one of an attachment, an inline "content" field, or ' \
    'an external "contentUri"'
  opt_msg = 'rosette_options can only be an instance of a Hash'
  if [@content, @content_uri, @file_path].compact.length > 1
    raise BadRequestFormatError.new(content_msg)
  elsif [@content, @content_uri, @file_path].all?(&:nil?)
    raise BadRequestFormatError.new(no_content_msg)
  elsif @rosette_options
    raise BadRequestError.new(opt_msg) unless @rosette_options.is_a? Hash
  end
end