Class: DocumentParameters
- Inherits:
-
Object
- Object
- DocumentParameters
- 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
-
#content ⇒ Object
Content to be analyzed (required if no content_uri and file_path).
-
#content_uri ⇒ Object
URL to retrieve content from and analyze (required if no content and file_path).
-
#custom_headers ⇒ Object
custom Rosette API headers.
-
#file_path ⇒ Object
File path of the file to be analyzed (required if no content and content_uri).
-
#genre ⇒ Object
genre to categorize the input data.
-
#language ⇒ Object
ISO 639-3 language code of the provided content (optional).
-
#rosette_options ⇒ Object
Rosette API options (optional, should be a hash).
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ DocumentParameters
constructor
:notnew:.
-
#load_params ⇒ Object
Converts this class to Hash with its keys in lower CamelCase.
-
#to_hash ⇒ Object
Converts this class to Hash.
-
#validate_params ⇒ Object
Validates the parameters by checking if there are multiple content sources set or no content provided at all.
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( = {}) #:notnew: = { content: nil, content_uri: nil, file_path: nil, genre: nil, language: nil, rosette_options: nil, custom_headers: nil }.update @content = [:content] @content_uri = [:content_uri] @file_path = [:file_path] @genre = [:genre] @language = [:language] @rosette_options = [:rosette_options] @custom_headers = [:custom_headers] end |
Instance Attribute Details
#content ⇒ Object
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_uri ⇒ Object
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_headers ⇒ Object
custom Rosette API headers
23 24 25 |
# File 'lib/document_parameters.rb', line 23 def custom_headers @custom_headers end |
#file_path ⇒ Object
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 |
#genre ⇒ Object
genre to categorize the input data
17 18 19 |
# File 'lib/document_parameters.rb', line 17 def genre @genre end |
#language ⇒ Object
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_options ⇒ Object
Rosette API options (optional, should be a hash)
21 22 23 |
# File 'lib/document_parameters.rb', line 21 def @rosette_options end |
Instance Method Details
#load_params ⇒ Object
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_hash ⇒ Object
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_params ⇒ Object
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 |