A “handle” to access Gliffy on a per-user-session basis Since most calls to gliffy require a user-token, this class encapsulates that token and the calls made under it.

Methods
A
D
F
N
U
Constants
ROOT_FOLDER = 'ROOT'
Attributes
[R] logger

Get access to the logger (useful for controlling log messages)

[R] request

Get access to the Request (useful for hacking or testing)

[R] token

Get access to the current token (useful for caching to disk)

[W] error_callback

Use this to override what happens when an error from Gliffy is received. The default (or nil) is to raise the exception that was generated. If you want to override this provide a block that takes the response that was received (which will be a hash-like object from HTTParty, possibly nil) and the exception that was generated (the message of which will have been parsed from Gliffy’s XML if that was possible):

    # If we got an HTTParty response, try to print out the body and the Gliffy message
    # Otherwise, barf
    handle.response = Proc.new do |response,exception|
      if response && response.respond_to? :body
        puts exception.to_str
        puts response.body
      else
        raise exception
      end
    end
Class Public methods
new(api_root,edit_api_root,credentials,http=nil,logger=nil)

Create a new handle to Gliffy

api_root
root URL (without the protocol) of where to connect to Gliffy
edit_api_root
root URL (without the protocol) of where to connect to Gliffy for the edit link only
credentials
a Credentials object to use for access
http
override of http access class (use at your own risk; must be substituable for HTTPart)
logger
logger instance, if you don’t want the default
# File lib/gliffy/handle.rb, line 46
    def initialize(api_root,edit_api_root,credentials,http=nil,logger=nil)
      @credentials = credentials
      @request = Request.new(api_root,credentials)
      @edit_link_request = Request.new(edit_api_root,credentials)
      @request.http = http if !http.nil?
      @logger = logger || Logger.new(STDOUT)
      @logger.level = Logger::INFO
      @request.logger = @logger
      if !@credentials.has_access_token?
        update_token
      end
      @error_callback = nil
    end
Instance Public methods
account_admins()

Get admins of your account. Returns users

# File lib/gliffy/handle.rb, line 81
    def account_admins
      make_request(:get,"#{account_url}/admins.xml")
    end
account_documents(show=nil)

Returns all documents in the account

show
if nil, all documents are returned; if :public only public are returned.
# File lib/gliffy/handle.rb, line 87
    def account_documents(show=nil)
      if show.nil?
        make_request(:get,"#{account_url}/documents.xml")
      else
        make_request(:get,"#{account_url}/documents.xml",:public => show == :public)
      end
    end
account_folders()

Returns all folders in the account

# File lib/gliffy/handle.rb, line 96
    def account_folders
      make_request(:get,"#{account_url}/folders.xml")
    end
account_get(show_users=true)

Returns account meta data

show_users
if true, include the list of users in this account
# File lib/gliffy/handle.rb, line 102
    def account_get(show_users=true)
      make_request(:get,"#{account_url}.xml", :showUsers => show_users)[0]
    end
account_users()

Get users in your account

# File lib/gliffy/handle.rb, line 107
    def account_users 
      make_request(:get,"#{account_url}/users.xml")
    end
anything(method,url,params={},parse=false,link=false)
# File lib/gliffy/handle.rb, line 291
    def anything(method,url,params={},parse=false,link=false)
      make_request(method,url,params,parse,link)
    end
delete_token()

Delete the current token

# File lib/gliffy/handle.rb, line 75
    def delete_token
      make_request(:delete,token_url)
      @credentials.clear_access_token
    end
document_create(name,folder_path=nil,template_id=nil,type=:diagram)

Create a new document

name
Name of the new document
folder_path
Path in which to place the document initially (nil to use default)
template_id
document id of a document to copy when initializing this new document (nil to make a blank one)
type
If Gliffy ever supports other document types, use this

Returns a document representing the document that was created.

