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:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/document_parameters.rb', line 21

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)



7
8
9
# File 'lib/document_parameters.rb', line 7

def content
  @content
end

#content_uriObject

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



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

def content_uri
  @content_uri
end

#custom_headersObject

custom Rosette API headers



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

def custom_headers
  @custom_headers
end

#file_pathObject

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



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

def file_path
  @file_path
end

#genreObject

genre to categorize the input data



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

def genre
  @genre
end

#languageObject

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



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

def language
  @language
end

#rosette_optionsObject

Rosette API options (optional, should be a hash)



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

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.



58
59
60
61
62
63
# File 'lib/document_parameters.rb', line 58

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.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/document_parameters.rb', line 68

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.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/document_parameters.rb', line 42

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