Class: WeChat::Bot::HTTP::Session
- Inherits:
-
Object
- Object
- WeChat::Bot::HTTP::Session
- Defined in:
- lib/wechat/bot/http/session.rb
Overview
可保存 Cookies 的 HTTP 请求类
简单实现 Python 版本 requests.Session()
Instance Attribute Summary collapse
- #cookies ⇒ HTTP::CookieJar readonly
Instance Method Summary collapse
- #cookie_of(name) ⇒ Object
- #delete(url, options = {}) ⇒ HTTP::Response
- #get(url, options = {}) ⇒ HTTP::Response
-
#initialize(bot) ⇒ Session
constructor
A new instance of Session.
- #post(url, options = {}) ⇒ HTTP::Response
- #put(url, options = {}) ⇒ HTTP::Response
- #request(verb, url, options = {}) ⇒ HTTP::Response
Constructor Details
#initialize(bot) ⇒ Session
Returns a new instance of Session.
12 13 14 15 16 |
# File 'lib/wechat/bot/http/session.rb', line 12 def initialize(bot) @bot = bot (@bot.config.) end |
Instance Attribute Details
#cookies ⇒ HTTP::CookieJar (readonly)
10 11 12 |
# File 'lib/wechat/bot/http/session.rb', line 10 def @cookies end |
Instance Method Details
#cookie_of(name) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/wechat/bot/http/session.rb', line 57 def (name) ..each do || return .value if name == .name end nil end |
#delete(url, options = {}) ⇒ HTTP::Response
34 35 36 |
# File 'lib/wechat/bot/http/session.rb', line 34 def delete(url, = {}) request(:delete, url, ) end |
#get(url, options = {}) ⇒ HTTP::Response
19 20 21 |
# File 'lib/wechat/bot/http/session.rb', line 19 def get(url, = {}) request(:get, url, ) end |
#post(url, options = {}) ⇒ HTTP::Response
24 25 26 |
# File 'lib/wechat/bot/http/session.rb', line 24 def post(url, = {}) request(:post, url, ) end |
#put(url, options = {}) ⇒ HTTP::Response
29 30 31 |
# File 'lib/wechat/bot/http/session.rb', line 29 def put(url, = {}) request(:put, url, ) end |
#request(verb, url, options = {}) ⇒ HTTP::Response
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/wechat/bot/http/session.rb', line 39 def request(verb, url, = {}) prepare_request(url) if [:timeout] connect_timeout, read_timeout = .delete(:timeout) @client = @client.timeout(connect: connect_timeout, read: read_timeout) end response = @client.request(verb, url, ) (response.) @bot.logger.verbose "[#{verb.upcase}] #{url}" @bot.logger.verbose "Options: #{}" @bot.logger.verbose "Response: #{response.body}" response end |