# File lib/gliffy/handle.rb, line 118
    def document_create(name,folder_path=nil,template_id=nil,type=:diagram)
      params = { 
        :documentName => name,
        :documentType => type
      }
      params[:templateDiagramId] = template_id if !template_id.nil? 
      params[:folderPath] = normalize_folder_path(folder_path) if !folder_path.nil? 
      documents = make_request(:create,"#{account_url}/documents.xml",params)
      return nil if documents.nil? 
      documents[0]
    end
document_delete(document_id)

Delete an existing document

# File lib/gliffy/handle.rb, line 131
    def document_delete(document_id)
      make_request(:delete,document_url(document_id))
    end
document_edit_link(document_id,return_url,return_text='Return')

Get the link to edit a document

# File lib/gliffy/handle.rb, line 189
    def document_edit_link(document_id,return_url,return_text='Return')
      update_token
      @edit_link_request.link_for(:GET,'',{ 
        :launchDiagramId => document_id, 
        :returnURL => return_url, 
        :returnButtonText=> return_text})
    end
document_get(document_id,type=:jpeg,size=:L,version=nil)

Get a document; returning the actual bytes

document_id
identifier of the document
type
document type. Types known to work:
:jpeg
- JPEG
:png
- PNG
:svg
- SVG (for Visio import)
:xml
- Gliffy proprietary XML format (for archiving)
size
size to show, from biggest to smallest: :L, :M, :S, :T
version
The version to get, or nil to get the most recent
# File lib/gliffy/handle.rb, line 151
    def document_get(document_id,type=:jpeg,size=:L,version=nil)
      params = { :size => size }
      params[:version] = version if !version.nil?
      response = make_request(:get,document_url(document_id,type),params,false)
      if (type == :xml) || (type == :svg)
        response.body
      else
        response
      end
    end
document_get_metadata(document_id,show_revisions=false)

Get meta-data about a document.

document_id
identifier of the document
show_revisions
if true, include info about the documents’ revision history
# File lib/gliffy/handle.rb, line 138
    def document_get_metadata(document_id,show_revisions=false)
      make_request(:get,document_metadata_url(document_id),:showRevisions => show_revisions)[0]
    end
document_get_public_url(document_id,size=:L)

Gets the public link to the document, which is static and not dependent on the key or authorization. This isn’t formally part of the Gliffy API, but is convienient. The document must be public and you can only get it in JPG format.

document_id
identifier of the document
size
size to show, from biggest to smallest: :L, :M, :S, :T
# File lib/gliffy/handle.rb, line 169
    def document_get_public_url(document_id,size=:L)
      "http://www.gliffy.com/pubdoc/#{document_id}/#{size.to_s}.jpg"
    end
document_get_url(document_id,type=:jpg,size=:L,version=nil)

Get a link to a document

document_id
identifier of the document
type
document type. Types known to work:
:jpg
- JPEG
:png
- PNG
:svg
- SVG (for Visio import)
:xml
- Gliffy proprietary XML format (for archiving)
size
size to show, from biggest to smallest: :L, :M, :S, :T
version
The version to get, or nil to get the most recent
# File lib/gliffy/handle.rb, line 182
    def document_get_url(document_id,type=:jpg,size=:L,version=nil)
      params = { :size => size }
      params[:version] = version if !version.nil?
      make_request(:get,document_url(document_id,type),params,false,true)
    end
document_move(document_id,new_path)

Move a document to a different folder

# File lib/gliffy/handle.rb, line 198
    def document_move(document_id,new_path)
      make_request(:move,folders_url(new_path) + "/documents/#{document_id}.xml")
    end
document_update(document_id,options)

Update a document’s meta-data or content

