Sha256: 465768985e4f9c5fb06d1ca8d50ae4675008dfaad5b63b840feca0627982ff87

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 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)
      @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)
      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

    # 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.0.0 lib/WatsonNLPWrapper.rb