From 1dc1c9f9398478fa54be972e6fab229641a27734 Mon Sep 17 00:00:00 2001 From: Abhay Kumar Date: Mon, 28 Jan 2008 21:31:58 -0800 Subject: [PATCH] made access to the library a bit better --- README | 3 ++- lib/calais.rb | 25 +++++++++++++++++-------- spec/calais_spec.rb | 4 ++-- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/README b/README index 6b89bd5..cef96ec 100644 --- a/README +++ b/README @@ -14,7 +14,8 @@ A Ruby interface to the Open Calais API (http://opencalais.com) == SYNOPSIS: -This is a very basic wrapper to the Open Calais API. It uses the POST endpoint and currently supports the Enlighten action. +This is a very basic wrapper to the Open Calais API. It uses the POST endpoint and currently supports the Enlighten action. Here's a simple call: + Calais.enlighten(:content => "The government of the United Kingdom has given corporations like fast food chain McDonald's the right to award high school qualifications to employees who complete a company training program.", :content_type => :text) == REQUIREMENTS: diff --git a/lib/calais.rb b/lib/calais.rb index 1348211..b9da544 100644 --- a/lib/calais.rb +++ b/lib/calais.rb @@ -8,18 +8,26 @@ $KCODE = "UTF8" class Calais POST_URL = "http://api.opencalais.com" - AVAILABLE_OUTPUT_FORMATS = ["XML/RDF"] - DEFAULT_OUTPUT_FORMAT = "XML/RDF" + AVAILABLE_OUTPUT_FORMATS = { + :rdf => "XML/RDF" + } + DEFAULT_OUTPUT_FORMAT = :rdf - AVAILABLE_CONTENT_TYPES = ["TEXT/XML", "TEXT/TXT", "TEXT/HTML"] - DEFAULT_CONTENT_TYPE = "TEXT/TXT" + AVAILABLE_CONTENT_TYPES = { + :xml => "TEXT/XML", + :html => "TEXT/HTML", + :text => "TEXT/TXT" + } + DEFAULT_CONTENT_TYPE = :xml DEFAULT_SUBMITTER = "calais.rb" - AVAILABLE_METHODS = {"enlighten" => "/enlighten/calais.asmx/Enlighten"} + AVAILABLE_METHODS = { + :enlighten => "/enlighten/calais.asmx/Enlighten" + } class << self - def enlighten(*args, &block) Calais.new(*args, &block).call('enlighten') end + def enlighten(*args, &block) Calais.new(*args, &block).call(:enlighten) end end attr_accessor :license_id @@ -35,6 +43,7 @@ class Calais end def call(method) + method = method.intern unless method.is_a?(Symbol) raise ArgumentError.new("Unknown method: #{method}") unless AVAILABLE_METHODS.keys.include? method post_args = { @@ -60,8 +69,8 @@ class Calais private def params_xml - content_type = @content_type && AVAILABLE_CONTENT_TYPES.include?(@content_type) ? @content_type : DEFAULT_CONTENT_TYPE - output_format = @output_format && AVAILABLE_OUTPUT_FORMATS.include?(@output_format) ? @output_format : DEFAULT_OUTPUT_FORMAT + content_type = @content_type && AVAILABLE_CONTENT_TYPES.keys.include?(@content_type) ? AVAILABLE_CONTENT_TYPES[@content_type] : AVAILABLE_CONTENT_TYPES[DEFAULT_CONTENT_TYPE] + output_format = @output_format && AVAILABLE_OUTPUT_FORMATS.keys.include?(@output_format) ? AVAILABLE_OUTPUT_FORMATS[@output_format] : AVAILABLE_OUTPUT_FORMATS[DEFAULT_OUTPUT_FORMAT] allow_distribution = @allow_distribution ? "true" : "false" allow_search = @allow_search ? "true" : "false" submitter = @submitter || DEFAULT_SUBMITTER diff --git a/spec/calais_spec.rb b/spec/calais_spec.rb index 1c9e34c..9077f1a 100644 --- a/spec/calais_spec.rb +++ b/spec/calais_spec.rb @@ -36,7 +36,7 @@ end describe Calais, ".enlighten" do before(:all) do - @marked = Calais.enlighten(:content => SAMPLE_DOCUMENT, :content_type => "TEXT/XML") + @marked = Calais.enlighten(:content => SAMPLE_DOCUMENT, :content_type => :xml) end it "returns a string" do @@ -61,7 +61,7 @@ end describe Calais, ".params_xml" do it "returns an xml encoded string" do - client = Calais.new(:content => SAMPLE_DOCUMENT, :content_type => "TEXT/XML") + client = Calais.new(:content => SAMPLE_DOCUMENT, :content_type => :xml) client.send("params_xml").should_not be_nil client.send("params_xml").should == %[] end -- 2.11.4.GIT