require 'net/http' module AlsTypograf # The request class module Request @@url = URI.parse('http://typograf.artlebedev.ru/webservices/typograf.asmx') @@result_regexp = /\s*((.|\n)*?)\s*<\/ProcessTextResult>/m # Process text with remote web-service # @param [String] text text to process # @param [Hash] options options for web-service def self.process_text(text, options = {}) request = Net::HTTP::Post.new(@@url.path, { 'Content-Type' => 'text/xml', 'SOAPAction' => '"http://typograf.artlebedev.ru/webservices/ProcessText"' }) request.body = <<-END_SOAP #{text.gsub(/&/, '&').gsub(//, '>')} #{options[:entity_type]} #{options[:use_br]} #{options[:use_p]} #{options[:max_nobr]} END_SOAP response = Net::HTTP.new(@@url.host, @@url.port).start do |http| http.request(request) end case response when Net::HTTPSuccess result = if @@result_regexp =~ response.body $1.gsub(/>/, '>').gsub(/</, '<').gsub(/&/, '&').gsub(/(\t|\n)$/, '') else text end else text end rescue ::Exception => exception text end end end