Class: WatsonNLPWrapper::WatsonNLPApi

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Constants
Defined in:
lib/WatsonNLPWrapper.rb

Constant Summary

Constants included from Constants

Constants::CONTENT_TYPE, Constants::DEFAULT_VERSION, Constants::NIL_ARGUMENT_ERROR

Instance Method Summary collapse

Constructor Details

#initialize(url, username, password, version = DEFAULT_VERSION) ⇒ WatsonNLPApi

Initialize instance variables for use later



12
13
14
15
16
17
18
19
20
# 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

Instance Method Details

#analyze(text, features = default_features) ⇒ Object

Sends a POST request to analyze text with certain features enabled



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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