1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* protocol.rb,smtp.rb,pop.rb,http.rb: modify document.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1548 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
aamine 2001-06-27 00:59:08 +00:00
parent 37b3f674a7
commit cf37626401
3 changed files with 189 additions and 173 deletions

View file

@ -27,7 +27,7 @@ For details of HTTP, refer [RFC2616]
=== Getting Document From Server
Be care to ',' (comma) putted after "response".
This is required for feature compatibility.
This is required for compatibility.
require 'net/http'
Net::HTTP.start( 'some.www.server', 80 ) {|http|
@ -73,13 +73,16 @@ there's no need to change code if there's proxy or not.
Net::HTTP.version_1_1
host = 'www.ruby-lang.org'
path = '/'
begin
Net::HTTP.start( host, 80 ) {|http|
response , = http.get('/')
response , = http.get(path)
print response.body
}
rescue Net::ProtoRetriableError => err
if m = %r<http:([^/]+)>.match( err.response['location'] ) then
if m = %r<http://([^/]+)>.match( err.response['location'] ) then
host = m[1].strip
path = m.post_match
retry
end
end
@ -131,8 +134,7 @@ Yes, this is not thread-safe.
: new( address = 'localhost', port = 80, proxy_addr = nil, proxy_port = nil )
creates a new Net::HTTP object.
If proxy_addr is given, this method is equals to
Net::HTTP::Proxy(proxy_addr,proxy_port).
If proxy_addr is given, creates an Net::HTTP object with proxy support.
: start( address = 'localhost', port = 80, proxy_addr = nil, proxy_port = nil )
: start( address = 'localhost', port = 80, proxy_addr = nil, proxy_port = nil ) {|http| .... }
@ -145,15 +147,14 @@ Yes, this is not thread-safe.
return value is a String.
: get_print( address, path, port = 80 )
gets entity body from path and print it.
return value is an entity body (a String).
gets entity body from path and output it to $stdout.
: Proxy( address, port = 80 )
creates a HTTP proxy class.
Arguments are address/port of proxy host.
You can replace HTTP class by this proxy class.
You can replace HTTP class with created proxy class.
If ADDRESS is nil, this method returns self (Net::HTTP class).
If ADDRESS is nil, this method returns self (Net::HTTP).
# example
proxy_class = Net::HTTP::Proxy( 'proxy.foo.org', 8080 )
@ -168,7 +169,7 @@ Yes, this is not thread-safe.
If self is a class which was created by HTTP::Proxy(), true.
: port
HTTP default port (80).
default HTTP port (80).
=== Instance Methods
@ -208,30 +209,32 @@ Yes, this is not thread-safe.
true if self is a HTTP proxy class
: proxy_address
address of proxy host. If self is not a proxy, nil.
address of proxy host. If self does not use a proxy, nil.
: proxy_port
port number of proxy host. If self is not a proxy, nil.
port number of proxy host. If self does not use a proxy, nil.
: get( path, header = nil, dest = '' )
: get( path, header = nil ) {|str| .... }
gets data from "path" on connecting host.
"header" must be a Hash like { 'Accept' => '*/*', ... }.
Response body is written into "dest" by using "<<" method.
gets data from PATH on the connecting host.
HEADER must be a Hash like { 'Accept' => '*/*', ... }.
Response body is written into DEST by using "<<" method.
This method returns Net::HTTPResponse object.
If called with block, give a part String of entity body.
If called with block, gives entity body little by little
to the block (as String).
In version 1.1, this method might raises exception for also
3xx (redirect). On the case you can get response object by
err.response.
3xx (redirect). On the case you can get a HTTPResponse object
by "anException.response".
In version 1.2, this method never raises exception.
# version 1.1 (Ruby 1.6)
# version 1.1 (bundled with Ruby 1.6)
response, body = http.get( '/index.html' )
# version 1.2 (Ruby 1.7 or later)
# version 1.2 (bundled with Ruby 1.7 or later)
response = http.get( '/index.html' )
# compatible in both version
@ -244,16 +247,20 @@ Yes, this is not thread-safe.
f.write str
end
}
# some effect
# same effect
File.open( 'save.txt', 'w' ) {|f|
http.get '/~foo/', nil, f
}
: head( path, header = nil )
gets only header from "path" on connecting host.
"header" is a Hash like { 'Accept' => '*/*', ... }.
gets only header from PATH on the connecting host.
HEADER is a Hash like { 'Accept' => '*/*', ... }.
This method returns a Net::HTTPResponse object.
You can http header from this object like:
In version 1.1, this method might raises exception for also
3xx (redirect). On the case you can get a HTTPResponse object
by "anException.response".
response = nil
Net::HTTP.start( 'some.www.server', 80 ) {|http|
@ -274,6 +281,10 @@ Yes, this is not thread-safe.
If called with block, gives a part of entity body string.
In version 1.1, this method might raises exception for also
3xx (redirect). On the case you can get a HTTPResponse object
by "anException.response".
# version 1.1
response, body = http.post( '/index.html', 'querytype=subject&target=ruby' )
# version 1.2
@ -292,13 +303,15 @@ Yes, this is not thread-safe.
http.post '/index.html', 'querytype=subject&target=ruby', nil, f
}
: request( request, [data] )
: request( request, [src] ) {|response| .... }
sends REQUEST to (remote) http server. This method also writes
string from DATA string if REQUEST is a post/put request.
(giving DATA for get/head request causes ArgumentError.)
: request( request [, data] )
: request( request [, data] ) {|response| .... }
sends a HTTPRequest object REQUEST to (remote) http server.
This method also writes string from DATA string if REQUEST is
a post/put request. Giving DATA for get/head request causes
ArgumentError.
If called with block, gives a HTTPResponse object to the block.
If called with block, gives a HTTPResponse object to the block
with connecting server.
== class Net::HTTP::Get, Head, Post

