Class: CookieHTTPClient
- Inherits:
-
Object
- Object
- CookieHTTPClient
- Defined in:
- lib/cookie_http_client.rb,
lib/cookie_http_client/version.rb
Defined Under Namespace
Constant Summary collapse
- THIS =
CookieHTTPClient- VERSION =
"0.0.3"nil
Instance Attribute Summary collapse
-
#last_uri ⇒ Object
readonly
Returns the value of attribute last_uri.
-
#max_redirect_count ⇒ Object
Returns the value of attribute max_redirect_count.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Class Method Summary collapse
Instance Method Summary collapse
-
#get(header = {}) ⇒ Object
Net::HTTPResponse.
-
#initialize(uri, proxy = Net::HTTP) ⇒ CookieHTTPClient
constructor
A new instance of CookieHTTPClient.
-
#post(data, header = {}, &block) ⇒ Object
Net::HTTPResponse.
-
#post_form(params, header = {}, &block) ⇒ Object
Net::HTTPResponse.
Constructor Details
#initialize(uri, proxy = Net::HTTP) ⇒ CookieHTTPClient
Returns a new instance of CookieHTTPClient.
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/cookie_http_client.rb', line 63 def initialize(uri, proxy=Net::HTTP) = CookieJar.new unless 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 |
Instance Attribute Details
#last_uri ⇒ Object (readonly)
Returns the value of attribute last_uri.
60 61 62 |
# File 'lib/cookie_http_client.rb', line 60 def last_uri @last_uri end |
#max_redirect_count ⇒ Object
Returns the value of attribute max_redirect_count.
61 62 63 |
# File 'lib/cookie_http_client.rb', line 61 def max_redirect_count @max_redirect_count end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
60 61 62 |
# File 'lib/cookie_http_client.rb', line 60 def uri @uri end |
Class Method Details
.clear_cookie ⇒ Object
38 39 40 41 |
# File 'lib/cookie_http_client.rb', line 38 def self. = CookieJar.new unless .clear end |
.cookie_jar ⇒ Object
35 36 37 |
# File 'lib/cookie_http_client.rb', line 35 def self. end |
Instance Method Details
#get(header = {}) ⇒ Object
Returns Net::HTTPResponse.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# 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 = {}, &block) ⇒ Object
Returns Net::HTTPResponse.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# 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) ⇒ Object
Returns Net::HTTPResponse.
119 120 121 122 |
# 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 |