Class: Confluence::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/confluence/client.rb

Overview

A useful helper for running Confluence XML-RPC from Ruby. Takes care of adding the token to each method call (so you can call server.getSpaces() instead of server.getSpaces(token)).

Usage:

client = Confluence::Client.new(:url => "confluence.atlassian.com") client.login("user", "password") p client.getSpaces

Constant Summary

PREFIX =
"confluence1"
XMLRPC_SUFFIX =
"/rpc/xmlrpc"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments = {}) ⇒ Client

Initializes a new client with the given arguments.

Parameters

arguments<Hash>

Described below.

Arguments

:url - The url of the Confluence instance. The trailing '/rpc/xmlrpc' path is optional. :token - An existing session token to reuse.



32
33
34
35
36
37
38
# File 'lib/confluence/client.rb', line 32

def initialize(arguments = {})
  @url = arguments[:url]
	@token = arguments[:token]
	
    Log4r::MDC.put('token', @token || 'nil')
	log.info "initialized client (:url => #{@url}, :token => #{@token || 'nil'})"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Translates every call into XMLRPC calls.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/confluence/client.rb', line 79

def method_missing(method_name, *args)
  handle_fault do
    if args.empty?
		    log.debug "#{method_name}"
    else
      log.debug "#{method_name}(#{(args.collect {|a| a.inspect}).join(', ')})"
     end
     
     begin
 		    result = proxy.send(method_name, *([@token] + args))
 		    log.debug(result.inspect)
 		    result
 		  rescue EOFError => e
 		    log.warn "Could not complete XMLRPC call, retrying... Error: #{e.inspect}"
 		    retry
	    end
   end
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token



21
22
23
# File 'lib/confluence/client.rb', line 21

def token
  @token
end

#urlObject (readonly)

Returns the value of attribute url



21
22
23
# File 'lib/confluence/client.rb', line 21

def url
  @url
end

#usernameObject (readonly)

Returns the value of attribute username



21
22
23
# File 'lib/confluence/client.rb', line 21

def username
  @username
end

Instance Method Details

#has_token?Boolean

Returns true, if the client has a session token.



42
43
44
# File 'lib/confluence/client.rb', line 42

def has_token?
 !@token.nil?
end

#login(username, password) ⇒ Object

Logs in and returns the newly acquired session token.

Parameters

username<String>

The username.

password<String>

The password.



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/confluence/client.rb', line 52

def (username, password)
  handle_fault do
    if @token = proxy.(username, password)
  			Log4r::MDC.put('token', @token)
  			log.info "logged in as '#{username}' and acquired token."

		    @username = username
  			@password = password
      end  			
   end
   
   @token
end

#logoutObject

Logs out and invalidates the session token.



68
69
70
71
72
73
74
75
# File 'lib/confluence/client.rb', line 68

def logout
  handle_fault do
     @token = nil if @token and result = proxy.logout(@token)
     log.info "logged out"
     Log4r::MDC.put('token', 'nil')
     result
   end
end