Commits

Anonymous committed 2532e27

Updated ruby client to only use the json version of the api.

  • Participants
  • Parent commits b921fb6

Comments (0)

Files changed (2)

File ruby/repustate.rb

 require 'base64'
 require 'cgi'
 require 'net/http'
-require 'rexml/document'
 
 require 'rubygems'
 require 'json'
   end
 
   # Retrieve the sentiment for a single URl or block of text.
-  def sentiment(options={:text => nil, :url => nil}, response_type='json')
-    call_api('score', options, response_type)
+  def sentiment(options={:text => nil, :url => nil})
+    call_api('score', options)
   end
 
   # Bulk score multiple pieces of text (not urls!).
-  def bulk_sentiment(options={:items => []}, response_type='json')
+  def bulk_sentiment(options={:items => []})
     items_to_score = Hash[options[:items].enum_for(:each_with_index).collect {
                             |val, i| ["text#{i}", val]
                           }]
-    call_api('bulk-score', items_to_score, response_type)
+    call_api('bulk-score', items_to_score)
   end
 
   # Clean up a web page. It doesn't work well on home pages - it's
   # designed for content pages.
-  def clean_html(options={:url => nil}, response_type='json')
+  def clean_html(options={:url => nil})
     use_http_get = true
-    call_api('clean-html', options, response_type, use_http_get)
+    call_api('clean-html', options, use_http_get)
   end
 
-  def adjectives(options={:cloud => nil, :text => nil, :url => nil},
-                 response_type='json')
-    call_natural_language('adj', options, response_type)
+  def adjectives(options={:cloud => nil, :text => nil, :url => nil})
+    call_natural_language('adj', options)
   end
 
-  def verbs(options={:cloud => nil, :text => nil, :url => nil},
-            response_type='json')
-    call_natural_language('verb', options, response_type)
+  def verbs(options={:cloud => nil, :text => nil, :url => nil})
+    call_natural_language('verb', options)
   end
 
   def ngrams(options={
                :min => nil,
                :freq => nil,
                :stopwords => nil,
-             }, response_type='json')
+             })
     use_http_get = (options[:text] == nil or options[:text].empty?)
-    call_api('ngrams', options, response_type, use_http_get)
+    call_api('ngrams', options, use_http_get)
   end
 
   # Convert english date indicators like "today", "tomorrow", "next week"
   # into date strings like 2011-01-12.
-  def date_extraction(options={:text => nil}, response_type='json')
-    call_api('extract-dates', options, response_type)
+  def date_extraction(options={:text => nil})
+    call_api('extract-dates', options)
   end
 
   # Given a list of images and titles, generate a simple powerpoint
                   :author => '',
                   :images => [],
                   :titles => [],
-                }, response_type=nil)
+                })
     args = {
       :title => options[:report_title],
       :author => options[:author],
 
   protected
 
-  def url_for_call(api_function, response_type)
+  def url_for_call(api_function)
     if api_function == 'powerpoint'
       return "http://api.repustate.com/#{@version}/#{@key}/powerpoint/"
     else
-      return "http://api.repustate.com/#{@version}/#{@key}/#{api_function}" +
-          ".#{response_type}"
+      return "http://api.repustate.com/#{@version}/#{@key}/#{api_function}.json"
     end
   end
 
     result.body
   end
 
-  def parse_result(result, api_function, response_type)
+  def parse_result(result, api_function)
     if api_function == 'powerpoint'
       return result
-    elsif response_type == 'json'
+    else
+      # Default is JSON
       return JSON.parse(result)
-    else
-      # Default is XML
-      return REXML::Document.new(result)
     end
   end
 
-  def call_api(api_function, args={}, response_type='json', use_http_get=false)
-    if not ['json', 'xml'].include?(response_type)
-      raise ArgumentError, "#{response_type} is not a valid response type"
-    end
-
+  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, response_type)
+    url = url_for_call(api_function)
     result = get_http_result(url, args, use_http_get)
-    parse_result(result, api_function, response_type)
+    parse_result(result, api_function)
   end
 
   def call_natural_language(api_function, args={
                             :cloud => nil,
                             :text => nil,
                             :url => nil,
-                            }, response_type='json')
-    call_api(api_function, args, response_type)
+                            })
+    call_api(api_function, args)
   end
 
 end

File ruby/tc_repustate.rb

     return Repustate.new(KEY)
   end
 
-  def check_status(result, response_type)
-    if response_type == 'json'
-      status = result['status']
-    else
-      status = result.root.elements['status'].text
-    end
-    assert_equal('OK', status, result)
-  end
-
-  def check_call(func_name, options={}, response_type='json', check_status=true)
-    args = [func_name, options]
-    if func_name != 'powerpoint'
-      args.push(response_type)
-    end
-    result = get_accessor().send(*args)
+  def check_call(func_name, options={}, check_status=true)
+    result = get_accessor().send(func_name, options)
     assert_not_nil(result, 'Invalid result')
     if check_status
-      check_status(result, response_type)
+      status = result['status']
+      assert_equal('OK', status, result)
     end
   end
 
     check_call('sentiment', {:url => URL})
   end
 
-  def test_xml_response_type
-    check_call('sentiment', {:text => TEXT}, 'xml')
-  end
-
   def test_bulk_sentiment
     check_call('bulk_sentiment', {:items => [URL]})
   end
       :images => ['repustate.gif'],
       :titles => ['A test title'],
     }
-    check_call('powerpoint', options, 'json', false)
+    check_call('powerpoint', options, false)
   end
 
 end