class THIS
Constants
- THIS
- VERSION
Attributes
last_uri[R]
max_redirect_count[RW]
uri[R]
Public Class Methods
new(uri, proxy=Net::HTTP)
click to toggle source
# File lib/cookie_http_client.rb, line 63 def initialize(uri, proxy=Net::HTTP) @@cookie_jar = CookieJar.new unless @@cookie_jar if uri.respond_to?(:path) @uri = uri else @uri = URI.parse(uri) end @uri.path = '/' if @uri.path.empty? @last_uri = nil @proxy = proxy @max_redirect_count = 10 end
Public Instance Methods
get(header={}) { |last_uri| ... }
click to toggle source
@param header:Hash request headers @param block callback when redirect with ‘uri` @return Net::HTTPResponse
# File lib/cookie_http_client.rb, line 79 def get(header={}) @last_uri = @uri.clone count = @max_redirect_count begin request(@last_uri, header){|http, path, header2| http.get(path, header2) } rescue HTTPFound =>e count-=1 raise e if count==0 @last_uri = e.redirect_uri @last_uri = yield(@last_uri) if block_given? retry end end
post(data, header={}) { |last_uri| ... }
click to toggle source
@param data post data @param header:Hash request headers @param block callback when redirect with ‘uri` @return Net::HTTPResponse
# File lib/cookie_http_client.rb, line 99 def post(data, header={}, &block) begin @last_uri = @uri request(@uri, header){|http, path, header2| http.post(path, data, header2) } rescue HTTPFound =>e @last_uri = e.redirect_uri @last_uri = yield(@last_uri) if block_given? n = THIS.new(@last_uri) r = n.get({}, &block) @last_uri = n.last_uri r end end
post_form(params, header={}, &block)
click to toggle source
@param params:Hash post data @param header:Hash request headers @param block callback when redirect with ‘uri` @return Net::HTTPResponse
# File lib/cookie_http_client.rb, line 119 def post_form(params, header={}, &block) data = params.map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join('&') post(data, header, &block) end
Protected Instance Methods
request(uri, header={}) { |http, path, h| ... }
click to toggle source
# File lib/cookie_http_client.rb, line 44 def request(uri, header={}) raise StandardError, "block required." unless block_given? path = [uri.path, uri.query].compact.join('?') http = @proxy.new(uri.host, uri.port) http.use_ssl = uri.scheme=='https' c = @@cookie_jar.cookie_string(uri) h = header.clone h["Cookie"] = c unless c.empty? r = yield(http, path, h) @@cookie_jar.set_cookie(r['set-cookie'], uri) raise HTTPFound.new(r, uri) if r.is_a? Net::HTTPFound or r.is_a? Net::HTTPMovedPermanently r end