Class: Zemanta

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/zemanta.rb,
lib/zemanta/version.rb

Constant Summary collapse

VERSION =
"0.0.1"
@@api_key =

api_key for Zemanta

nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil) ⇒ Zemanta

Initialize the client



15
16
17
18
# File 'lib/zemanta.rb', line 15

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

Class Method Details

.api_keyObject



11
# File 'lib/zemanta.rb', line 11

def self.api_key; @@api_key end

.api_key=(key) ⇒ Object



10
# File 'lib/zemanta.rb', line 10

def self.api_key=(key); @@api_key=key end

Instance Method Details

#api_keyObject



12
# File 'lib/zemanta.rb', line 12

def api_key; Zemanta.api_key end

#get(method, options) ⇒ Object

Get REST service with JSON response



44
45
46
47
48
49
50
51
52
53
# File 'lib/zemanta.rb', line 44

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(response, options = {}) ⇒ Object

Handle response from service



56
57
58
59
60
61
62
63
64
# File 'lib/zemanta.rb', line 56

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

#preferences(options = {}) ⇒ Object

Get preferences of specific user.



39
40
41
# File 'lib/zemanta.rb', line 39

def preferences options={}
	get("preferences",options)
end

#suggest(text, options = {}) ⇒ Object

Provide contextual metadata about text See: developer.zemanta.com/docs/suggest/



22
23
24
25
26
# File 'lib/zemanta.rb', line 22

def suggest text, options={}
	get("suggest",options.merge!({
		text: text
	}))
end

#suggest_markup(text, options = {}) ⇒ Object

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.



32
33
34
35
36
# File 'lib/zemanta.rb', line 32

def suggest_markup text, options={}
	get("suggest_markup",options.merge!({
		text: text
	}))
end