Class: Mizuno::ClientExchange

Inherits:
ContentExchange
  • Object
show all
Defined in:
lib/mizuno/client_exchange.rb

Overview

what do I want to happen on a timeout or error?

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ ClientExchange

Returns a new instance of ClientExchange.



10
11
12
13
# File 'lib/mizuno/client_exchange.rb', line 10

def initialize(client)
    super(false)
    @client = client
end

Instance Method Details

#onConnectionFailed(error) ⇒ Object



51
52
53
# File 'lib/mizuno/client_exchange.rb', line 51

def onConnectionFailed(error)
    @exception ||= error
end

#onException(error) ⇒ Object



47
48
49
# File 'lib/mizuno/client_exchange.rb', line 47

def onException(error)
    @exception ||= error
end

#onExpireObject



39
40
41
42
43
44
45
# File 'lib/mizuno/client_exchange.rb', line 39

def onExpire
    @client.clear(self)
    @response.timeout = true
    @response.status = -1
    @response.body = nil
    run_callback
end

#onResponseCompleteObject



32
33
34
35
36
37
# File 'lib/mizuno/client_exchange.rb', line 32

def onResponseComplete
    @client.clear(self)
    @response.status = getResponseStatus
    @response.body = getResponseContent
    run_callback
end

#onResponseHeader(name, value) ⇒ Object



28
29
30
# File 'lib/mizuno/client_exchange.rb', line 28

def onResponseHeader(name, value)
    @response[name.to_s] = value.to_s
end

#run_callbackObject



55
56
57
58
59
60
61
# File 'lib/mizuno/client_exchange.rb', line 55

def run_callback
    begin
        @callback.call(@response)
    rescue => error
        onException(error)
    end
end

#setup(url, options = {}, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mizuno/client_exchange.rb', line 15

def setup(url, options = {}, &block)
    @callback = block
    @response = ClientResponse.new(url)
    setURL(url)
    @response.ssl = (getScheme == 'https')
    setMethod((options[:method] or "GET").upcase)
    headers = options[:headers] and headers.each_pair { |k, v| 
        setRequestHeader(k, v) }
    return unless options[:body]
    body = StringIO.new(options[:body].read)
    setRequestContentSource(body.to_inputstream) 
end

#waitForDoneObject



63
64
65
66
# File 'lib/mizuno/client_exchange.rb', line 63

def waitForDone
    super
    throw(@exception) if @exception
end