class Typhoeus::EasyFactory

@api private
This is done by this class.
on_complete callback to be set.
Before an easy is ready to be added to a multi the
This is a Factory for easies to be used in the hydra.

def easy

Returns:
  • (Ethon::Easy) - The easy.

Other tags:
    Example: Return easy. -
def easy
  @easy ||= Typhoeus::Pool.get
end

def get

Returns:
  • (Ethon::Easy) - The easy.

Other tags:
    Example: Prepared easy. -
def get
  begin
    read_callback_body = request.options.delete(:set_read_callback)
    easy.http_request(
      request.base_url.to_s,
      request.options.fetch(:method, :get),
      sanitize(request.options)
    )
    # this needs to happen after http_request because
    # ethon will set infilesize to zero if form.empty?
    set_read_callback(read_callback_body) if !read_callback_body.nil?
  rescue Ethon::Errors::InvalidOption => e
    help = provide_help(e.message.match(/:\s(\w+)/)[1])
    raise $!, "#{$!}#{help}", $!.backtrace
  end
  set_callback
  easy
end

def initialize(request, hydra = nil)

Parameters:
  • hydra (Hydra) -- The hydra to build an easy for.
  • request (Request) -- The request to build an easy for.

Other tags:
    Example: Create easy factory. -
def initialize(request, hydra = nil)
  @request = request
  @hydra = hydra
end

def provide_help(option)

def provide_help(option)
  if new_option = CHANGED_OPTIONS[option.to_sym]
    "\nPlease try #{new_option} instead of #{option}." if new_option
  elsif REMOVED_OPTIONS.include?(option.to_sym)
    "\nThe option #{option} was removed."
  end
end

def sanitize(options)

def sanitize(options)
  # set nosignal to true by default
  # this improves thread safety and timeout behavior
  sanitized = {:nosignal => true}
  options.each do |k,v|
    s = k.to_sym
    next if SANITIZE_IGNORE.include?(s)
    if new_option = RENAMED_OPTIONS[k.to_sym]
      warn("Deprecated option #{k}. Please use #{new_option} instead.")
      sanitized[new_option] = v
    # sanitize timeouts
    elsif SANITIZE_TIMEOUT.include?(s)
      if !v.integer?
        warn("Value '#{v}' for option '#{k}' must be integer.")
      end
      sanitized[k] = v.ceil
    else
      sanitized[k] = v
    end
  end
  sanitize_timeout!(sanitized, :timeout)
  sanitize_timeout!(sanitized, :connecttimeout)
  sanitized
end

def sanitize_timeout!(options, timeout)

def sanitize_timeout!(options, timeout)
  timeout_ms = :"#{timeout}_ms"
  if options[timeout] && options[timeout].round != options[timeout]
    if !options[timeout_ms]
      options[timeout_ms] = (options[timeout]*1000).ceil
    end
    options[timeout] = options[timeout].ceil
  end
  options
end

def set_callback

Returns:
  • (Ethon::Easy) - The easy.

Other tags:
    Example: Set callback. -
def set_callback
  if request.streaming?
    response = nil
    easy.on_headers do |easy|
      response = Response.new(Ethon::Easy::Mirror.from_easy(easy).options)
      request.execute_headers_callbacks(response)
    end
    request.on_body.each do |callback|
      easy.on_body do |chunk, easy|
        callback.call(chunk, response)
      end
    end
  else
    easy.on_headers do |easy|
      request.execute_headers_callbacks(Response.new(Ethon::Easy::Mirror.from_easy(easy).options))
    end
  end
  request.on_progress.each do |callback|
    easy.on_progress do |dltotal, dlnow, ultotal, ulnow, easy|
      callback.call(dltotal, dlnow, ultotal, ulnow, response)
    end
  end
  easy.on_complete do |easy|
    request.finish(Response.new(easy.mirror.options))
    Typhoeus::Pool.release(easy)
    if hydra && !hydra.queued_requests.empty?
      hydra.dequeue_many
    end
  end
end

def set_read_callback(body)

Returns:
  • (Ethon::Easy) - The easy.

Parameters:
  • body (String/File) -- The body read by the readfunction.
def set_read_callback(body)
  easy.infilesize_large =
    if body.respond_to?(:bytesize)
      body.bytesize
    elsif body.respond_to?(:size)
      body.size
    end
  easy.upload = true
  easy.set_read_callback(body)
  easy
end