3 require 'rexml/document'
9 POST_URL = "http://api.opencalais.com"
11 AVAILABLE_OUTPUT_FORMATS = {
14 DEFAULT_OUTPUT_FORMAT = :rdf
16 AVAILABLE_CONTENT_TYPES = {
21 DEFAULT_CONTENT_TYPE = :xml
23 DEFAULT_SUBMITTER = "calais.rb"
26 :enlighten => "/enlighten/calais.asmx/Enlighten"
30 def enlighten(*args, &block) Calais.new(*args, &block).call(:enlighten) end
33 attr_accessor :license_id
34 attr_accessor :content
35 attr_accessor :content_type, :output_format
36 attr_accessor :allow_distribution, :allow_search, :submitter, :external_id
37 attr_accessor :external_metadata
39 def initialize(options={}, &block)
40 @license_id = YAML.load(File.read(File.join(File.dirname(__FILE__), '..', 'conf', 'calais.yml')))['key']
41 options.each {|k,v| send("#{k}=", v)}
42 yield(self) if block_given?
46 method = method.intern unless method.is_a?(Symbol)
47 raise ArgumentError.new("Unknown method: #{method}") unless AVAILABLE_METHODS.keys.include? method
50 "licenseID" => @license_id,
51 "content" => @content,
52 "paramsXML" => params_xml
55 url = URI.parse(POST_URL + AVAILABLE_METHODS[method])
57 resp, data = Net::HTTP.post_form(url, post_args)
61 doc = REXML::Document.new(data)
64 raise ServiceError.new("OpenCalais Service Error (#{resp})")
68 class ServiceError < Exception; end
72 content_type = @content_type && AVAILABLE_CONTENT_TYPES.keys.include?(@content_type) ? AVAILABLE_CONTENT_TYPES[@content_type] : AVAILABLE_CONTENT_TYPES[DEFAULT_CONTENT_TYPE]
73 output_format = @output_format && AVAILABLE_OUTPUT_FORMATS.keys.include?(@output_format) ? AVAILABLE_OUTPUT_FORMATS[@output_format] : AVAILABLE_OUTPUT_FORMATS[DEFAULT_OUTPUT_FORMAT]
74 allow_distribution = @allow_distribution ? "true" : "false"
75 allow_search = @allow_search ? "true" : "false"
76 submitter = @submitter || DEFAULT_SUBMITTER
77 external_id = @external_id || Digest::SHA1.hexdigest(@content.inspect)
78 external_metadata = @external_metadata || ""
80 xml = %[<c:params xmlns:c="http://s.opencalais.com/1/pred/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">]
81 xml += %[<c:processingDirectives c:contentType="#{content_type}" c:outputFormat="#{output_format}"></c:processingDirectives>]
82 xml += %[<c:userDirectives c:allowDistribution="#{allow_distribution}" c:allowSearch="#{allow_search}" c:externalID="#{external_id}" c:submitter="#{submitter}"></c:userDirectives>]
83 xml += %[<c:externalMetadata>#{external_metadata}</c:externalMetadata>]