Class: ChupaText::Decomposers::HTTPServer

Inherits:
ChupaText::Decomposer show all
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

Methods inherited from ChupaText::Decomposer

registry

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(options)
  super
  @url = @options[:url]
  @url = URI(@url) if @url
end

Class Method Details

.default_urlObject


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
      message = "#{log_tag}[connection] "
      message << "Failed to process data in server: "
      message << "#{url}: "
      message << "#{error.class}: #{error.message}\n"
      message << error.backtrace.join("\n")
      message
    end
  rescue Net::ReadTimeout => error
    error do
      message = "#{log_tag}[timeout] "
      message << "Failed to process data in server: "
      message << "#{url}: "
      message << "#{error.class}: #{error.message}\n"
      message << error.backtrace.join("\n")
      message
    end
  end
end

#target?(data) ⇒ Boolean

Returns:

  • (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