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_'

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

#commandObject



64
65
66
# File 'lib/tap/mechanize/capture.rb', line 64

def command
  serve js_injection(:redirect_http)
end

#create(id) ⇒ Object



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

def create(id)
  data.create(:data, id) do |io|
    io << YAML.dump([parse_request])
  end
  download(id)
end

#destroy(id) ⇒ Object



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

def destroy(id)
  data.destroy(:data, 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 = data.path(:data, id)
  filename = id
  filename += ".yml" if File.extname(id) == ""
  
  response['Content-Type'] = "text/plain"
  response['Content-Disposition'] = "attachment; filename=#{filename};"
  data.read(:data, id)
end

#httpObject

Parses HTTP request



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/tap/mechanize/capture.rb', line 124

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 => data.index(:data) }
end

#redirect_httpObject

Returns the redirection script.



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
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/tap/mechanize/capture.rb', line 78

def redirect_http
  
  # note configs are formatted into the javascript; keys MUST be valid
  # names, but values can be anything that converts to json
  configs = [
    [:submit_request, false],
    [:keep_content, false],
    [:keep_headers, false],
    [:name, 'request']
  ]
  
  css = render 'redirect.css'
  script = render 'redirect.js', :locals => {
    :redirect_action => uri(:update),
    :configs => configs
  }
  
  content = render 'redirect_http.erb', :locals => {
    :css => css,
    :script => script,
    :configs => configs
  }
  
  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.



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

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"
  data.read(:data, id)
end

#testObject



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

def test
  render 'test.erb'
end

#tutorialObject

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



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

def tutorial
  render 'tutorial.erb'
end

#update(id = "request") ⇒ Object



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

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