class Confluence::Api

Constants

CREDENTIALS_FILE

Public Class Methods

new() click to toggle source
# File lib/confluence/api.rb, line 15
def initialize
  credentials = YAML.load(File.read(CREDENTIALS_FILE))
  @username = credentials["username"]
  @token = credentials["token"]
  @space = credentials["space"]
end

Public Instance Methods

create_page(parent_page_id: nil, body:, page_title:) click to toggle source
# File lib/confluence/api.rb, line 93
def create_page(parent_page_id: nil, body:, page_title:)
  payload = {
    type: "page",
    title: page_title,
    space: {
      key: @space
    },
    body: {
      wiki: {
        value: body,
        representation: "wiki"
      }
    }
  }

  if parent_page_id
    payload.merge!({
      ancestors: [
        { id: parent_page_id }
      ]
    })
  end

  response = HTTParty.post(
    "https://dbdoc.atlassian.net/wiki/rest/api/content/", {
      headers: {
        "Authorization" => "Basic #{basic_auth}",
        "Content-Type" => "application/json"
      },
      body: payload.to_json
    }
  )

  if response.code == 200
    {
      response: response,
      page_id: JSON.parse(response.body)["id"]
    }
  else
    puts "--> ERROR UPLOADING #{page_title}: "
    pp response

    {
      response: response
    }
  end
end
delete_page(page_id:) click to toggle source
# File lib/confluence/api.rb, line 22
def delete_page(page_id:)
  response = HTTParty.delete(
    "https://dbdoc.atlassian.net/wiki/rest/api/content/#{page_id}", {
      headers: {
        "Authorization" => "Basic #{basic_auth}",
        "Content-Type" => "application/json"
      }
    }
  )

  response.code == 200
end
existing_pages() click to toggle source
# File lib/confluence/api.rb, line 35
def existing_pages
  # TODO: paginate over all pages in the space
  response = HTTParty.get(
    "https://dbdoc.atlassian.net/wiki/rest/api/content/?&spaceKey=#{@space}", {
      headers: {
        "Authorization" => "Basic #{basic_auth}",
        "Content-Type" => "application/json"
      }
    }
  )

  JSON.parse(response.body)
end
update_page(page_id:, body:, page_title:, version:) click to toggle source
# File lib/confluence/api.rb, line 49
def update_page(page_id:, body:, page_title:, version:)
  payload = {
    id: page_id,
    type: "page",
    title: page_title,
    space: {
      key: @space
    },
    body: {
      wiki: {
        value: body,
        representation: "wiki"
      }
    },
    version: {
      number: version
    }
  }

  response = HTTParty.put(
    "https://dbdoc.atlassian.net/wiki/rest/api/content/#{page_id}", {
      headers: {
        "Authorization" => "Basic #{basic_auth}",
        "Content-Type" => "application/json"
      },
      body: payload.to_json
    }
  )

  if response.code == 200
    {
      response: response,
      page_id: JSON.parse(response.body)["id"]
    }
  else
    puts "--> ERROR UPLOADING #{page_title}: "
    pp response

    {
      response: response
    }
  end
end

Private Instance Methods

basic_auth() click to toggle source
# File lib/confluence/api.rb, line 143
def basic_auth
  Base64.encode64("#{@username}:#{@token}").chomp
end