Sha256: 0b825d97debc458daad0d255cf42f846751ef5e69f9865fdd9271837d7d62342

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require 'json'
require 'httparty'

require 'WatsonNLPWrapper/version'
require 'WatsonNLPWrapper/constants'

module WatsonNLPWrapper
  class WatsonNLPApi
    include HTTParty
    include WatsonNLPWrapper::Constants
    # Initialize instance variables for use later
    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

    # Sends a POST request to analyze text with certain features enabled
    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
      # Returns credentials used for basic auth
      def auth
        {
          username: @username,
          password: @password
        }
      end

      # Default features if no features specified
      def default_features
        {
          entities: {
            emotion: true,
            sentiment: true,
            limit: 2
          },
          keywords: {
            emotion: true,
            sentiment: true,
            limit: 2
          }
        }
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
WatsonNLPWrapper-1.1.0 lib/WatsonNLPWrapper.rb