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
-
#analyse_this ⇒ Object
(also: #analyze_this)
Gets the source text parsed.
-
#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
173 174 175 176 |
# File 'lib/dover_to_calais.rb', line 173 def initialize(data_src) @data_src = data_src @callbacks = [] end |
Instance Attribute Details
#data_src ⇒ String (readonly)
Returns the data source to be processed, either a file path or a URL.
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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/dover_to_calais.rb', line 164 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 = [] 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 !@error @callbacks << block else result = ResponseData.new nil, @error block.call(result) end end #method # Gets the source text parsed. If the parsing is successful, the data source is POSTed to OpenCalais # via an EventMachine request and a callback is set to manage the OpenCalais response. # All Dover object callbacks are then called with the request result yielded to them. # # @param N/A # @return a {Class ResponseData} object def analyse_this @document = get_src_data(@data_src) begin if @document[0..2].eql?('ERR') raise 'Invalid data source' else 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 if http.response_header.status == 200 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: cannot find <OpenCalaisSimple> tag in response data - source invalid?') else #non-200 response header result = ResponseData.new nil, "ERR: OpenCalais service responded with #{http.response_header.status} - response body: '#{http.response}'" end @callbacks.each { |c| c.call(result) } end #callback http.errback do result = ResponseData.new nil, "ERR: #{http.error}" @callbacks.each { |c| c.call(result) } end #errback end #if rescue Exception=>e #result = ResponseData.new nil, "ERR: #{e}" #@callbacks.each { |c| c.call(result) } @error = "ERR: #{e}" end end #method alias_method :analyze_this, :analyse_this public :to_calais, :analyse_this private :get_src_data end |
#error ⇒ String? (readonly)
Returns any error that occurred during data-source processing, nil if none occurred.
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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/dover_to_calais.rb', line 164 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 = [] 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 !@error @callbacks << block else result = ResponseData.new nil, @error block.call(result) end end #method # Gets the source text parsed. If the parsing is successful, the data source is POSTed to OpenCalais # via an EventMachine request and a callback is set to manage the OpenCalais response. # All Dover object callbacks are then called with the request result yielded to them. # # @param N/A # @return a {Class ResponseData} object def analyse_this @document = get_src_data(@data_src) begin if @document[0..2].eql?('ERR') raise 'Invalid data source' else 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 if http.response_header.status == 200 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: cannot find <OpenCalaisSimple> tag in response data - source invalid?') else #non-200 response header result = ResponseData.new nil, "ERR: OpenCalais service responded with #{http.response_header.status} - response body: '#{http.response}'" end @callbacks.each { |c| c.call(result) } end #callback http.errback do result = ResponseData.new nil, "ERR: #{http.error}" @callbacks.each { |c| c.call(result) } end #errback end #if rescue Exception=>e #result = ResponseData.new nil, "ERR: #{e}" #@callbacks.each { |c| c.call(result) } @error = "ERR: #{e}" end end #method alias_method :analyze_this, :analyse_this public :to_calais, :analyse_this private :get_src_data end |
Instance Method Details
#analyse_this ⇒ Object Also known as: analyze_this
Gets the source text parsed. If the parsing is successful, the data source is POSTed to OpenCalais via an EventMachine request and a callback is set to manage the OpenCalais response. All Dover object callbacks are then called with the request result yielded to them.
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 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/dover_to_calais.rb', line 220 def analyse_this @document = get_src_data(@data_src) begin if @document[0..2].eql?('ERR') raise 'Invalid data source' else 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 if http.response_header.status == 200 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: cannot find <OpenCalaisSimple> tag in response data - source invalid?') else #non-200 response header result = ResponseData.new nil, "ERR: OpenCalais service responded with #{http.response_header.status} - response body: '#{http.response}'" end @callbacks.each { |c| c.call(result) } end #callback http.errback do result = ResponseData.new nil, "ERR: #{http.error}" @callbacks.each { |c| c.call(result) } end #errback end #if rescue Exception=>e #result = ResponseData.new nil, "ERR: #{e}" #@callbacks.each { |c| c.call(result) } @error = "ERR: #{e}" end end |
#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 !@error @callbacks << block else result = ResponseData.new nil, @error block.call(result) end end |