Class: Curl::Multi

Inherits:
Object
  • Object
show all
Defined in:
lib/curl-multi/version.rb,
lib/curl-multi.rb

Overview

:nodoc:

Defined Under Namespace

Modules: VERSION

Instance Method Summary collapse

Constructor Details

#initializeMulti

Returns a new instance of Multi.



132
133
134
# File 'lib/curl-multi.rb', line 132

def initialize
  @handles = []
end

Instance Method Details

#add(url, body, success, failure = lambda{}) ⇒ Object

Add a URL to the queue of items to fetch



137
138
139
140
141
142
143
144
# File 'lib/curl-multi.rb', line 137

def add(url, body, success, failure=lambda{})
  while (h = add_to_curl(Req.new(url, success, failure), url, body)) == nil
    select([], [])
  end
  @handles << h
  work()
  :ok
end

#cleanupObject



127
128
129
130
# File 'lib/curl-multi.rb', line 127

def cleanup
  @handles = []
  :ok
end

#get(url, success, failure = lambda{}) ⇒ Object



91
92
93
# File 'lib/curl-multi.rb', line 91

def get(url, success, failure=lambda{})
  add(url, nil, success, failure)
end

#inspectObject Also known as: to_s



88
# File 'lib/curl-multi.rb', line 88

def inspect() '{Curl::Multi' + @handles.map{|h| ' '+h.url}.join + '}' end

#post(url, params, success, failure = lambda{}) ⇒ Object



95
96
97
# File 'lib/curl-multi.rb', line 95

def post(url, params, success, failure=lambda{})
  add(url, URI.escape_params(params), success, failure)
end

#post_process(handles) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/curl-multi.rb', line 146

def post_process(handles)
  errors = []
  handles.each do |h|
    begin
      yield(h) # This ought not to raise anything.
    rescue => ex
      errors << ex
    end
  end
  return errors
end

#select(rfds, wfds) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/curl-multi.rb', line 99

def select(rfds, wfds)
  loop do
    ready_rfds, ready_wfds = c_select(rfds.map{|s|s.fileno},
                                      wfds.map{|s|s.fileno})
    work() # Curl may or may not have work to do, but we can't tell.
    return ready_rfds, ready_wfds if ready_rfds.any? or ready_wfds.any?
    return [], [] if rfds == [] and wfds == []
  end
end

#sizeObject



86
# File 'lib/curl-multi.rb', line 86

def size() @handles.size end

#workObject

Do as much work as possible without blocking on the network. That is, read as much data as is ready and write as much data as we have buffer space for. If any complete responses arrive, call their handlers.

Raises:



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/curl-multi.rb', line 112

def work
  perform()

  done, @handles = @handles.partition{|h| h.done?}
  failed, done = done.partition{|h| h.failed?}

  errors = []
  errors += post_process(done) {|x| x.do_success}
  errors += post_process(failed) {|x| x.do_failure}
  raise errors[0] if errors.size == 1
  raise MultiError.new(errors) if errors.size > 1

  :ok
end