Sha256: d235ecbe86561fef66ebf49e6f63ca00628c2ac332f5993e0f185634c7660674

Contents?: true

Size: 981 Bytes

Versions: 3

Compression:

Stored size: 981 Bytes

Contents

require 'net/http'
require 'nokogiri'
require 'addressable/uri'

class TermExtraction
  class Zemanta < TermExtraction
    def terms
      terms = []
      data  = Nokogiri::XML.parse(remote_xml)

      data.search('//name').each do |n|
        terms << n.text
      end

      terms
    end

    def uri
      Addressable::URI.parse(gateway)
    end

    def post_params
      {
        'method'        =>'zemanta.suggest',
        'api_key'       => @api_key,
        'return_images' => 0,
        'text'          => @context,
        'format'        => 'xml'
      }
    end

    class << self
      def canonical_name
        'zemanta'
      end
    end

    private
      def gateway
        'http://api.zemanta.com/services/rest/0.0/'
      end

      def remote_xml
        begin
          Net::HTTP.post_form(uri, post_params).body
        rescue => e
          $stderr.puts "Couldn't fetch from API: #{e.message}" if $VERBOSE
          nil
        end
      end
  end
end

Version data entries

3 entries across 3 versions & 3 rubygems

Version Path
alexrabarts-term_extraction-0.1.4 lib/term_extraction/zemanta.rb
term-extraction-0.1.4 lib/term_extraction/zemanta.rb
term_extraction-0.1.4 lib/term_extraction/zemanta.rb