class ConfluenceHelper::Confluence

Public Instance Methods

deliverables_pages(rootpageid) click to toggle source
# File lib/confluence_helper.rb, line 62
def deliverables_pages(rootpageid)
  child_pages_in_space(rootpageid, {:expand => "metadata.labels,body.export_view"} ).
    each_with_object({}) do |page, hash|
      hash[page["title"]] = DeliverablePage.new( 
          labels: page.fetch("metadata", {}).fetch("labels", {}).fetch("results",[]).map{|l| l["name"]},
          body: page.fetch("body", {}).fetch("export_view", {}).fetch("value", "")
       )
    end
end
find_url_by_title_and_space(spacekey, title) click to toggle source
# File lib/confluence_helper.rb, line 53
def find_url_by_title_and_space(spacekey, title)
  page_by_title_in_space(spacekey, title, {})["_links"]["self"]
end
get_labels_for_each_child_page(rootpageid) click to toggle source
# File lib/confluence_helper.rb, line 41
def get_labels_for_each_child_page(rootpageid)
  child_pages_in_space(rootpageid, {:expand => "metadata.labels"} ).
    each_with_object({}) do |page, hash|
      hash[page["title"]] = 
        page.
          fetch("metadata", {}).
          fetch("labels",{}).
          fetch("results",[]).
          map{|l| l["name"]}
    end
end
save_page_attachment( spacekey, page_title, attachment_name, destination ) click to toggle source
# File lib/confluence_helper.rb, line 57
def save_page_attachment( spacekey, page_title, attachment_name, destination )
  response = query_rest_api( get_attachment_url( spacekey, page_title, attachment_name ) )
  File.open(destination, 'wb') { |fp| fp.write(response.body) }
end

Private Instance Methods

child_pages_in_space(pageid, options) click to toggle source
# File lib/confluence_helper.rb, line 118
def child_pages_in_space(pageid, options)
  enum_for(:for_all_pages_of_results, "rest/api/content/#{pageid}/child/page", options)
end
for_all_pages_of_results(url, options, &block) click to toggle source
# File lib/confluence_helper.rb, line 122
def for_all_pages_of_results(url, options, &block)
  full_options = {start:0, limit:25}.merge(options)
  response = query_rest_api(url, full_options)
  if response && response.success? 
    results = response.body.fetch('results',[])
    results.each{ |p| block.call(p) }
    if(results.count == full_options[:limit])
      start = full_options[:start] + results.count 
      for_all_pages_of_results(url, options.merge({start:start}), &block)  
    end
  end
end
get_attachment_data(spacekey, page_title, attachment_name) click to toggle source
# File lib/confluence_helper.rb, line 100
def get_attachment_data(spacekey, page_title, attachment_name)
  get_attachments_data(spacekey, page_title).find{|x| x["title"] == attachment_name}
end
get_attachment_url(spacekey, page_title, attachment_name) click to toggle source
# File lib/confluence_helper.rb, line 96
def get_attachment_url(spacekey, page_title, attachment_name)
  "#{ENVied.CONSTASH_URL}#{ get_attachment_data(spacekey, page_title, attachment_name)["_links"]["download"]}"
end
get_attachments_data(spacekey, page_title) click to toggle source
# File lib/confluence_helper.rb, line 104
def get_attachments_data(spacekey, page_title)
  page_by_title_in_space(spacekey, page_title, {expand:"children.attachment"})["children"]["attachment"]["results"]
end
get_logged_in_connection(request = :json) click to toggle source
# File lib/confluence_helper.rb, line 135
  def get_logged_in_connection(request = :json)
    # TODO: move to memcache
    # store = ActiveSupport::Cache.lookup_store(:mem_cache_store, ['localhost:11211'])
  
    return Faraday.new(:url => ENVied.CONSTASH_URL) do |faraday|
      faraday.use :http_cache # , store: store
      faraday.request :url_encoded              # form-encode POST params
      faraday.basic_auth(ENVied.CONSTASH_USERNAME, ENVied.CONSTASH_PASSWORD)
      faraday.request request

      faraday.response :json, :content_type => /\bjson$/
#      faraday.response :logger                  # log requests to STDOUT

      faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
    end
  end
get_url(space, title) click to toggle source
# File lib/confluence_helper.rb, line 161
def get_url(space, title)
  @@url[ [space,title] ] ||= find_url_by_title_and_space(space, title)
end
page_by_title_in_space(spacekey, page_title, options) click to toggle source
# File lib/confluence_helper.rb, line 108
def page_by_title_in_space(spacekey, page_title, options)
  all_pages = pages_in_space(spacekey, options).lazy
  all_pages.find {|p| p['title'] == page_title}.
    tap{|page| raise "Can't find page with title #{page_title} in that space #{spacekey}\n\tFound pages: #{all_pages.map{|p| p['title']}.join("\n\t")}" unless page }
end
pages_in_space(spacekey, options) click to toggle source
# File lib/confluence_helper.rb, line 114
def pages_in_space(spacekey, options)
  enum_for(:for_all_pages_of_results, "rest/api/space/#{spacekey}/content/page", options)
end
query_rest_api(url, options = {}) click to toggle source
# File lib/confluence_helper.rb, line 152
def query_rest_api(url, options = {})
  puts "-->#{url} #{options.inspect}"
  get_logged_in_connection().
    get(url, options).
      tap{ |response| 
        raise "Error while attempting to query '#{ENVied.CONSTASH_URL}/#{url}'\n Status: #{response.status} \n #{response.body}" unless [200, 302].include? response.status  
      }
end