class DocumentParameters

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

Attributes

content[RW]

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

content_uri[RW]

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

custom_headers[RW]

custom Rosette API headers

file_path[RW]

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

genre[RW]

genre to categorize the input data

language[RW]

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

rosette_options[RW]

Rosette API options (optional, should be a hash)

Public Instance Methods

load_params() click to toggle source

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

Returns the new Hash.

# File document_parameters.rb, line 66
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_hash() click to toggle source

Converts this class to Hash.

Returns the new Hash.

# File document_parameters.rb, line 76
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() click to toggle source

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

# File 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

Protected Instance Methods

initialize(options = {}) click to toggle source
# File 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