Class: Tap::Mechanize::Capture

Inherits:
Controller
  • Object
show all
Includes:
Utils, Ubiquity::Utils
Defined in:
lib/tap/mechanize/capture.rb

Overview

:startdoc::controller :startdoc::ubiquity

Constant Summary collapse

PREFIX =
'__redirect_http_'
REDIRECT_PARAMETER =
'__redirect_http_original_action'

Constants included from Utils

Utils::EOL

Instance Method Summary collapse

Methods included from Utils

each_member, headerize, parse_http_request, parse_multipart, parse_rack_request, parse_webrick_request, splat

Instance Method Details

#create(id, keep_content = true) ⇒ Object



21
22
23
24
25
26
# File 'lib/tap/mechanize/capture.rb', line 21

def create(id, keep_content=true)
  persistence.create(id) do |io|
    io << YAML.dump([parse_request(keep_content)])
  end
  download(id)
end

#destroy(id) ⇒ Object



54
55
56
57
# File 'lib/tap/mechanize/capture.rb', line 54

def destroy(id)
  persistence.destroy(id)
  redirect uri(:index)
end

#download(id) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/tap/mechanize/capture.rb', line 33

def download(id)
  path = persistence.path(id)
  filename = id
  filename += ".yml" if File.extname(id) == ""
  
  response['Content-Type'] = "text/plain"
  response['Content-Disposition'] = "attachment; filename=#{filename};"
  persistence.read(id)
end

#httpObject

Parses HTTP request



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/tap/mechanize/capture.rb', line 112

def http
  if request.get?
    render 'http.erb'
  else
    keep_content = request.params['keep_content'] == "true"
    
    hash = {}
    parse_http_request(request.params['http'], keep_content).each_pair do |key, value| 
      hash[key.to_s] = value
    end
    
    # remove extranous data
    hash.delete('headers')
    hash.delete('version')
    
    response['Content-Type'] = "text/plain"
    YAML.dump(hash)
  end
end

#indexObject

Brings up the tutorial.



17
18
19
# File 'lib/tap/mechanize/capture.rb', line 17

def index
  render 'index.erb', :locals => {:captures => persistence.index }
end

#redirect_httpObject

Returns the redirection script.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/tap/mechanize/capture.rb', line 77

def redirect_http
  css = render 'redirect.css'
  script = render 'redirect.js', :locals => {
    :redirect_parameter => REDIRECT_PARAMETER,
    :redirect_action => uri(:update),
  }
  
  content = render 'redirect_http.erb', :locals => {
    :css => css,
    :script => script
  }
  
  if request.get?
    response['Content-Type'] = 'text/plain'
    %Q{
<div id="#{prefix}">
#{content}
</div>}
  else
    response['Content-Type'] = 'text/javascript'
  %Q{
if(current = document.getElementById("#{prefix}")) {
  RedirectHttp.revert();
} else {
  var div = document.createElement("div");
  div.id = "#{prefix}";
  div.innerHTML = #{content.to_json};
  document.body.insertBefore(div, document.body.firstChild);
  RedirectHttp.redirect();
}
}
  end
end

#sayObject

Say is the target of the tutorial.



72
73
74
# File 'lib/tap/mechanize/capture.rb', line 72

def say
  "<pre>#{request.params['words']}</pre>"
end

#show(id) ⇒ Object



28
29
30
31
# File 'lib/tap/mechanize/capture.rb', line 28

def show(id)
  response['Content-Type'] = "text/plain"
  persistence.read(id)
end

#testObject



67
68
69
# File 'lib/tap/mechanize/capture.rb', line 67

def test
  render 'test.erb'
end

#tutorialObject

Brings up a tutorial teaching how to capture and resubmit forms.



60
61
62
63
64
65
# File 'lib/tap/mechanize/capture.rb', line 60

def tutorial
  serve js_injection(:redirect_http) do |link|
    content = render 'tutorial.erb'
    content + link
  end
end

#update(id = "request", keep_content = true) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/tap/mechanize/capture.rb', line 43

def update(id="request", keep_content=true)
  path = persistence.path(id)
  requests = File.exists?(path) ? YAML.load_file(path) : []
  requests << parse_request(keep_content)
  
  persistence.update(id) do |io|
    io << YAML.dump(requests)
  end
  download(id)
end