made access to the library a bit better
[ruby-calais.git] / lib / calais.rb
blobb9da544df5c071fd485c97fdea01a92476bbec88
1 require 'digest/sha1'
2 require 'net/http'
3 require 'rexml/document'
4 require 'yaml'
6 $KCODE = "UTF8"
8 class Calais
9   POST_URL = "http://api.opencalais.com"
10   
11   AVAILABLE_OUTPUT_FORMATS = {
12     :rdf => "XML/RDF"
13   }
14   DEFAULT_OUTPUT_FORMAT = :rdf
15   
16   AVAILABLE_CONTENT_TYPES = {
17     :xml => "TEXT/XML",
18     :html => "TEXT/HTML",
19     :text => "TEXT/TXT"
20   }
21   DEFAULT_CONTENT_TYPE = :xml
22   
23   DEFAULT_SUBMITTER = "calais.rb"
24   
25   AVAILABLE_METHODS = {
26     :enlighten => "/enlighten/calais.asmx/Enlighten"
27   }
28   
29   class << self
30     def enlighten(*args, &block) Calais.new(*args, &block).call(:enlighten) end
31   end
32   
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
38   
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?
43   end
44   
45   def call(method)
46     method = method.intern unless method.is_a?(Symbol)
47     raise ArgumentError.new("Unknown method: #{method}") unless AVAILABLE_METHODS.keys.include? method
48     
49     post_args = {
50       "licenseID" => @license_id,
51       "content" => @content,
52       "paramsXML" => params_xml
53     }
54     
55     url = URI.parse(POST_URL + AVAILABLE_METHODS[method])
56     
57     resp, data = Net::HTTP.post_form(url, post_args)
58     
59     case resp
60     when Net::HTTPOK
61       doc = REXML::Document.new(data)
62       doc.root.text
63     else
64       raise ServiceError.new("OpenCalais Service Error (#{resp})")
65     end
66   end
67   
68   class ServiceError < Exception; end
69   
70   private
71     def params_xml
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 || ""
79       
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>]
84       xml += %[</c:params>]
85     end
86 end
88 class Calais
89   VERSION = '0.0.1'
90 end