Class: CookieHTTPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/cookie_http_client.rb,
lib/cookie_http_client/version.rb

Defined Under Namespace

Classes: CookieJar, HTTPFound

Constant Summary collapse

THIS =
CookieHTTPClient
VERSION =
"0.0.3"
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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)
  @@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

Instance Attribute Details

#last_uriObject (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_countObject

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

#uriObject (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



38
39
40
41
# File 'lib/cookie_http_client.rb', line 38

def self.clear_cookie
  @@cookie_jar = CookieJar.new unless @@cookie_jar
  @@cookie_jar.clear
end


35
36
37
# File 'lib/cookie_http_client.rb', line 35

def self.cookie_jar
  @@cookie_jar
end

Instance Method Details

#get(header = {}) ⇒ Object

Returns Net::HTTPResponse.

Parameters:

  • header:Hash

    request headers

  • block

    callback when redirect with ‘uri`

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.

Parameters:

  • data

    post data

  • header:Hash

    request headers

  • block

    callback when redirect with ‘uri`

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.

Parameters:

  • params:Hash

    post data

  • header:Hash

    request headers

  • block

    callback when redirect with ‘uri`

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