Skip to content

Commit a9af407

Browse files
committed
Extract request login into another method
1 parent 941c1ed commit a9af407

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

lib/active_model/typograf.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,4 @@ def typograf(*columns)
5858
end
5959
end
6060

61-
if defined?(ActiveRecord)
62-
ActiveRecord::Base.extend(ActiveModel::Typograf)
63-
end
61+
ActiveRecord::Base.extend(ActiveModel::Typograf) if defined?(ActiveRecord)

lib/als_typograf.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ def self.debug?
8989

9090
def self.method_missing(method_name, *args)
9191
case method_name.to_s
92-
when /^(#{VALID_OPTIONS})=$/
93-
self[Regexp.last_match[1].to_sym] = args.first
94-
when /^(#{VALID_OPTIONS})$/
95-
self[method_name.to_sym]
96-
else
97-
super
92+
when /^(#{VALID_OPTIONS})=$/
93+
self[Regexp.last_match[1].to_sym] = args.first
94+
when /^(#{VALID_OPTIONS})$/
95+
self[method_name.to_sym]
96+
else
97+
super
9898
end
9999
end
100100

lib/als_typograf/request.rb

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ module Request
1212
# Process text with remote web-service
1313
# @param [String] text text to process
1414
# @param [Hash] options options for web-service
15-
15+
# @return [String]
1616
def self.process_text(text, options = {})
1717
text = text.encode(options[:encoding])
1818

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
2220
<?xml version="1.0" encoding="#{options[:encoding]}" ?>
2321
<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/">
2422
<soap:Body>
@@ -33,18 +31,21 @@ def self.process_text(text, options = {})
3331
</soap:Envelope>
3432
END_SOAP
3533

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(/&gt;/, '>').gsub(/&lt;/, '<').gsub(/&amp;/, '&').gsub(/(\t|\n)$/, '')
42-
end
43-
44-
text.encode(options[:encoding])
34+
make_request(body, options) || text
4535
rescue StandardError => e
4636
AlsTypograf.log_exception(e)
4737
text
4838
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(/&gt;/, '>').gsub(/&lt;/, '<').gsub(/&amp;/, '&').gsub(/(\t|\n)$/, '') if RESULT_REGEXP =~ result
48+
result.encode(options[:encoding])
49+
end
4950
end
5051
end

0 commit comments

Comments
 (0)