Class: Web::Unit::Response
- Includes:
- Utils
- Defined in:
- lib/web/unit/response.rb
Constant Summary collapse
- @@host =
nil- @@port =
nil- @@http =
nil
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#forms ⇒ Object
readonly
Returns the value of attribute forms.
-
#frames ⇒ Object
readonly
Returns the value of attribute frames.
-
#images ⇒ Object
readonly
Returns the value of attribute images.
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#password ⇒ Object
Returns the value of attribute password.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#tables ⇒ Object
readonly
Returns the value of attribute tables.
-
#tree ⇒ Object
readonly
Returns the value of attribute tree.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
-
.get(url, data = nil, up = nil) ⇒ Object
— Response.get(url,data=nil) return a Response.
-
.html(html, url = nil) ⇒ Object
— Response.html( html) return a Response.
-
.post(url, data = nil, up = nil) ⇒ Object
— Response.post(url,data=nil) return a Response.
- .reset ⇒ Object
Instance Method Summary collapse
- #add_form(form) ⇒ Object
- #add_frame(frame) ⇒ Object
- #add_image(image) ⇒ Object
- #add_link(link) ⇒ Object
- #add_table(table) ⇒ Object
-
#click(str, x = nil, y = nil) ⇒ Object
— Response#click( str ) at first Response#readlink, and then, Form#submit.
- #do_connect(host, port) ⇒ Object
- #feed ⇒ Object
-
#find(tag) ⇒ Object
— Response#find( tag ) same as Htmlelem#find.
-
#form ⇒ Object
— Response#form short for Response.forms.
-
#format ⇒ Object
— Response#format.
-
#frame ⇒ Object
— Response#frame short for Response.frames.
- #freeze ⇒ Object
-
#image ⇒ Object
— Response#image.
- #init_html(html, url) ⇒ Object
- #init_http(url, method, data = nil, retried = false, multipart = false) ⇒ Object
- #js_open_objs ⇒ Object
-
#link ⇒ Object
— Response#link short for Response.links.
-
#open ⇒ Object
— Response#open short for Response.opens.
-
#opens ⇒ Object
— Response#opens.
-
#params ⇒ Object
— Response#param short for Response.forms.params.
-
#pbody ⇒ Object
— Response#pbody.
- #push(button = nil) ⇒ Object
-
#readlink(str) ⇒ Object
— Response#readlink( str ) same as Htmlelem#find.
-
#redirect ⇒ Object
— Response#redirect.
-
#search(str) ⇒ Object
— Response#search( str ) same as Htmlelem#search.
-
#submit(button = nil, x = nil, y = nil) ⇒ Object
— Response#submit( button=nil,x=nil,y=nil) short for Response.forms.submit.
-
#table ⇒ Object
— Response#table short for Response.tables.
-
#title ⇒ Object
— Response#title return title.
Methods included from Utils
#complete_url, #orthop_url, #parse_url
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
20 21 22 |
# File 'lib/web/unit/response.rb', line 20 def body @body end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
20 21 22 |
# File 'lib/web/unit/response.rb', line 20 def code @code end |
#forms ⇒ Object (readonly)
Returns the value of attribute forms.
21 22 23 |
# File 'lib/web/unit/response.rb', line 21 def forms @forms end |
#frames ⇒ Object (readonly)
Returns the value of attribute frames.
21 22 23 |
# File 'lib/web/unit/response.rb', line 21 def frames @frames end |
#images ⇒ Object (readonly)
Returns the value of attribute images.
21 22 23 |
# File 'lib/web/unit/response.rb', line 21 def images @images end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
21 22 23 |
# File 'lib/web/unit/response.rb', line 21 def links @links end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
20 21 22 |
# File 'lib/web/unit/response.rb', line 20 def method @method end |
#password ⇒ Object
Returns the value of attribute password.
22 23 24 |
# File 'lib/web/unit/response.rb', line 22 def password @password end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
20 21 22 |
# File 'lib/web/unit/response.rb', line 20 def response @response end |
#tables ⇒ Object (readonly)
Returns the value of attribute tables.
21 22 23 |
# File 'lib/web/unit/response.rb', line 21 def tables @tables end |
#tree ⇒ Object (readonly)
Returns the value of attribute tree.
21 22 23 |
# File 'lib/web/unit/response.rb', line 21 def tree @tree end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
20 21 22 |
# File 'lib/web/unit/response.rb', line 20 def url @url end |
#username ⇒ Object
Returns the value of attribute username.
22 23 24 |
# File 'lib/web/unit/response.rb', line 22 def username @username end |
Class Method Details
.get(url, data = nil, up = nil) ⇒ Object
— Response.get(url,data=nil)
return a Response.
28 29 30 31 32 33 |
# File 'lib/web/unit/response.rb', line 28 def self::get( url, data=nil, up=nil ) r = self.new r.username = up[0] if up r.password = up[1] if up r.init_http( url, "GET", data ) end |
.html(html, url = nil) ⇒ Object
— Response.html( html)
return a Response.
51 52 53 54 |
# File 'lib/web/unit/response.rb', line 51 def self::html( html, url=nil ) html = "<html>#{html}</html>" unless html =~ %r!^<html>! self.new.init_html( html, url ) end |
.post(url, data = nil, up = nil) ⇒ Object
— Response.post(url,data=nil)
return a Response.
39 40 41 42 43 44 |
# File 'lib/web/unit/response.rb', line 39 def self::post( url, data=nil, up=nil ) r = self.new r.username = up[0] if up r.password = up[1] if up r.init_http( url, "POST", data ) end |
.reset ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/web/unit/response.rb', line 56 def self::reset begin @@http.finish if @@http rescue IOError @@http = nil end @@host = nil @@port = nil @@http = nil end |
Instance Method Details
#add_form(form) ⇒ Object
169 170 171 |
# File 'lib/web/unit/response.rb', line 169 def add_form( form ) @forms.push( form ) end |
#add_frame(frame) ⇒ Object
173 174 175 |
# File 'lib/web/unit/response.rb', line 173 def add_frame( frame ) @frames.push( frame ) end |
#add_image(image) ⇒ Object
181 182 183 |
# File 'lib/web/unit/response.rb', line 181 def add_image( image ) @images.push( image ) end |
#add_link(link) ⇒ Object
165 166 167 |
# File 'lib/web/unit/response.rb', line 165 def add_link( link ) @links.push( link ) end |
#add_table(table) ⇒ Object
177 178 179 |
# File 'lib/web/unit/response.rb', line 177 def add_table( table ) @tables.push( table ) end |
#click(str, x = nil, y = nil) ⇒ Object
— Response#click( str )
at first Response#readlink, and then, Form#submit.
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/web/unit/response.rb', line 232 def click( str, x=nil, y=nil ) begin @tree.readlink( str ) rescue ElemNotFound matched = 0 @tree.find( 'input' ).each do |i| if i.class == InputSubmit matched = i.name if i.name == str || i.value == str elsif i.class == InputImage matched = i.name if i.name == str end end if matched == 0 raise ElemNotFound else submit( matched, x, y ) end end end |
#do_connect(host, port) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/web/unit/response.rb', line 75 def do_connect(host, port) if @@host != host or @@port != port @@host = host @@port = port begin @@http.finish if @@http rescue IOError @@http = nil end @@http = Net::HTTP::new( host, port ) end end |
#feed ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/web/unit/response.rb', line 144 def feed @forms = [] @tables = [] @links = [] @frames = [] @images = [] # added by patrick to make redirect test work if ( @response && @response['location'] && @code == "302" ) add_link( Link.new( 'href' => @response['location'], 'target' => '', 'onclick' => '' ) ) end # end added by patrick to make redirect test work p = Parser.new @tree = p.feed( self ) p.close freeze end |
#find(tag) ⇒ Object
— Response#find( tag )
same as Htmlelem#find.
205 206 207 |
# File 'lib/web/unit/response.rb', line 205 def find( tag ) @tree.find( tag ) end |
#form ⇒ Object
— Response#form
short for Response.forms[0]
297 298 299 |
# File 'lib/web/unit/response.rb', line 297 def form @forms[0] end |
#format ⇒ Object
— Response#format
383 384 385 386 387 388 389 |
# File 'lib/web/unit/response.rb', line 383 def format w = MemWriter.new p = HTMLParser.new( AbstractFormatter.new( w ) ) p.feed( @body ) p.close w.to_s end |
#frame ⇒ Object
— Response#frame
short for Response.frames[0]
315 316 317 |
# File 'lib/web/unit/response.rb', line 315 def frame @frames[0] end |
#freeze ⇒ Object
185 186 187 188 189 |
# File 'lib/web/unit/response.rb', line 185 def freeze @forms.each do |f| f.freeze end end |
#image ⇒ Object
— Response#image
279 280 281 |
# File 'lib/web/unit/response.rb', line 279 def image self.images[0] end |
#init_html(html, url) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/web/unit/response.rb', line 67 def init_html( html, url ) @url = url @method = 'html' @body = html feed self end |
#init_http(url, method, data = nil, retried = false, multipart = false) ⇒ Object
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/web/unit/response.rb', line 88 def init_http( url, method, data=nil, retried=false, multipart=false) url = $URLBASE + url unless url =~ %r!://! @url = url @method = method header = {} prot,host,port,path = parse_url( url ) do_connect( host, port) header.update( Cookies::header( path ) ) if @username != nil && @password != nil header['authorization'] = 'Basic ' + ["#{@username}:#{@password}"].pack('m').gsub(/\s+/, '') end begin if multipart header['Content-type'] = "multipart/form-data,boundary=#{BOUNDARY}" #puts data @response = @@http.post2( path, data.gsub(/\n/, "\r\n"), header) else case @method.upcase when "GET" then data path << '?' + data if data puts path if $DEBUG @response = @@http.get2( path, header ) when "POST" then if header['Content-type'] == nil header['Content-type'] = "application/x-www-form-urlencoded" end @response = @@http.post2( path, data, header ) end end rescue Errno::EPIPE @@host = nil raise if retried return self.init_http( url, method, data, true, multipart) end @body = @response.body @code = @response.code p @response['set-cookie'] if $DEBUG Cookies::update( @response['set-cookie'] ) case @response['content-type'] when %r!text/html! feed when %r!text/xml! return DomWalker::new( @response.body ) end raise HttpNotFound if @code == "404" self end |
#js_open_objs ⇒ Object
355 356 357 358 |
# File 'lib/web/unit/response.rb', line 355 def js_open_objs $stderr.puts "no longer support Response#js_open_objs, use Response#opens" opens end |
#link ⇒ Object
— Response#link
short for Response.links[0]
288 289 290 |
# File 'lib/web/unit/response.rb', line 288 def link @links[0] end |
#open ⇒ Object
— Response#open
short for Response.opens[0]
365 366 367 |
# File 'lib/web/unit/response.rb', line 365 def open opens[0] end |
#opens ⇒ Object
— Response#opens
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/web/unit/response.rb', line 335 def opens a = [] re_script = %r|<!--(.*?)// *-->|m re_open = %r|open\( *['"]([^'"]*)['"], *['"]([^'"]*)['"] *\)| body_rest = response.body while re_script =~ body_rest body_rest = $' rest = $1 while re_open =~ rest ah = {} ah['file'] = $1 ah['target'] = $2 ah['url'] = complete_url( ah['file'], @url ) a << JSciriptOpenObject::new( ah ) rest = $' end end a end |
#params ⇒ Object
— Response#param
short for Response.forms[0].params
271 272 273 |
# File 'lib/web/unit/response.rb', line 271 def params @forms[0].params end |
#pbody ⇒ Object
— Response#pbody
373 374 375 376 377 |
# File 'lib/web/unit/response.rb', line 373 def pbody puts "\n" + '-' * 72 puts self.format puts '-' * 72 end |
#push(button = nil) ⇒ Object
261 262 263 264 |
# File 'lib/web/unit/response.rb', line 261 def push( =nil ) $stderr.puts "no longer support Response#push, use Response#submit" submit end |
#readlink(str) ⇒ Object
— Response#readlink( str )
same as Htmlelem#find.
223 224 225 |
# File 'lib/web/unit/response.rb', line 223 def readlink( str ) @tree.readlink( str ) end |
#redirect ⇒ Object
— Response#redirect
323 324 325 326 327 328 329 |
# File 'lib/web/unit/response.rb', line 323 def redirect if @code == '302' @links[0].read else raise BadOperation end end |
#search(str) ⇒ Object
— Response#search( str )
same as Htmlelem#search.
214 215 216 |
# File 'lib/web/unit/response.rb', line 214 def search( str ) @tree.search( str ) end |
#submit(button = nil, x = nil, y = nil) ⇒ Object
— Response#submit( button=nil,x=nil,y=nil)
short for Response.forms[0].submit.
257 258 259 |
# File 'lib/web/unit/response.rb', line 257 def submit( =nil, x=nil, y=nil ) @forms[0].submit( , x, y ) end |
#table ⇒ Object
— Response#table
short for Response.tables[0]
306 307 308 |
# File 'lib/web/unit/response.rb', line 306 def table @tables[0] end |
#title ⇒ Object
— Response#title
return title.
196 197 198 |
# File 'lib/web/unit/response.rb', line 196 def title @tree.find( 'title' )[0].data end |