Class: Confluence::Api

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/confluence/api.rb

Constant Summary collapse

CREDENTIALS_FILE =
File.join(Dir.pwd, "confluence.yml")

Instance Method Summary collapse

Constructor Details

#initializeApi

Returns a new instance of Api.



15
16
17
18
19
20
# 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

Instance Method Details

#create_page(parent_page_id: nil, body:, page_title:) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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:) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# 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_pagesObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# 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:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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