Class: OpenfireAdmin::HttpClient
- Inherits:
-
Object
- Object
- OpenfireAdmin::HttpClient
- Defined in:
- lib/openfire_admin/http_client.rb
Overview
simple http client ( cookie support )
Instance Method Summary collapse
-
#get(path) {|Net::HTTPResponse| ... } ⇒ Object
get path.
-
#initialize(url) ⇒ HttpClient
constructor
A new instance of HttpClient.
-
#post(path, form_data) {|Net::HTTPResponse| ... } ⇒ Object
post with form data.
- #request(req) {|Net::HTTPResponse| ... } ⇒ Object
Constructor Details
#initialize(url) ⇒ HttpClient
Returns a new instance of HttpClient.
6 7 8 9 10 |
# File 'lib/openfire_admin/http_client.rb', line 6 def initialize(url) @cookies = {} @url = url requrie 'net/https' if @url.scheme == 'https' end |
Instance Method Details
#get(path) {|Net::HTTPResponse| ... } ⇒ Object
get path
51 52 53 |
# File 'lib/openfire_admin/http_client.rb', line 51 def get(path) request(Net::HTTP::Get.new(path)){|res| yield res } end |
#post(path, form_data) {|Net::HTTPResponse| ... } ⇒ Object
post with form data
42 43 44 45 46 |
# File 'lib/openfire_admin/http_client.rb', line 42 def post(path, form_data) req = Net::HTTP::Post.new(path) req.set_form_data(form_data) request(req){|res| yield res } end |
#request(req) {|Net::HTTPResponse| ... } ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/openfire_admin/http_client.rb', line 14 def request(req) Net::HTTP.start(@url.host, @url.port) do |http| http.use_ssl = true if @url.scheme == 'https' puts "#{req.method} #{req.path}" if @verbos req['Host'] = @url.host req['Cookie'] = @cookies.map{|k,v| "#{k}=#{v}"}.join(";") unless @cookies.empty? res = http.request(req) = res.get_fields('Set-Cookie') .each{|str| k,v = str[0...str.index(';')].split('=') @cookies[k] = v } if if @verbos puts "#{res.code} #{res.}" res.each{|k,v| puts "#{k}=#{v}" } end def res.request @request end res.instance_variable_set(:@request, req) yield res end end |