@@ -12,13 +12,11 @@ module Request
12
12
# Process text with remote web-service
13
13
# @param [String] text text to process
14
14
# @param [Hash] options options for web-service
15
-
15
+ # @return [String]
16
16
def self . process_text ( text , options = { } )
17
17
text = text . encode ( options [ :encoding ] )
18
18
19
- request = Net ::HTTP ::Post . new ( SERVICE_URL . path , 'Content-Type' => 'text/xml' , 'SOAPAction' => SOAP_ACTION )
20
-
21
- request . body = <<-END_SOAP
19
+ body = <<-END_SOAP
22
20
<?xml version="1.0" encoding="#{ options [ :encoding ] } " ?>
23
21
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
24
22
<soap:Body>
@@ -33,18 +31,21 @@ def self.process_text(text, options = {})
33
31
</soap:Envelope>
34
32
END_SOAP
35
33
36
- response = Net ::HTTP . new ( SERVICE_URL . host , SERVICE_URL . port ) . start { |http | http . request ( request ) }
37
-
38
- response . body . force_encoding ( options [ :encoding ] ) if response . body . respond_to? ( :force_encoding )
39
-
40
- if response . is_a? ( Net ::HTTPSuccess ) && RESULT_REGEXP =~ response . body
41
- text = Regexp . last_match [ 1 ] . gsub ( />/ , '>' ) . gsub ( /</ , '<' ) . gsub ( /&/ , '&' ) . gsub ( /(\t |\n )$/ , '' )
42
- end
43
-
44
- text . encode ( options [ :encoding ] )
34
+ make_request ( body , options ) || text
45
35
rescue StandardError => e
46
36
AlsTypograf . log_exception ( e )
47
37
text
48
38
end
39
+
40
+ def self . make_request ( text , options )
41
+ request = Net ::HTTP ::Post . new ( SERVICE_URL . path , 'Content-Type' => 'text/xml' , 'SOAPAction' => SOAP_ACTION )
42
+ request . body = text
43
+ response = Net ::HTTP . new ( SERVICE_URL . host , SERVICE_URL . port ) . start { |http | http . request ( request ) }
44
+ return nil unless response . is_a? ( Net ::HTTPSuccess )
45
+ result = response . body
46
+ result . force_encoding ( options [ :encoding ] ) if result . respond_to? ( :force_encoding )
47
+ result = Regexp . last_match [ 1 ] . gsub ( />/ , '>' ) . gsub ( /</ , '<' ) . gsub ( /&/ , '&' ) . gsub ( /(\t |\n )$/ , '' ) if RESULT_REGEXP =~ result
48
+ result . encode ( options [ :encoding ] )
49
+ end
49
50
end
50
51
end
0 commit comments