[Groonga-commit] ranguba/chupa-text at c90cc43 [master] http-server: add support for changing default URL at run-time

Back to archive index
Kouhei Sutou null+****@clear*****
Mon May 20 11:32:11 JST 2019


Kouhei Sutou	2019-05-20 11:32:11 +0900 (Mon, 20 May 2019)

  Revision: c90cc4310a8956e7ed3ee1c10c25d910f2db5559
  https://github.com/ranguba/chupa-text/commit/c90cc4310a8956e7ed3ee1c10c25d910f2db5559

  Message:
    http-server: add support for changing default URL at run-time

  Modified files:
    lib/chupa-text/decomposers/http-server.rb

  Modified: lib/chupa-text/decomposers/http-server.rb (+17 -13)
===================================================================
--- lib/chupa-text/decomposers/http-server.rb    2019-03-29 19:30:45 +0900 (be3fb2a)
+++ lib/chupa-text/decomposers/http-server.rb    2019-05-20 11:32:11 +0900 (da829f6)
@@ -38,16 +38,13 @@ module ChupaText
 
       def initialize(options)
         super
-        @url = @options[:url] ||
-               self.class.default_url ||
-               ENV["CHUPA_TEXT_HTTP_SERVER_URL"]
+        @url = @options[:url]
         @url = URI(@url) if @url
       end
 
       def target?(data)
-        return false unless @url
         return false if data.text_plain?
-        true
+        @url or default_url
       end
 
       def target_score(data)
@@ -59,8 +56,9 @@ module ChupaText
       end
 
       def decompose(data, &block)
-        http = Net::HTTP.new(@url.host, @url.port)
-        http.use_ssl = true if****@url*****_a?(URI::HTTPS)
+        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)
           http.open_timeout = data.timeout * 1.5
           http.read_timeout = data.timeout * 1.5
@@ -70,13 +68,13 @@ module ChupaText
         end
         begin
           http.start do
-            process_request(http, data, &block)
+            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 << "#{url}: "
             message << "#{error.class}: #{error.message}\n"
             message << error.backtrace.join("\n")
             message
@@ -85,7 +83,7 @@ module ChupaText
           error do
             message = "#{log_tag}[timeout] "
             message << "Failed to process data in server: "
-            message << "#{@url}: "
+            message << "#{url}: "
             message << "#{error.class}: #{error.message}\n"
             message << error.backtrace.join("\n")
             message
@@ -94,8 +92,14 @@ module ChupaText
       end
 
       private
-      def process_request(http, data)
-        request = Net::HTTP::Post.new(@url)
+      def default_url
+        url = self.class.default_url || ENV["CHUPA_TEXT_HTTP_SERVER_URL"]
+        return nil if url.nil?
+        URI(url)
+      end
+
+      def process_request(url, http, data)
+        request = Net::HTTP::Post.new(url)
         request["transfer-encoding"] = "chunked"
         data.open do |input|
           request.set_form(build_parameters(data, input),
@@ -115,7 +119,7 @@ module ChupaText
           else
             error do
               message = "#{log_tag} Failed to process data in server: "
-              message << "#{@url}: "
+              message << "#{url}: "
               message << "#{response.code}: #{response.message.strip}\n"
               case response.content_type
               when "application/json"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20190520/cf0688b1/attachment-0001.html>


More information about the Groonga-commit mailing list
Back to archive index