document_id
identifier of document to update
options
data to update; omission of an option will not change it
:name
change the name
:public
if false, will remove public statues, if true, will make public
:content
if present, should be the gliffy XML content to update (don’t use this unless it’s crucial)
# File lib/gliffy/handle.rb, line 208
    def document_update(document_id,options)
      if (options[:content])
        make_request(:update,document_url(document_id),{:content => options[:content]})
      end
      if (options[:name] || options[:public])
        params = {}
        params[:documentName] = options[:name] if options[:name]
        params[:public] = options[:public] if options[:public]
        make_request(:update,document_metadata_url(document_id),params)
      end
    end
folder_add_user(path,username)

Add a user to a folder

# File lib/gliffy/handle.rb, line 221
    def folder_add_user(path,username)
      make_request(:update,folder_users_url(path,username),{:read => true, :write => true})
    end
folder_create(path)

Create a new folder

path
the path to the folder, each path separated by a forward slash. If this starts with a forward slash it will attempt to make folder with the exact given path. This will probably fail. If this DOESN’T start with a slash, this will make the folder inside ROOT, which is what you want. So, don’t start this with a slash.
# File lib/gliffy/handle.rb, line 230
    def folder_create(path)
      make_request(:create,"#{folders_url(path)}.xml")
    end
folder_delete(path)

Delete a folder

path
the path to the folder. See folder_create.
# File lib/gliffy/handle.rb, line 236
    def folder_delete(path)
      make_request(:delete,"#{folders_url(path)}.xml")
    end
folder_documents(path)

Get the documents in a folder

path
the path to the folder whose documents to get
# File lib/gliffy/handle.rb, line 242
    def folder_documents(path)
      make_request(:get,"#{folders_url(path)}/documents.xml")
    end
folder_remove_user(path,username)

Remove a user from access to the folder

# File lib/gliffy/handle.rb, line 253
    def folder_remove_user(path,username)
      make_request(:update,folder_users_url(path,username),{:read => false, :write => false})
    end
folder_users(path)

Get users with access to the folder

path
the path to the folder whose users to get
# File lib/gliffy/handle.rb, line 248
    def folder_users(path)
      make_request(:get,"#{folders_url(path)}/users.xml")
    end
update_token(force=false)

Updates the token being used if there isn’t one in the credentials, or by forcing

force
always attempt to update the token
# File lib/gliffy/handle.rb, line 63
    def update_token(force=false)
      if !@credentials.has_access_token? || force
        @logger.debug("Requesting new token " + (force ? " by force " : " since we have none "))
        response = @request.create(token_url,
                                   :description => @credentials.description,
                                   :protocol_override => :https)
        @token = Response.from_http_response(response)
        @credentials.update_access_token(@token)
      end
    end
user_create(username)

Create a new user

username
the name to give this user
# File lib/gliffy/handle.rb, line 259
    def user_create(username)
      make_request(:create,"#{account_url}/users.xml",{ :userName => username })
    end
user_delete(username)

Delete an existing user

# File lib/gliffy/handle.rb, line 264
    def user_delete(username)
      make_request(:delete,"#{user_url(username)}.xml")
    end
user_documents(username='$username')

Get the documents a user has access to (this is potentially expensive, as it results in multiple calls to gliffy)

username
if provided, get documents for the given username, otherwise get them for the logged-in user
# File lib/gliffy/handle.rb, line 271
    def user_documents(username='$username')
      return user_documents_helper(username,user_folders(username))
    end
user_folders(username='$username')

Get the folders a user has access to

username
if provided, get folders for the given username, otherwise get them for the logged-in user
# File lib/gliffy/handle.rb, line 277
    def user_folders(username='$username')
      make_request(:get,"#{user_url(username)}/folders.xml")
    end
user_update(username,options)

Update a user’s meta-data

username
the username to operate on
options
the options for updating their info. Any omitted option will not change that value on the server.
:email
sets their email address
:password
sets their password for logging into gliffy.com
:admin
if true, sets them to be an admin; if false, revokes their admin privs
# File lib/gliffy/handle.rb, line 287
    def user_update(username,options)
      make_request(:update,user_url(username),options)
    end