Sha256: b62ac6fdd2404ad3f728de938d698a5309ac8c4c3a098a10ed6a82ec17d11018

Contents?: true

Size: 962 Bytes

Versions: 2

Compression:

Stored size: 962 Bytes

Contents

require 'crack'
require 'net/http'
require 'addressable/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
      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

2 entries across 2 versions & 1 rubygems

Version Path
term_extraction-0.1.6 lib/term_extraction/zemanta.rb
term_extraction-0.1.5 lib/term_extraction/zemanta.rb