class PageObject
Attributes
created[R]
created_by[R]
id[R]
last_updated[R]
status[R]
title[R]
version[R]
Public Class Methods
new(title_or_id, spacekey)
click to toggle source
# File lib/page.rb, line 8 def initialize(title_or_id, spacekey) if title_or_id.is_a? Integer @title, @id, @version, @status, @created, @created_by, @last_updated, @url = get_page_info_by_id(title_or_id.to_s, spacekey) else @title, @id, @version, @status, @created, @created_by, @last_updated, @url = get_page_info_by_title(title_or_id, spacekey) end end
Public Instance Methods
add_labels(labels)
click to toggle source
# File lib/page.rb, line 96 def add_labels(labels) payload = String.new('[') json_label = '{ "prefix": "global", "name": "__LABEL__" }'.freeze labels.each_with_index do |l, idx| jl = json_label.sub(/__LABEL__/, l) if idx == 0 payload += jl else payload += ',' + jl end end payload = payload + ']' # puts "Add label payload: #{payload}" begin res = RestClient.post "#{@@conf_url}/#{@@urn}/#{@id}/label?os_username=#{@@login}&os_password=#{@@pwd}", payload, :content_type => 'application/json' rescue RestClient::ExceptionWithResponse => e puts Nokogiri.XML(e.response) end if res.nil? puts "*** WARNING: Label update failed for page with ID: #{@id}" nil else # puts JSON.parse(res) true end end
attach_binary_file(file_name, file_basename)
click to toggle source
# File lib/page.rb, line 167 def attach_binary_file(file_name, file_basename) if File.exist?("#{file_basename}/#{file_name}") payload = { multipart: true, file: File.new("#{file_basename}/#{file_name}", 'rb'), comment: 'Automated Ruby import', minorEdit: true } url_mod = "#{@@conf_url}/#{@@urn}/#{@id}/child/attachment?os_username=#{@@login}&os_password=#{@@pwd}" begin RestClient.post(url_mod, payload, {"X-Atlassian-Token" => "nocheck"}) true rescue RestClient::ExceptionWithResponse => e puts Nokogiri.XML(e.response) nil end else puts "*** WARNING: File can't be found for #{file_basename}/#{file_name}" nil end end
attachment_id(attachment_name)
click to toggle source
# File lib/page.rb, line 202 def attachment_id(attachment_name) fname = attachment_name.dup fname = CGI.escape(fname) begin response = RestClient.get "#{@@conf_url}/#{@@urn}/#{@id}/child/attachment", {params: { :filename => fname, 'os_username' => @@login, 'os_password' => @@pwd }} response = JSON.parse(response) if response['results'].any? return response['results'][0]['id'] end rescue RestClient::ExceptionWithResponse => e puts Nokogiri.XML(e.response) end nil end
delete_attachment(attach_id)
click to toggle source
# File lib/page.rb, line 190 def delete_attachment(attach_id) begin RestClient.delete "#{@@conf_url}/#{@@urn}/#{attach_id}", {params: { :os_username => @@login, :os_password => @@pwd }} rescue RestClient::ExceptionWithResponse => e puts Nokogiri.XML(e.response) return nil end true end
delete_labels(labels)
click to toggle source
Note that we must use query parameters here because deleting labels with a “/” will fail otherwise.
# File lib/page.rb, line 129 def delete_labels(labels) labels.each do |l| puts "Deleting label: #{l}" begin uri = "#{@@conf_url}/#{@@urn}/#{@id}/label?name=#{l}&os_username=#{@@login}&os_password=#{@@pwd}" uri = Addressable::URI.parse(uri).normalize.to_s res = RestClient.delete uri rescue RestClient::ExceptionWithResponse => e puts Nokogiri.XML(e.response) end if res.nil? puts "*** WARNING: Label removal failed for page with ID: #{@id}" return nil end end true end
delete_page(page_id)
click to toggle source
# File lib/page.rb, line 61 def delete_page(page_id) begin RestClient.delete "#{@@conf_url}/#{@@urn}/#{page_id}", {params: { :os_username => @@login, :os_password => @@pwd }} rescue RestClient::ExceptionWithResponse => e puts Nokogiri.XML(e.response) return nil end true end
get_all_attachments(page_id)
click to toggle source
Return an array of all page attachment information
# File lib/page.rb, line 151 def get_all_attachments(page_id) url = "#{@@conf_url}/#{@@urn}/#{page_id}/child/attachment?os_username=#{@@login}&os_password=#{@@pwd}&status=current" begin atts = RestClient.get url, :content_type => 'application/json', :accept => 'json' rescue RestClient::ExceptionWithResponse => e puts Nokogiri.XML(e.response) nil end unless atts.nil? JSON.parse(atts)["results"] end end
labels()
click to toggle source
# File lib/page.rb, line 73 def labels begin res = RestClient.get "#{@@conf_url}/#{@@urn}/#{@id}/label", {params: { :os_username => @@login, :os_password => @@pwd, :start => 0, :limit => 500 }} rescue RestClient::ExceptionWithResponse => e puts Nokogiri.XML(e.response) nil end size = JSON.parse(res)['size'] if size > 0 size -= 1 labels = Array.new (0..size).each do |idx| labels << JSON.parse(res)['results'][idx]["name"] end labels else nil end end
rendered_body()
click to toggle source
Includes only the rendered HTML BODY of the page
# File lib/page.rb, line 39 def rendered_body begin res = RestClient.get "#{@@conf_url}/#{@@urn}/#{@id}", {params: { :expand => 'body.view', :os_username => @@login, :os_password => @@pwd }} rescue RestClient::ExceptionWithResponse => e puts Nokogiri.XML(e.response) end JSON.parse(res)['body']['view']['value'] end
save_file_attachments(page_id, storage_path)
click to toggle source
# File lib/page.rb, line 222 def save_file_attachments(page_id, storage_path) if File.writable? storage_path att_array = get_all_attachments(page_id) att_array.each do |line| download_hash = line.to_hash title = download_hash["title"] url = @@conf_url + download_hash["_links"]["download"] + "&os_username=#{@@login}&os_password=#{@@pwd}" File.open(storage_path + title, 'wb') {|f| block = proc { |response| response.read_body do |chunk| f.write chunk.to_s end } RestClient::Request.execute(method: :get, url: url, block_response: block) } end else puts "*** ERROR: Cannot write to path: #{storage_path}" puts " Skipping." return false end true end
storage_format()
click to toggle source
# File lib/page.rb, line 50 def storage_format begin res = RestClient.get "#{@@conf_url}/#{@@urn}/#{@id}", {params: { :expand => 'body.storage', :os_username => @@login, :os_password => @@pwd }} rescue RestClient::ExceptionWithResponse => e puts Nokogiri.XML(e.response) end JSON.parse(res)['body']['storage']['value'] end
styled_view()
click to toggle source
Includes entire HTML render (including HEADER etc.)
# File lib/page.rb, line 25 def styled_view begin res = RestClient.get "#{@@conf_url}/#{@@urn}/#{@id}", {params: { :expand => 'body.styled_view', :os_username => @@login, :os_password => @@pwd }} rescue RestClient::ExceptionWithResponse => e puts Nokogiri.XML(e.response) end JSON.parse(res)['body']['styled_view']['value'] end
url()
click to toggle source
# File lib/page.rb, line 16 def url unless @url.nil? @@conf_url + @url end end
Private Instance Methods
get_page_info_by_id(id, spacekey)
click to toggle source
# File lib/page.rb, line 280 def get_page_info_by_id(id, spacekey) begin res = RestClient.get "#{@@conf_url}/#{@@urn}/#{id}?os_username=#{@@login}&os_password=#{@@pwd}&expand=version,history" rescue RestClient::ExceptionWithResponse => e puts Nokogiri.XML(e.response) end # if res.nil? || JSON.parse(res)['results'].nil? if res.nil? puts "*** WARNING: Page not found for id: #{id}" puts " Space Key: #{spacekey}" return nil else return JSON.parse(res)['title'], JSON.parse(res)['id'], JSON.parse(res)['version']['number'], JSON.parse(res)['status'], JSON.parse(res)['history']['createdDate'], JSON.parse(res)['history']['createdBy']['username'], JSON.parse(res)['version']['when'], JSON.parse(res)['_links']['webui'] end end
get_page_info_by_title(title, spacekey)
click to toggle source
Here we can return various metadata for a given page.
# File lib/page.rb, line 255 def get_page_info_by_title(title, spacekey) begin res = RestClient.get "#{@@conf_url}/#{@@urn}", {params: { :title => title, :spaceKey => spacekey, :os_username => @@login, :os_password => @@pwd, :expand => 'version,history' }} rescue RestClient::ExceptionWithResponse => e puts Nokogiri.XML(e.response) end if res.nil? || JSON.parse(res)['results'][0].nil? puts '*** WARNING: Page ID not found.' puts " Page: #{title}" puts " Space Key: #{spacekey}" return nil else return JSON.parse(res)['results'][0]['title'], JSON.parse(res)['results'][0]['id'], JSON.parse(res)['results'][0]['version']['number'], JSON.parse(res)['results'][0]['status'], JSON.parse(res)['results'][0]['history']['createdDate'], JSON.parse(res)['results'][0]['history']['createdBy']['username'], JSON.parse(res)['results'][0]['version']['when'], JSON.parse(res)['results'][0]['_links']['webui'] end end