Class: OpenfireAdmin::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/openfire_admin/http_client.rb

Overview

simple http client ( cookie support )

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ HttpClient

Returns a new instance of HttpClient.

Parameters:

  • url (URI)

    admin console uri. HttpClient use its host , port and scheme



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

Parameters:

  • request (String)

    path

Yields:

  • (Net::HTTPResponse)


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

Parameters:

  • request (String)

    path

  • form (Hash<String,String>)

    data

Yields:

  • (Net::HTTPResponse)


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

Parameters:

  • request (Net::HTTPRequest)

Yields:

  • (Net::HTTPResponse)


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)
    cookies = res.get_fields('Set-Cookie')
    cookies.each{|str|
      k,v = str[0...str.index(';')].split('=')
      @cookies[k] = v
    } if cookies
    if @verbos
      puts "#{res.code} #{res.message}"
      res.each{|k,v| puts "#{k}=#{v}" }
    end
    def res.request
      @request
    end
    res.instance_variable_set(:@request, req)
    yield res
  end
end