class Repustate
Public Class Methods
new(key, version='v3')
click to toggle source
# File lib/repustate.rb, line 23 def initialize(key, version='v3') @key = key @version = version end
Public Instance Methods
bulk_sentiment(options={:items => [], :lang => 'en'})
click to toggle source
Bulk score multiple pieces of text (not urls!).
# File lib/repustate.rb, line 35 def bulk_sentiment(options={:items => [], :lang => 'en'}) items_to_score = Hash[options[:items].enum_for(:each_with_index).collect { |val, i| ["text#{i}", val] }] call_api('bulk-score', items_to_score) end
clean_html(options={:url => nil})
click to toggle source
Clean up a web page. It doesn’t work well on home pages - it’s designed for content pages.
# File lib/repustate.rb, line 44 def clean_html(options={:url => nil}) use_http_get = true call_api('clean-html', options, use_http_get) end
entities(options={:text => nil, :lang => 'en'})
click to toggle source
# File lib/repustate.rb, line 49 def entities(options={:text => nil, :lang => 'en'}) use_http_get = true call_api('entities', options) end
sentiment(options={:text => nil, :lang => 'en'})
click to toggle source
Retrieve the sentiment for a single URl or block of text. Optionally select a language other than English.
# File lib/repustate.rb, line 30 def sentiment(options={:text => nil, :lang => 'en'}) call_api('score', options) end
topic(options={:text => nil, :topics => nil, :lang => 'en'})
click to toggle source
# File lib/repustate.rb, line 54 def topic(options={:text => nil, :topics => nil, :lang => 'en'}) use_http_get = true call_api('entities', options) end
Protected Instance Methods
call_api(api_function, args={}, use_http_get=false)
click to toggle source
# File lib/repustate.rb, line 85 def call_api(api_function, args={}, use_http_get=false) # Avoid sending empty parameters args = args.select { |key, value| value and not value.empty? } url = url_for_call(api_function) result = get_http_result(url, args, use_http_get) parse_result(result, api_function) end
get_http_result(url, args, use_http_get)
click to toggle source
# File lib/repustate.rb, line 65 def get_http_result(url, args, use_http_get) if use_http_get query = args.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&') url = url.concat("?#{query}") result = Net::HTTP.get_response(URI.parse(url)) else result = Net::HTTP.post_form(URI.parse(url), args) end if result.code != '200' raise "HTTP Error: #{result.code} #{result.body}" end result.body end
parse_result(result, api_function)
click to toggle source
# File lib/repustate.rb, line 81 def parse_result(result, api_function) return JSON.parse(result) end
url_for_call(api_function)
click to toggle source
# File lib/repustate.rb, line 61 def url_for_call(api_function) return "http://api.repustate.com/#{@version}/#{@key}/#{api_function}.json" end