Class: Solon::Net::HttpsGateway

Inherits:
Object
  • Object
show all
Defined in:
lib/solon/net/https_gateway.rb

Constant Summary collapse

MAX_RETRIES =
3
OPEN_TIMEOUT =
60
READ_TIMEOUT =
60

Instance Method Summary collapse

Constructor Details

#initialize(url, retry_safe = false, debug = false) ⇒ HttpsGateway

Returns a new instance of HttpsGateway.



18
19
20
21
22
# File 'lib/solon/net/https_gateway.rb', line 18

def initialize(url, retry_safe=false, debug=false)
  @url        = url
  @retry_safe = retry_safe
  @debug      = debug
end

Instance Method Details

#debug?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/solon/net/https_gateway.rb', line 28

def debug?
  @debug
end

#retry_exceptionsObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/solon/net/https_gateway.rb', line 63

def retry_exceptions
  retries = MAX_RETRIES
  begin
    yield
  rescue RetriableConnectionError => e
    retries -= 1
    puts e.inspect
    retry unless retries.zero?
    raise ConnectionError, e.message
  rescue ConnectionError
    retries -= 1
    retry if retry_safe? && !retries.zero?
    raise
  end
end

#retry_safe?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/solon/net/https_gateway.rb', line 24

def retry_safe?
  @retry_safe
end

#send(headers, data) ⇒ Object



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
59
60
61
# File 'lib/solon/net/https_gateway.rb', line 32

def send(headers, data)
  headers['Content-Type'] ||= "application/x-www-form-urlencoded"
  
  uri   = URI.parse(@url)
  
  http = ::Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = OPEN_TIMEOUT
  http.read_timeout = READ_TIMEOUT

  http.set_debug_output $stdout if debug?
  
  http.use_ssl      = true
  
  http.verify_mode    = ::OpenSSL::SSL::VERIFY_NONE
  
  retry_exceptions do 
    begin
      http.post(uri.request_uri, data, headers).body
    rescue EOFError => e
      raise ConnectionError, "The remote server dropped the connection"
    rescue Errno::ECONNRESET => e
      raise ConnectionError, "The remote server reset the connection"
    rescue Errno::ECONNREFUSED => e
      raise RetriableConnectionError, "The remote server refused the connection"
    rescue Timeout::Error, Errno::ETIMEDOUT => e
      raise ConnectionError, "The connection to the remote server timed out"
    end
  end
  
end