Class: Tap::Http::Submit

Inherits:
Task
  • Object
show all
Defined in:
lib/tap/http/submit.rb

Overview

Tap::Http::Submit::manifest submits a captured http request

Direct Known Subclasses

Get

Defined Under Namespace

Classes: FormNode

Constant Summary collapse

Form =
WWW::Mechanize::Form

Instance Method Summary collapse

Instance Method Details

#process(*requests) ⇒ Object



28
29
30
31
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/tap/http/submit.rb', line 28

def process(*requests)
  # initialize mechanize agent
  agent = WWW::Mechanize.new
  agent.log = app.logger unless app.quiet
  
  # reconfigures agent with mechanize config
  mechanize.config.dup.bind(agent)
  
  agent.pre_connect_hooks << lambda do |options|
    connection = options[:connection]
    unless connection.started?
      log :mechanize, "#{connection.address}#{connection.use_ssl? ? ' (ssl)' : ''}"
    end
  end
  
  # handle requests
  requests.inject(nil) do |last_page, request|
    request = symbolize(request)
    
    # note get handles nil request methods (ie '') and has to
    # reassign uri to url to make mechanize happy
    page = case request[:request_method].to_s
    when /get/i, '' then agent.get(request.merge(:url => request[:uri]))
    when /post/i    then agent.submit(prepare_form(request), nil)
    when /head/i    then agent.head(nil, nil, request)
    when /put/i     then agent.put(nil, nil, request)
    when /delete/i  then agent.delete(nil, nil, request)
    else raise "unsupported request method: #{request.inspect}" 
    end
    
    # process page results if desired
    block_given? ? yield(page) : page.content
  end
end