Class: X2CH::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/x2ch.rb

Class Method Summary collapse

Class Method Details

.download(url, if_modified_since = nil, range = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/x2ch.rb', line 114

def self.download(url, if_modified_since = nil, range = nil)
	header = {"User-Agent" => "Monazilla/1.00 (x2ch/0.9.1)", "Accept-Encoding" => 'gzip'}
	if if_modified_since
		header["If-Modified-Since"] = if_modified_since
	end
	if range
		header["Range"] = range
	end
	begin
		res = open(url, header){|f|
			body = nil
			if f.content_encoding.index('gzip')
				body = Zlib::GzipReader.new(f).read.toutf8
			else
				body = f.read.toutf8
			end
			[body, f.status, f.last_modified, f.content_encoding, body.size]
		}
	rescue OpenURI::HTTPError => e
		raise DownloadError.new(e.message)
	end
	StringResponse.new(res[0], res[1], res[2], res[3], res[4])
end