Class: SentimentAnalysis::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/sentiment_analysis/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



9
10
11
# File 'lib/sentiment_analysis/client.rb', line 9

def initialize(options)
  @api_key = options[:api_key]
end

Instance Method Details

#quota(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sentiment_analysis/client.rb', line 14

def quota(options={})
  query = {:api_key => @api_key}

  case options[:format]
    when nil      then self.class.get('/quota.json', :query => query)['quota_remaining']
    when :json    then self.class.get('/quota.json', :query => query)
    when :xml     then self.class.get('/quota.xml',  :query => query).body
    else
      raise SentimentAnalysis::FormatError.new("Invalid format : #{options[:format]}")
  end
end

#review(options) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sentiment_analysis/client.rb', line 41

def review(options)
  raise ArgumentError.new(":text parameter is missing") unless options[:text]
  query = {:api_key => @api_key, :text => options[:text]}

  case options[:format]
    when nil, :json then self.class.get('/review.json', :query => query)
    when :xml       then self.class.get('/review.xml',  :query => query).body
    else
      raise SentimentAnalysis::FormatError.new("Invalid format : #{options[:format]}")
  end
end

#train(options) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sentiment_analysis/client.rb', line 27

def train(options)
  raise ArgumentError.new(":text parameter is missing") unless options[:text]
  raise ArgumentError.new(":mood parameter is missing") unless options[:mood]
  query = {:api_key => @api_key, :text => options[:text], :mood => options[:mood] }

  case options[:format]
    when nil, :json then self.class.get('/train.json', :query => query)
    when :xml       then self.class.get('/train.xml',  :query => query).body
    else
      raise SentimentAnalysis::FormatError.new("Invalid format : #{options[:format]}")
  end
end