require'confluence-rest-api'rest_server='https://myserver.com'user_name='username'password='password'space_key='space'################################################################### A connection to the client must be done before any page requests##################################################################client=ConfluenceClient.new(rest_server,user_name,password)################################## Query an existing page by title#################################page=PageObject.new('page_title',space_key)ifpage.id.nil?puts'*** WARNING: Unable to open page: page_title'elseputs"Body: #{page.rendered_body}"puts"Fully styled #{page.styled_view}"puts"ID: #{page.id}"puts"Status: #{page.status}"puts"Version: #{page.version}"puts"Created By: #{page.created_by}"puts"Date Created: #{page.created}"puts"Date Updated: #{page.last_update}"puts"Page URL: #{page.url}"end################################ Query an existing page by id###############################page=PageObject.new(123456789,space_key)ifpage.title.nil?puts'*** WARNING: Unable to open page with id: 123456789'elseputs"Body: #{page.rendered_body}"puts"Fully styled #{page.styled_view}"puts"ID: #{page.id}"puts"Status: #{page.status}"puts"Version: #{page.version}"puts"Created By: #{page.created_by}"puts"Date Created: #{page.created}"puts"Date Updated: #{page.last_update}"puts"Page URL: #{page.url}"end############################################################ Create a new page with a page titled "Home" as its parent###########################################################home_page=PageObject.new('Home',space_key)unlesshome_page.id.nil?client.create_page_with_parent('My Page Title',space_key,'My Page Body Content',home_page.id)end############################## Add an attachment to a page#############################page_obj=PageObject.new('My Page Title',space_key)unlesspage_obj.id.nil?img_base_name='my/image/location'image='my_image.png'ifpage_obj.attach_binary_file(image,img_base_name).nil?puts"*** WARNING: Image attachment #{image} for #{title} was not successful."elseputs"Image attachment #{image} for #{page_obj.title} was successful."endend################################### Remove an attachment from a page##################################page_obj=PageObject.new('My Page Title',space_key)id=page_obj.attachment_id('my_image.png')ifid.nil?puts"Attachment doesn't exist."elsifpage_obj.delete_attachment(id).nil?puts"*** WARNING: Attachment with ID #{id} was not deleted."elseputs"Attachment with ID #{id} was deleted"end################################################### Get an array listing of the page attachment data##################################################page_obj=PageObject.new('My Page Title',space_key)att_array=get_all_attachments(page_obj.id)att_array.eachdo |line|
entry_hash=line.to_hashtitle=entry_hash["title"]uri=entry_hash["_links"]["download"]puts"Attachment title: #{title}"puts"Attachment link : #{uri}"end####################################################### Save all attachments of a page to the given location######################################################page_obj=PageObject.new('My Page Title',space_key)page_obj.save_file_attachments(page_obj.id,'./')################################################# Get all labels associated with a given page ID################################################page_obj=PageObject.new('My Page Title',space_key)page_labels=page_obj.labels(page_obj.id)puts"Page Labels for #{page_obj.title}: #{page_labels}"########################## Adding labels to a page#########################page_obj=PageObject.new('My Page Title',space_key)new_labels=['my_label1','my_label2']page_obj.add_labels(new_labels)############################## Deleting labels from a page#############################page_obj=PageObject.new('My Page Title',space_key)del_labels=['my_label1','my_label2']page_obj.delete_labels(del_labels)################ Delete a page###############page_obj=PageObject.new('My Page Title',space_key)ifpage_obj.delete_page(page_obj.id).nil?puts"*** WARNING: Page with ID #{page_obj.id} was not deleted."elseputs"Page with ID #{page_obj.id} was deleted."end