Module: AlsTypograf::Request
- Defined in:
- lib/als_typograf/request.rb
Overview
The request class
Constant Summary collapse
- SERVICE_URL =
URI.parse('http://typograf.artlebedev.ru/webservices/typograf.asmx')
- RESULT_REGEXP =
/<ProcessTextResult>\s*((.|\n)*?)\s*<\/ProcessTextResult>/
Class Method Summary collapse
-
.process_text(text, options = {}) ⇒ Object
Process text with remote web-service.
Class Method Details
.process_text(text, options = {}) ⇒ Object
Process text with remote web-service
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/als_typograf/request.rb', line 13 def self.process_text(text, = {}) text = text.encode([: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 <?xml version="1.0" encoding="#{[:encoding]}" ?> <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/"> <soap:Body> <ProcessText xmlns="http://typograf.artlebedev.ru/webservices/"> <text>#{text.gsub(/&/, '&').gsub(/</, '<').gsub(/>/, '>')}</text> <entityType>#{[:entity_type]}</entityType> <useBr>#{[:use_br]}</useBr> <useP>#{[:use_p]}</useP> <maxNobr>#{[:max_nobr]}</maxNobr> </ProcessText> </soap:Body> </soap:Envelope> END_SOAP response = Net::HTTP.new(SERVICE_URL.host, SERVICE_URL.port).start do |http| http.request(request) end response.body.force_encoding([: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([:encoding]) rescue StandardError => e AlsTypograf.log_exception(e) text end |