lib/curl_agent.rb in curb-openuri-0.1.0 vs lib/curl_agent.rb in curb-openuri-0.2.0
- old
+ new
@@ -74,11 +74,13 @@
end
agent = CurlAgent.new(name, options)
agent.perform!
- io = StringIO.new(agent.body_str)
+ io = IO.new(agent.body_str, agent.header_str)
+ io.base_uri = URI.parse(agent.last_effective_url) rescue nil
+ io.status = [agent.response_code, '']
if block
block.call(io)
else
io
end
@@ -91,6 +93,34 @@
perm = rest.shift
end
end
return mode, perm, rest
end
-end
+
+ class IO < StringIO
+ # returns an Array which consists status code and message.
+ attr_accessor :status
+
+ # returns a URI which is base of relative URIs in the data.
+ # It may differ from the URI supplied by a user because redirection.
+ attr_accessor :base_uri
+
+ def initialize(body_str, header_str)
+ super(body_str)
+ @header_str = header_str
+ end
+
+ # returns a Hash which represents header fields.
+ # The Hash keys are downcased for canonicalization.
+ def meta
+ @meta ||= begin
+ arr = @header_str.split(/\r?\n/)
+ arr.shift
+ arr.inject({}) do |hash, hdr|
+ key, val = hdr.split(/:\s+/, 2)
+ hash[key.downcase] = val
+ hash
+ end
+ end
+ end
+ end
+end
\ No newline at end of file