# -*- encoding: utf-8 -*- require 'net/http' module AlsTypograf # The request class module Request SERVICE_URL = URI.parse('http://typograf.artlebedev.ru/webservices/typograf.asmx') RESULT_REGEXP = /\s*((.|\n)*?)\s*<\/ProcessTextResult>/ # 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 = {}) text = text.encode(options[:encoding]) #noinspection RubyStringKeysInHashInspection request = Net::HTTP::Post.new(SERVICE_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(SERVICE_URL.host, SERVICE_URL.port).start do |http| http.request(request) end response.body.force_encoding(options[:encoding]) if response.body.respond_to?(:force_encoding) text = case response when Net::HTTPSuccess if RESULT_REGEXP =~ response.body $1.gsub(/>/, '>').gsub(/</, '<').gsub(/&/, '&').gsub(/(\t|\n)$/, '') else text end else text end text.encode(options[:encoding]) rescue StandardError => e AlsTypograf.log_exception(e) text end end end