Class: DoverToCalais::Dover
- Inherits:
-
Object
- Object
- DoverToCalais::Dover
- Defined in:
- lib/dover_to_calais.rb
Overview
This class is responsible for parsing, reading and sending to OpenCalais, text from a data source. The data source is passed to the class constructor and can be pretty much any form of document or URL. The class allows the user to specify one or more callbacks, to be called when the data source has been processed by OpenCalais (#to_calais).
Constant Summary collapse
- CALAIS_SERVICE =
'https://api.opencalais.com/tag/rs/enrich'
Instance Attribute Summary collapse
-
#data_src ⇒ String
readonly
The data source to be processed, either a file path or a URL.
-
#error ⇒ String?
readonly
Any error that occurred during data-source processing, nil if none occurred.
Instance Method Summary collapse
-
#initialize(data_src) ⇒ Dover
constructor
creates a new Dover object, passing the name of the data source to be processed.
-
#to_calais(&block) ⇒ Object
Defines the user callbacks.
Constructor Details
#initialize(data_src) ⇒ Dover
creates a new Dover object, passing the name of the data source to be processed
172 173 174 175 176 |
# File 'lib/dover_to_calais.rb', line 172 def initialize(data_src) @data_src = data_src @callbacks = [] analyse_this end |
Instance Attribute Details
#data_src ⇒ String (readonly)
Returns the data source to be processed, either a file path or a URL.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/dover_to_calais.rb', line 163 class Dover CALAIS_SERVICE = 'https://api.opencalais.com/tag/rs/enrich' attr_reader :data_src, :error # creates a new Dover object, passing the name of the data source to be processed # # @param data_src [String] the name of the data source to be processed def initialize(data_src) @data_src = data_src @callbacks = [] analyse_this end # uses the {https://github.com/Erol/yomu yomu} gem to extract text from a number of document formats and URLs. # If an exception occurs, it is written to the {@error} instance variable # # @param [String] src the name of the data source (file-path or URI) # @return [String, nil] the extracted text, or nil if an exception occurred. def get_src_data(src) begin yomu = Yomu.new src rescue Exception=>e @error = "ERR: #{e}" else yomu.text end end # Defines the user callbacks. If the data source is successfully read, then this method will store a # user-defined block which will be called on completion of the OpenCalais HTTP request. If the data source # cannot be read -for whatever reason- then the block will immediately be called, passing the parameter that # caused the read failure. # # @param block a user-defined block # @return N/A def to_calais(&block) #fred rules ok if @document @callbacks << block else result = ResponseData.new nil, @error block.call(result) end end #method # def analyse_this @document = get_src_data(@data_src) begin if @document response = nil = {:inactivity_timeout => 0} if DoverToCalais::PROXY && DoverToCalais::PROXY.class.eql?('Hash') && DoverToCalais::PROXY.keys[0].eql?(:proxy) = .merge(DoverToCalais::PROXY) end = { :body => @document.to_s, :head => { 'x-calais-licenseID' => DoverToCalais::API_KEY, :content_type => 'TEXT/RAW', :enableMetadataType => 'GenericRelations,SocialTags', :outputFormat => 'Text/Simple'} } http = EventMachine::HttpRequest.new(CALAIS_SERVICE, ).post http.callback do http.response.match(/<OpenCalaisSimple>/) do |m| response = Nokogiri::XML('<OpenCalaisSimple>' + m.post_match) do |config| #strict xml parsing, disallow network connections config.strict.nonet end #block end #block result = response ? ResponseData.new(response, nil) : ResponseData.new(nil,'ERR: unable to find <OpenCalaisSimple> tag in response data') @callbacks.each { |c| c.call(result) } end #callback http.errback do result = ResponseData.new nil, "#{http.error}" @callbacks.each { |c| c.call(result) } end #errback end #if rescue Exception=>e puts "ERR: #{e}" exit 0 end end #method public :to_calais private :get_src_data, :analyse_this end |
#error ⇒ String? (readonly)
Returns any error that occurred during data-source processing, nil if none occurred.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/dover_to_calais.rb', line 163 class Dover CALAIS_SERVICE = 'https://api.opencalais.com/tag/rs/enrich' attr_reader :data_src, :error # creates a new Dover object, passing the name of the data source to be processed # # @param data_src [String] the name of the data source to be processed def initialize(data_src) @data_src = data_src @callbacks = [] analyse_this end # uses the {https://github.com/Erol/yomu yomu} gem to extract text from a number of document formats and URLs. # If an exception occurs, it is written to the {@error} instance variable # # @param [String] src the name of the data source (file-path or URI) # @return [String, nil] the extracted text, or nil if an exception occurred. def get_src_data(src) begin yomu = Yomu.new src rescue Exception=>e @error = "ERR: #{e}" else yomu.text end end # Defines the user callbacks. If the data source is successfully read, then this method will store a # user-defined block which will be called on completion of the OpenCalais HTTP request. If the data source # cannot be read -for whatever reason- then the block will immediately be called, passing the parameter that # caused the read failure. # # @param block a user-defined block # @return N/A def to_calais(&block) #fred rules ok if @document @callbacks << block else result = ResponseData.new nil, @error block.call(result) end end #method # def analyse_this @document = get_src_data(@data_src) begin if @document response = nil = {:inactivity_timeout => 0} if DoverToCalais::PROXY && DoverToCalais::PROXY.class.eql?('Hash') && DoverToCalais::PROXY.keys[0].eql?(:proxy) = .merge(DoverToCalais::PROXY) end = { :body => @document.to_s, :head => { 'x-calais-licenseID' => DoverToCalais::API_KEY, :content_type => 'TEXT/RAW', :enableMetadataType => 'GenericRelations,SocialTags', :outputFormat => 'Text/Simple'} } http = EventMachine::HttpRequest.new(CALAIS_SERVICE, ).post http.callback do http.response.match(/<OpenCalaisSimple>/) do |m| response = Nokogiri::XML('<OpenCalaisSimple>' + m.post_match) do |config| #strict xml parsing, disallow network connections config.strict.nonet end #block end #block result = response ? ResponseData.new(response, nil) : ResponseData.new(nil,'ERR: unable to find <OpenCalaisSimple> tag in response data') @callbacks.each { |c| c.call(result) } end #callback http.errback do result = ResponseData.new nil, "#{http.error}" @callbacks.each { |c| c.call(result) } end #errback end #if rescue Exception=>e puts "ERR: #{e}" exit 0 end end #method public :to_calais private :get_src_data, :analyse_this end |
Instance Method Details
#to_calais(&block) ⇒ Object
Defines the user callbacks. If the data source is successfully read, then this method will store a user-defined block which will be called on completion of the OpenCalais HTTP request. If the data source cannot be read -for whatever reason- then the block will immediately be called, passing the parameter that caused the read failure.
203 204 205 206 207 208 209 210 211 212 |
# File 'lib/dover_to_calais.rb', line 203 def to_calais(&block) #fred rules ok if @document @callbacks << block else result = ResponseData.new nil, @error block.call(result) end end |