Class: Asciidoctor::ConfluencePublisher::ConfluenceApi

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor/confluence_publisher/confluence_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, space, username, password, skip_verify_ssl: false, proxy: nil) ⇒ ConfluenceApi

Returns a new instance of ConfluenceApi.



9
10
11
12
13
14
15
16
# File 'lib/asciidoctor/confluence_publisher/confluence_api.rb', line 9

def initialize(host, space, username, password, skip_verify_ssl: false, proxy: nil)
  @host = host
  @space = space
  @username = username
  @password = password
  @skip_verify_ssl = skip_verify_ssl
  RestClient.proxy = proxy if proxy
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



7
8
9
# File 'lib/asciidoctor/confluence_publisher/confluence_api.rb', line 7

def host
  @host
end

#spaceObject (readonly)

Returns the value of attribute space.



7
8
9
# File 'lib/asciidoctor/confluence_publisher/confluence_api.rb', line 7

def space
  @space
end

#usernameObject (readonly)

Returns the value of attribute username.



7
8
9
# File 'lib/asciidoctor/confluence_publisher/confluence_api.rb', line 7

def username
  @username
end

Instance Method Details

#create_attachment(page_id, file_path) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/asciidoctor/confluence_publisher/confluence_api.rb', line 119

def create_attachment(page_id, file_path)
  url = host + "/rest/api/content/#{page_id}/child/attachment"
  payload = {
      file: File.new(file_path, 'rb')
  }
  header = {
      x_atlassian_token: 'nocheck'
  }

  req_result = send_request(:post, url, payload, default_headers.merge(header), multipart: true)
  Model::Attachment.new(req_result[:body]) if req_result[:success]
end

#create_page(title, content, ancestor_id) ⇒ Object

create a confluence page



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/asciidoctor/confluence_publisher/confluence_api.rb', line 20

def create_page(title, content, ancestor_id)
  url = host + '/rest/api/content?expand=version,ancestors,space'
  payload = {
      title: title,
      type: 'page',
      space: {key: space},
      ancestors: Array(ancestor_id).map { |ans_id| { id: ans_id } },
      body: {
          storage: {
              value: content,
              representation: 'storage'
          }
      }
  }

  req_result = send_request(:post, url, payload, default_headers)
  Model::Page.new req_result[:body] if req_result[:success]
end

#get_attachments(page_id) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/asciidoctor/confluence_publisher/confluence_api.rb', line 99

def get_attachments(page_id)
  url = host + "/rest/api/content/#{page_id}/child/attachment"
  start = 0
  limit = 50

  result = []
  loop do
    payload = { start: start, limit: limit}
    req_result = send_request(:get, url, payload, default_headers)
    no_data = true
    if req_result[:success] && req_result[:body]['size'] > 0
      result.concat req_result[:body]['results'].map { |attachment| Model::Attachment.new attachment }
    end

    break if no_data
    start += 1
  end
  result
end

#get_page_by_id(page_id) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/asciidoctor/confluence_publisher/confluence_api.rb', line 60

def get_page_by_id(page_id)
  url = host + "/rest/api/content/#{page_id}"
  payload = {
      expand: 'version,space,ancestors'
  }
  begin
    req_result = send_request(:get, url, payload, default_headers)
    Model::Page.new(req_result[:body]) if req_result[:success]
  rescue => e
    $stderr.puts "not found page with id #{page_id}. message: #{e.message}"
  end
end

#get_page_property(owner_id, key) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/asciidoctor/confluence_publisher/confluence_api.rb', line 160

def get_page_property(owner_id, key)
  url = host + "/rest/api/content/#{owner_id}/property/#{key}"
  payload = {
      expand: 'version'
  }

  begin
    req_result = send_request(:get, url, payload, default_headers)
    Model::Property.new req_result[:body] if req_result[:success]
  rescue => e
  end
end

#get_pages_by_title(title) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/asciidoctor/confluence_publisher/confluence_api.rb', line 73

def get_pages_by_title(title)
  url = host + '/rest/api/content'
  payload = {
      type: 'page',
      spaceKey: space,
      title: title,
      expand: 'ancestors,space,version'
  }

  start =0
  limit = 30
  result = []
  loop do
    pageable = { start: start, limit: limit}
    req_result = send_request(:get, url, payload.merge(pageable), default_headers)
    no_data = true
    if req_result[:success] && req_result[:body]['size'] > 0
      result.concat req_result[:body]['results'].map { |page| Model::Page.new(page) }
      no_data = req_result[:body]['size'] < req_result[:body]['limit']
    end
    break if no_data
    start += 1
  end
  result
end

#remove_page_property(owner_id, key) ⇒ Object



173
174
175
176
177
178
# File 'lib/asciidoctor/confluence_publisher/confluence_api.rb', line 173

def remove_page_property(owner_id, key)
  url = host + "/rest/api/content/#{owner_id}/property/#{key}"
  payload = {}

  send_request(:delete, url, payload, default_headers)
end

#set_page_property(owner_id, key, value) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/asciidoctor/confluence_publisher/confluence_api.rb', line 146

def set_page_property(owner_id, key, value)
  url = host + "/rest/api/content/#{owner_id}/property/#{key}?expand=version"
  current_property = get_page_property(owner_id, key)
  payload = {
      value: value,
      version: {
          number: (current_property && current_property.version.number).to_i + 1
      }
  }

  req_result = send_request(:put, url, payload, default_headers)
  Model::Property.new req_result[:body] if req_result[:success]
end

#update_attachment(page_id, attachment_id, file_path) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/asciidoctor/confluence_publisher/confluence_api.rb', line 132

def update_attachment(page_id, attachment_id, file_path)
  url = host + "/rest/api/content/#{page_id}/child/attachment/#{attachment_id}/data"
  payload = {
      file: File.new(file_path, 'rb')
  }
  header = {
      x_atlassian_token: 'nocheck',
      content_type: 'multipart/form-data'
  }

  req_result = send_request(:post, url, payload, default_headers.merge(header))
  Model::Attachment.new(req_result[:body]) if req_result[:success]
end

#update_page(page_id, title, content) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/asciidoctor/confluence_publisher/confluence_api.rb', line 39

def update_page(page_id, title, content)
  url = host + "/rest/api/content/#{page_id}"
  current_page = get_page_by_id(page_id)
  payload = {
      title: title,
      type: 'page',
      body: {
          storage: {
              value: content,
              representation: 'storage'
          }
      },
      version: {
          number: current_page.version.number + 1
      }
  }

  req_result = send_request(:put, url, payload, default_headers)
  Model::Page.new req_result[:body] if req_result[:success]
end