Sha256: 713889219a902fa768a76a720936b6d77c6fe3351b6a88ef32ab4046b40736f5

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

require "zemanta/version"
require "httparty"

class Zemanta
	include HTTParty
	base_uri 'http://api.zemanta.com'

	# api_key for Zemanta
	@@api_key=nil
	def self.api_key=(key); @@api_key=key end
	def self.api_key; @@api_key end
	def api_key; Zemanta.api_key end

	# Initialize the client
	def initialize(api_key=nil)
		@@api_key = api_key unless api_key.nil?
		@@api_key = ENV["ZEMANTA_KEY"] if @@api_key.nil? and not(ENV["ZEMANTA_KEY"].nil?)
	end

	# Provide contextual metadata about text
	# See: http://developer.zemanta.com/docs/suggest/
	def suggest text, options={}
		get("suggest",options.merge!({
			text: text
		}))
	end

	# Suggest method allows developers to query Zemanta
	# for contextual metadata on a given text.
	# In comparison to zemanta.suggest, this method returns
	# only links to semantical entities.
	def suggest_markup text, options={}
		get("suggest_markup",options.merge!({
			text: text
		}))
	end

	# Get preferences of specific user. 
	def preferences options={}
		get("preferences",options)
	end

	# Get REST service with JSON response
	def get(method,options)
		options.merge!({
			method: "zemanta.#{method}",
			api_key: @@api_key,
			format: "json"})

		handle_response(
			self.class.get('/services/rest/0.0/',query:options),
		options)
	end

	# Handle response from service
	def handle_response response, options={}
		unless response['status'] =~ /ok/
			msg = response.parsed_response.match("<h1>(.+?)</h1>")[1]
			msg = "Error." if msg.nil?
			throw "Zemanta: %s" % msg
		end

		response
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zemanta-0.0.1 lib/zemanta.rb