Class: Sem4rSoap::HttpConnector::ConnectorNetHttp

Inherits:
Object
  • Object
show all
Includes:
SoapDumper
Defined in:
lib/sem4r_soap/http_connector.rb

Overview

use Net::HTTP standard library http client

Instance Method Summary collapse

Methods included from SoapDumper

#dump_soap_options, #dump_soap_request, #dump_soap_response

Constructor Details

#initialize(logger = nil) ⇒ ConnectorNetHttp

Returns a new instance of ConnectorNetHttp.



46
47
48
# File 'lib/sem4r_soap/http_connector.rb', line 46

def initialize(logger = nil)
  @logger = logger
end

Instance Method Details

#download(url, path_name) ⇒ Object

Downloads content at url in path_name



81
82
83
84
85
86
87
88
# File 'lib/sem4r_soap/http_connector.rb', line 81

def download(url, path_name)
  uri     = URI.parse(url)
  http    = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Get.new(uri.request_uri)
  request.initialize_http_header({"User-Agent" => "My Ruby Script"})
  response = http.request(request)
  File.open(path_name, "w") { |f| f.write(response.body) }
end

#logger=(log) ⇒ Object



50
51
52
# File 'lib/sem4r_soap/http_connector.rb', line 50

def logger=(log)
  @logger = log
end

#post(uri, str, headers) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/sem4r_soap/http_connector.rb', line 90

def post(uri, str, headers)
  unless uri.respond_to? :path
    uri     = URI.parse(uri) # might raise URI::InvalidURIError
  end
  retries = 0; response = nil
  while retries <= MAX_RETRIES and response.nil?
    retries += 1
    begin
      client   = client_for_uri(uri)
      response = client.post(uri.path, str, headers)
      status   = response.code.to_i
      # pp response.class.to_s
      # pp "status: #{status}"
    rescue StandardError => e
      @logger.warn("request_post retries!!! #{e.to_s}") if @logger
      invalidate_client_for_uri(uri)
      sleep(1 * retries) # wait
    end
  end

  unless response
    raise "Connection Error, Network is down?? :-((("
  end
  response
end

#send(service_url, soap_action, soap_request_xml) ⇒ Object

Send a soap request

Raises:

  • (URI::InvalidURIError)

    when service_url is incorrect



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sem4r_soap/http_connector.rb', line 59

def send(service_url, soap_action, soap_request_xml)
  uri     = URI.parse(service_url) # might raise URI::InvalidURIError

  headers = {
      "Content-Type"   => "text/xml; charset=utf-8",
      "Content-Length" => soap_request_xml.length.to_s,
      "SOAPAction"     => soap_action}

  @logger.info("Post to #{uri.path} (#{soap_action})") if @logger
  dump_soap_request(service_url, soap_request_xml)
  response = post(uri, soap_request_xml, headers)
  unless response
    raise Sem4rError, "Connection Error"
  end
  response_xml = response.body
  dump_soap_response(service_url, response_xml)
  response_xml
end