Re-namespacing a bit to clear out some fairly old stuff from the top level
This commit is contained in:
44
oldstuff/RubyFun/cookbook/014/01.rb
Normal file
44
oldstuff/RubyFun/cookbook/014/01.rb
Normal file
@@ -0,0 +1,44 @@
|
||||
require 'open-uri'
|
||||
puts 'using "open-uri"'
|
||||
puts open('http://oreilly.com/').read(200)
|
||||
|
||||
# ------------------------------------------------------------
|
||||
require 'net/http'
|
||||
puts 'using "net/http"'
|
||||
response = Net::HTTP.get_response('oreilly.com', '/about/')
|
||||
puts 'response.code=' + response.code.to_s
|
||||
puts 'response.body.size=' + response.body.size.to_s
|
||||
puts 'response[\'Content-type\']=' + response['Content-type']
|
||||
puts 'response[0, 200]' + response.body[0, 200]
|
||||
|
||||
|
||||
# ------------------------------------------------------------
|
||||
require 'uri'
|
||||
puts 'using "net/http" with a URI'
|
||||
puts 'request object:' + \
|
||||
Net::HTTP.get(URI.parse("http://oreilly.com")).to_s
|
||||
response = Net::HTTP.get_response(URI.parse("http://oreilly.com/about/"))
|
||||
|
||||
# .........
|
||||
puts "Success!" if response.is_a? Net::HTTPOK
|
||||
|
||||
puts case response.code[0]
|
||||
when ?1 then "Status code indicates an HTTP informational response."
|
||||
when ?2 then "Status code indicates success."
|
||||
when ?3 then "Status code indicates redirection."
|
||||
when ?4 then "Status code indicates client error."
|
||||
when ?5 then "Status code indicates server error."
|
||||
else "Non-standard status code."
|
||||
end
|
||||
|
||||
puts 'Server=' + response['Server']
|
||||
puts 'SERVER=' + response['SERVER']
|
||||
|
||||
puts 'all keys:'
|
||||
response.each_key { |key| puts " #{key}" }
|
||||
|
||||
Net::HTTP.get_response('oreilly.com', '/about/') do |response|
|
||||
response.read_body do |segment|
|
||||
puts "Received segment of #{segment.size} byte(s)!"
|
||||
end
|
||||
end
|
28
oldstuff/RubyFun/cookbook/014/02.rb
Normal file
28
oldstuff/RubyFun/cookbook/014/02.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
require 'net/http'
|
||||
require 'uri'
|
||||
|
||||
uri = URI.parse("https://www.donotcall.gov/")
|
||||
puts 'uri=' + uri.to_s
|
||||
|
||||
request = Net::HTTP.new(uri.host, uri.port)
|
||||
# response = request.get('/')
|
||||
# ^--- would have resulted in an error
|
||||
|
||||
require 'net/https'
|
||||
puts 'making a request with use_ssl=true and verify_mode=VERIFY_NONE'
|
||||
request.use_ssl = true
|
||||
request.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
||||
response = request.get('/')
|
||||
|
||||
puts 'response.body.size=' + response.body.size.to_s
|
||||
|
||||
|
||||
request = Net::HTTP.new(uri.host, uri.port)
|
||||
puts 'making a request with use_ssl=true, ' + \
|
||||
'ca_path=/etc/ssl/certs and verify_mode=VERIFY_PEER'
|
||||
request.use_ssl = true
|
||||
request.ca_path = "/etc/ssl/certs/"
|
||||
request.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
||||
response = request.get('/')
|
||||
|
||||
puts 'response.body.size=' + response.body.size.to_s
|
64
oldstuff/RubyFun/cookbook/014/03.rb
Normal file
64
oldstuff/RubyFun/cookbook/014/03.rb
Normal file
@@ -0,0 +1,64 @@
|
||||
require 'net/http'
|
||||
require 'uri'
|
||||
|
||||
|
||||
# A simple wrapper method that accepts either strings or URI objects
|
||||
# and performs an HTTP GET.
|
||||
|
||||
module Net
|
||||
class HTTP
|
||||
def HTTP.get_with_headers(uri, headers=nil)
|
||||
uri = URI.parse(uri) if uri.respond_to? :to_str
|
||||
start(uri.host, uri.port) do |http|
|
||||
return http.get(uri.path, headers)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Let's get a web page in German.
|
||||
|
||||
res = Net::HTTP.get_with_headers('http://www.google.com/',
|
||||
{'Accept-Language' => 'de'})
|
||||
|
||||
# Check a bit of the body to make sure it's really in German
|
||||
s = res.body.size
|
||||
puts 'part of body in german=' + res.body[s-3670..s-3470].to_s
|
||||
|
||||
|
||||
Net::HTTP.get_with_headers('http://www.google.com/',
|
||||
{'User-Agent' => 'Ruby Web Browser v1.0'})
|
||||
|
||||
|
||||
uncompressed = Net::HTTP.get_with_headers('http://www.cnn.com/')
|
||||
puts 'uncompressed body size=' + uncompressed.body.size.to_s
|
||||
|
||||
gzipped = Net::HTTP.get_with_headers('http://www.cnn.com/',
|
||||
{'Accept-Encoding' => 'gzip'})
|
||||
puts 'gzipped Content-Encoding=' + gzipped['Content-Encoding']
|
||||
puts 'gzipped body size=' + gzipped.body.size.to_s
|
||||
|
||||
require 'zlib'
|
||||
require 'stringio'
|
||||
|
||||
body_io = StringIO.new(gzipped.body)
|
||||
unzipped_body = Zlib::GzipReader.new(body_io).read()
|
||||
|
||||
puts 'unzipped body size=' + unzipped_body.size.to_s
|
||||
|
||||
|
||||
uri = URI.parse('http://www.google.com/')
|
||||
request = Net::HTTP::Get.new(uri.path)
|
||||
['en_us', 'en', 'en_gb', 'ja'].each do |language|
|
||||
request.add_field('Accept-Language', language)
|
||||
end
|
||||
puts 'request[\'Accept-Language\']=' + request['Accept-Language'].to_s
|
||||
|
||||
Net::HTTP.start(uri.host, uri.port) do |http|
|
||||
response = http.request(request)
|
||||
puts 'response[\'Content-Type\']=' + response['Content-Type']
|
||||
puts 'response headers:'
|
||||
response.each_key do |key|
|
||||
puts " #{key}"
|
||||
end
|
||||
end
|
@@ -0,0 +1,29 @@
|
||||
require 'resolv'
|
||||
require 'resolv-replace'
|
||||
|
||||
|
||||
def multiple_lookup(*names)
|
||||
dns = Resolv::DNS.new
|
||||
results = {}
|
||||
threads = []
|
||||
names.each do |name|
|
||||
threads << Thread.new(name) do |name|
|
||||
begin
|
||||
dns.each_address(name) { |a| (results[name] ||= []) << a }
|
||||
rescue Resolv::ResolvError
|
||||
results[name] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
threads.each { |t| t.join }
|
||||
return results
|
||||
end
|
||||
|
||||
|
||||
domains = ("a".."z").collect { |l| l + '.com' }
|
||||
multiple_lookup(*domains).sort.each do |name, addresses|
|
||||
if addresses
|
||||
puts "#{name}: #{addresses.size} " + \
|
||||
"address#{addresses.size == 1 ? "" : "es"}"
|
||||
end
|
||||
end
|
11
oldstuff/RubyFun/cookbook/014/04-list-oreilly-mx-ns.rb
Normal file
11
oldstuff/RubyFun/cookbook/014/04-list-oreilly-mx-ns.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
require 'resolv'
|
||||
|
||||
dns = Resolv::DNS.new
|
||||
domain = "oreilly.com"
|
||||
dns.each_resource(domain, Resolv::DNS::Resource::IN::MX) do |mail_server|
|
||||
puts mail_server.exchange
|
||||
end
|
||||
|
||||
dns.each_resource(domain, Resolv::DNS::Resource::IN::NS) do |nameserver|
|
||||
puts nameserver.name
|
||||
end
|
3
oldstuff/RubyFun/cookbook/014/04.rb
Normal file
3
oldstuff/RubyFun/cookbook/014/04.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
require 'resolv'
|
||||
|
||||
Resolv::DNS.new.each_address('oreilly.com') { |addr| puts addr }
|
13
oldstuff/RubyFun/cookbook/014/05-headerful-message.rb
Normal file
13
oldstuff/RubyFun/cookbook/014/05-headerful-message.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
require 'rubygems'
|
||||
require 'action_mailer'
|
||||
|
||||
|
||||
class SimpleMailer < ActionMailer::Base
|
||||
def headerful_message
|
||||
@headers['X-custom-header'] = 'Its value'
|
||||
body 'Body'
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
puts SimpleMailer.headerful_message
|
18
oldstuff/RubyFun/cookbook/014/05-minimalist.rb
Normal file
18
oldstuff/RubyFun/cookbook/014/05-minimalist.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
require 'rubygems'
|
||||
require 'net/smtp'
|
||||
require 'smtp-tls'
|
||||
|
||||
|
||||
body = <<EOM
|
||||
To: daniel.buch+rubytest@gmail.com
|
||||
From: daniel.buch@gmail.com
|
||||
Subject: minimalist message
|
||||
|
||||
AHOY
|
||||
EOM
|
||||
|
||||
Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com',
|
||||
'daniel.buch@gmail.com', STDIN.readline, :login) do |smtp|
|
||||
smtp.send_message(body, 'daniel.buch@gmail.com',
|
||||
'daniel.buch+rubytest@gmail.com')
|
||||
end
|
31
oldstuff/RubyFun/cookbook/014/05-with-dir-dump-attachment.rb
Normal file
31
oldstuff/RubyFun/cookbook/014/05-with-dir-dump-attachment.rb
Normal file
@@ -0,0 +1,31 @@
|
||||
require 'rubygems'
|
||||
require 'action_mailer'
|
||||
require 'mime/types'
|
||||
|
||||
|
||||
class SimpleMailer < ActionMailer::Base
|
||||
def directory_dump_message(recipient, directory)
|
||||
from 'directory-dump@localhost'
|
||||
recipients recipient
|
||||
subject "Dump of #{directory}"
|
||||
body %{Here are the files currently in "#{directory}":}
|
||||
|
||||
Dir.new(directory).each do |f|
|
||||
path = File.join(directory, f)
|
||||
if File.file? path
|
||||
mime_type = MIME::Types.of(f).first
|
||||
content_type = (mime_type ? mime_type.content_type :
|
||||
'application/octet-stream')
|
||||
attachments[f] = {
|
||||
:mime_type => mime_type,
|
||||
:encoding => ('quoted-printable' if content_type =~ /^text\//),
|
||||
:content => File.read(path)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
puts SimpleMailer.directory_dump_message('daniel.buch+rubytest@gmail.com',
|
||||
'/tmp')
|
28
oldstuff/RubyFun/cookbook/014/05.rb
Normal file
28
oldstuff/RubyFun/cookbook/014/05.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
require 'rubygems'
|
||||
require 'action_mailer'
|
||||
|
||||
|
||||
class SimpleMailer < ActionMailer::Base
|
||||
def simple_message(recipient)
|
||||
from 'daniel.buch@gmail.com'
|
||||
recipients recipient
|
||||
subject 'A single-part message for you'
|
||||
body 'This message has a plain text body.'
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
puts 'generated message:'
|
||||
puts SimpleMailer.simple_message('daniel.buch+rubytest@gmail.com')
|
||||
|
||||
|
||||
ActionMailer::Base.smtp_settings = {
|
||||
:tls => true,
|
||||
:address => 'smtp.gmail.com',
|
||||
:port => "587",
|
||||
:domain => 'gmail.com',
|
||||
:user_name => 'daniel.buch@gmail.com',
|
||||
:password => STDIN.readline
|
||||
}
|
||||
|
||||
SimpleMailer.simple_message('daniel.buch+rubytest@gmail.com').deliver
|
Reference in New Issue
Block a user