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:
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( = {}) #: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)
7 8 9 |
# File 'lib/document_parameters.rb', line 7 def content @content end |
#content_uri ⇒ Object
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_headers ⇒ Object
custom Rosette API headers
19 20 21 |
# File 'lib/document_parameters.rb', line 19 def custom_headers @custom_headers end |
#file_path ⇒ Object
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 |
#genre ⇒ Object
genre to categorize the input data
13 14 15 |
# File 'lib/document_parameters.rb', line 13 def genre @genre end |
#language ⇒ Object
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_options ⇒ Object
Rosette API options (optional, should be a hash)
17 18 19 |
# File 'lib/document_parameters.rb', line 17 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.
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_hash ⇒ Object
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_params ⇒ Object
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 |