class WatsonNLPWrapper::WatsonNLPApi

Public Class Methods

new(url, username, password, version = DEFAULT_VERSION) click to toggle source

Initialize instance variables for use later

# File lib/WatsonNLPWrapper.rb, line 12
def initialize(url, username, password, version = DEFAULT_VERSION)
  if url.nil? || username.nil? || password.nil? || version.nil?
    raise ArgumentError.new(NIL_ARGUMENT_ERROR)
  end
  @url = url
  @username = username
  @password = password
  @version = version
end

Public Instance Methods

analyze(text, features = default_features) click to toggle source

Sends a POST request to analyze text with certain features enabled

# File lib/WatsonNLPWrapper.rb, line 23
def analyze(text, features = default_features)
  if text.nil? || features.nil?
    raise ArgumentError.new(NIL_ARGUMENT_ERROR)
  end

  response = self.class.post(
    "#{@url}/analyze?version=#{@version}",
    body: {
      text: text.to_s,
      features: features
    }.to_json,
    basic_auth: auth,
    headers: {
      "Content-Type" => CONTENT_TYPE
    }
  )

  response.parsed_response
end

Private Instance Methods

auth() click to toggle source

Returns credentials used for basic auth

# File lib/WatsonNLPWrapper.rb, line 45
def auth
  {
    username: @username,
    password: @password
  }
end
default_features() click to toggle source

Default features if no features specified

# File lib/WatsonNLPWrapper.rb, line 53
def default_features
  {
    entities: {
      emotion: true,
      sentiment: true,
      limit: 2
    },
    keywords: {
      emotion: true,
      sentiment: true,
      limit: 2
    }
  }
end