Sha256: dc4b63d65e460dba3dce5806e60bfe74decb7fb148553d67df2f3abddb9e73d5

Contents?: true

Size: 937 Bytes

Versions: 1

Compression:

Stored size: 937 Bytes

Contents

require 'crack'
require 'net/http'
require 'uri'

class TermExtraction
  class Zemanta < TermExtraction
    def terms
      begin
        Crack::XML.parse(remote_xml)['rsp']['keywords']['keyword'].map { |h| h['name'] }
      rescue
        []
      end
    end

    def uri
      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

1 entries across 1 versions & 1 rubygems

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