Module: Kernel

Defined in:
lib/goodies/lwr-simple.rb

Instance Method Summary collapse

Instance Method Details

#get(uri) ⇒ Object

get will fetch the document identified by the given URL and return it. The url argument can be either a simple string or a URI object.

You will not be able to examine the response code or response headers like (‘Content-Type’) wgen you are accessing the web using this function. If you need that information you should use the full OO interface of Net::HTTP.



39
40
41
42
43
44
45
46
47
# File 'lib/goodies/lwr-simple.rb', line 39

def get(uri)
	url = LWR::Simple.normalize(uri)

	begin
		return open(url).read
	rescue
		nil
	end
end

#getprint(uri, limit = 10) ⇒ Object Also known as: getputs

Get and print a document identified by URL. If the request fails, then the status code and message are printed on $stderr. The return value is the HTTPResponse object. See also getputs.

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/goodies/lwr-simple.rb', line 79

def getprint(uri, limit = 10)
	raise ArgumentError, 'HTTP redirect too deep' if limit == 0
	
	url = LWR::Simple.normalize(uri)

	begin
		res = Net::HTTP.get_response(url)
	rescue => ex
		$stderr.puts "#{ex.class}: #{ex.message}"
	else
		case res
			when Net::HTTPSuccess
				puts res.body
			when Net::HTTPRedirection
				getprint(res["location"], limit - 1)
			else
				$stderr.puts "#{res.code} #{res.message}"
		end
	ensure
		return res
	end
end

#getstore(uri, file) ⇒ Object

Gets a document identified by a URL and stores it in the file. The URL can either be a string or a URI object. File can be either a string or a File object. If a problem ocurrs while writing to a file, nil is returned. If it succeeds, the return value is the HTTPResponse object.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/goodies/lwr-simple.rb', line 106

def getstore(uri, file)
	res = nil

	file = file.path if file.is_a? File

	begin
		File.open(file, "w") do |f|
			res = f.getprint(uri) # this is why Ruby is so sweet
		end
	rescue
		return nil
	end

	res
end

#head(uri) ⇒ Object

Get document headers. Returns the following 5 values if successful:

  • content_type

  • document_length

  • modified_time

  • expires

  • server

Returns an empty array if it fails.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/goodies/lwr-simple.rb', line 57

def head(uri)
	url = LWR::Simple.normalize(uri)

	head = []

	begin
		Net::HTTP::start(url.host, url.port) do |c|
			res = c.request_head(url.path)
			head << res["content-type"]
			head << res["content-length"]
			head << res["last-modified"]
			head << res["expires"]
			head << res["server"]
		end
	ensure
		return head
	end
end

#mirror(uri, file) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/goodies/lwr-simple.rb', line 122

def mirror(uri, file)
	url = LWR::Simple.normalize(uri)
	req = Net::HTTP::Get.new(url.path)

	case file
		when String
			if File.exists? file
				file_exists = true
			else
				file_exists = false
			end
			file = File.open(file, "w")
		when File
			if File.exists? file.path
				file_exists = true
			else
				file_exists = false
			end
		else
			raise ArgumentError, "Failed arguments"
	end

	if file_exists
		req.add_field("If-Modified-Since", file.mtime.httpdate)
	end

	res = Net::HTTP.new(url.host, url.port).start do |http|
		http.request(req)
	end

	case res
		when Net::HTTPSuccess
			tmpfile = Tempfile.new("lwr")
			tmpfile.print res.body
			
			if res["content-length"] and tmpfile.size < res["content-length"].to_i
				raise Exception, "Transfer truncated, only #{tmpfile.size} of #{res["content-length"]} bytes received"
				File.unlink(tmpfile.path)
			elsif res["content-length"] and tmpfile.size > res["content-length"].to_i
				raise Exception, "Content-length mismatch, expected #{res["content-length"]} bytes, got #{tmpfile.size}"
				File.unlink(tmpfile.path)
			else # OK
				File.unlink(file.path) if File.exists? file.path
				File.rename(tmpfile.path, file.path)

			end

		else
	end

	res

end