Class: Apadmi::Grout::ConfluenceService

Inherits:
Object
  • Object
show all
Defined in:
lib/apadmi/grout/service/confluence_service.rb

Overview

Provides a layer of abstraction on top of the Confluence api

Instance Method Summary collapse

Constructor Details

#initialize(network_service) ⇒ ConfluenceService

Returns a new instance of ConfluenceService.

Parameters:



11
12
13
# File 'lib/apadmi/grout/service/confluence_service.rb', line 11

def initialize(network_service)
  @network_service = network_service
end

Instance Method Details

#create_page(space_id, parent_id, title, content) ⇒ Apadmi::Grout::ConfluencePage

Parameters:

  • space_id (Integer)
  • parent_id (String)
  • title (String)
  • content (String)

Returns:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/apadmi/grout/service/confluence_service.rb', line 45

def create_page(space_id, parent_id, title, content)
  payload = {
    type: "page",
    title: title,
    spaceId: space_id,
    parentId: parent_id,
    body: {
      value: content,
      representation: "storage"
    }
  }.to_json

  response = @network_service.do_post("/wiki/api/v2/pages", payload)
  json = JSON.parse(response.body)

  Apadmi::Grout::ConfluencePage.from_hash(json)
end

#delete_page(page_id) ⇒ Object

Parameters:

  • page_id (String)


86
87
88
# File 'lib/apadmi/grout/service/confluence_service.rb', line 86

def delete_page(page_id)
  @network_service.do_delete("/wiki/api/v2/pages/#{page_id}")
end

#get_page_by_id(page_id) ⇒ Apadmi::Grout::ConfluencePage

Parameters:

  • page_id (String)

Returns:



17
18
19
20
21
# File 'lib/apadmi/grout/service/confluence_service.rb', line 17

def get_page_by_id(page_id)
  response = @network_service.do_get("/wiki/api/v2/pages/#{page_id}?body-format=storage")
  json = JSON.parse(response.body)
  Apadmi::Grout::ConfluencePage.from_hash(json)
end

#get_page_by_title(title, parent_id) ⇒ Apadmi::Grout::ConfluencePage?

Parameters:

  • title (String)
  • parent_id (String)

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/apadmi/grout/service/confluence_service.rb', line 26

def get_page_by_title(title, parent_id)
  # Using api/v2/pages?title=<title>&body-format=storage
  response = @network_service.do_get("/wiki/api/v2/pages?title=#{URI.encode_www_form_component(title)}&body-format=storage")

  json = JSON.parse(response.body)
  base_url = json.dig("_links", "base")

  matches = json["results"].select { |r| r["parentId"] == parent_id }
  return nil if matches.empty?
  raise "Multiple pages found with title '#{title}' under parent #{parent_id}" if matches.length > 1

  Apadmi::Grout::ConfluencePage.from_hash(matches.first, base_url)
end

#update_page(page_id, title, content, version_number) ⇒ Apadmi::Grout::ConfluencePage

Parameters:

  • page_id (String)
  • title (String)
  • content (String)
  • version_number (Integer)

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/apadmi/grout/service/confluence_service.rb', line 68

def update_page(page_id, title, content, version_number)
  payload = {
    version: { number: version_number + 1 },
    title: title,
    type: "page",
    status: "current",
    id: page_id,
    body: {
      value: content,
      representation: "storage"
    }
  }.to_json
  response = @network_service.do_put("/wiki/api/v2/pages/#{page_id}", payload)
  json = JSON.parse(response.body)
  Apadmi::Grout::ConfluencePage.from_hash(json)
end