require 'crack' require 'open-uri' require 'uri' class TermExtraction class Yahoo < TermExtraction def terms begin Crack::XML.parse(remote_xml)['ResultSet']['Result'] rescue [] end end def uri api_uri = URI.parse(gateway) api_uri.query = { 'appid' => @api_key, 'output' => 'xml', 'context' => @context }.map { |k,v| "#{URI.escape(k || '')}=#{URI.escape(v || '')}" }.join('&') api_uri end class << self def canonical_name 'yahoo' end end private def ns { 's' => 'urn:yahoo:cate' } end def gateway 'http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction' end def remote_xml begin open(uri).read rescue => e $stderr.puts "Couldn't fetch from API: #{e.message}" if $VERBOSE nil end end end end