View file

@ -28,7 +28,6 @@ This example retrieves mails from server and delete it (on server).
Mails are written in file named 'inbox/1', 'inbox/2', ....
Replace 'pop3.server.address' your POP3 server address.
require 'net/pop'
Net::POP3.start( 'pop3.server.address', 110,
@ -38,9 +37,9 @@ Replace 'pop3.server.address' your POP3 server address.
else
i = 0
pop.each_mail do |m| # or "pop.mails.each ..."
File.open( 'inbox/' + i.to_s, 'w' ) do |f|
File.open( 'inbox/' + i.to_s, 'w' ) {|f|
f.write m.pop
end
}
m.delete
i += 1
end
@ -51,18 +50,22 @@ Replace 'pop3.server.address' your POP3 server address.
=== Shorter Version
require 'net/pop'
i = 0
Net::POP3.start( 'pop3.server.address', 110,
'YourAccount', 'YourPassword' ) {|pop|
if pop.mails.empty? then
puts 'no mail.'
else
i = 0
pop.delete_all do |m|
File.open( 'inbox/' + i.to_s, 'w' ) {|f|
f.write m.pop
}
i += 1
end
end
}
And this is more shorter example.
And here is more shorter example.
require 'net/pop'
i = 0
@ -129,20 +132,20 @@ net/pop also supports APOP authentication. There's two way to use APOP:
end
: foreach( address = 'localhost', port = 110, account, password ) {|mail| .... }
starts protocol and iterate for each POPMail object.
starts POP3 protocol and iterates for each POPMail object.
This method equals to
Net::POP3.start( address, port, account, password ) do |pop|
pop.each do |m|
Net::POP3.start( address, port, account, password ) {|pop|
pop.each_mail do |m|
yield m
end
end
}
Typical usage:
Net::POP3.foreach( addr, nil, acnt, pass ) do |m|
m.pop file
m.delete
# example
Net::POP3.foreach( 'your.pop.server', 110,
'YourAccount', 'YourPassword' ) do |m|
file.write m.pop
m.delete if $DELETE
end
: delete_all( address = 'localhost', port = 110, account, password )