Class: ChupaText::Decomposers::HTTPServer
- Inherits:
-
ChupaText::Decomposer
- Object
- ChupaText::Decomposer
- ChupaText::Decomposers::HTTPServer
- Includes:
- Loggable
- Defined in:
- lib/chupa-text/decomposers/http-server.rb
Constant Summary collapse
- @@default_url =
nil
Class Method Summary collapse
Instance Method Summary collapse
- #decompose(data, &block) ⇒ Object
-
#initialize(options) ⇒ HTTPServer
constructor
A new instance of HTTPServer.
- #target?(data) ⇒ Boolean
- #target_score(data) ⇒ Object
Methods inherited from ChupaText::Decomposer
Constructor Details
#initialize(options) ⇒ HTTPServer
Returns a new instance of HTTPServer.
39 40 41 42 43 |
# File 'lib/chupa-text/decomposers/http-server.rb', line 39 def initialize() super @url = @options[:url] @url = URI(@url) if @url end |
Class Method Details
.default_url ⇒ Object
30 31 32 |
# File 'lib/chupa-text/decomposers/http-server.rb', line 30 def default_url @@default_url end |
.default_url=(url) ⇒ Object
34 35 36 |
# File 'lib/chupa-text/decomposers/http-server.rb', line 34 def default_url=(url) @@default_url = url end |
Instance Method Details
#decompose(data, &block) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/chupa-text/decomposers/http-server.rb', line 58 def decompose(data, &block) url = @url || default_url http = Net::HTTP.new(url.host, url.port) http.use_ssl = true if url.is_a?(URI::HTTPS) if data.timeout.is_a?(Numeric) timeout = data.timeout * 0.9 http.open_timeout = timeout http.read_timeout = timeout http.write_timeout = timeout if http.respond_to?(:write_timeout=) http.continue_timeout = timeout end begin http.start do process_request(url, http, data, &block) end rescue SystemCallError => error error do = "#{log_tag}[connection] " << "Failed to process data in server: " << "#{url}: " << "#{error.class}: #{error.}\n" << error.backtrace.join("\n") end rescue Net::ReadTimeout => error error do = "#{log_tag}[timeout] " << "Failed to process data in server: " << "#{url}: " << "#{error.class}: #{error.}\n" << error.backtrace.join("\n") end end end |
#target?(data) ⇒ Boolean
45 46 47 48 |
# File 'lib/chupa-text/decomposers/http-server.rb', line 45 def target?(data) return false if data.text_plain? @url or default_url end |
#target_score(data) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/chupa-text/decomposers/http-server.rb', line 50 def target_score(data) if target?(data) -100 else nil end end |