Sha256: 0a0b8d449fc9b740d9f7e9f3cd5d1a519ba811b288632c678d2c90c78d7dfbaf

Contents?: true

Size: 955 Bytes

Versions: 1

Compression:

Stored size: 955 Bytes

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
term_extraction-0.1.7 lib/term_extraction/yahoo.rb