Class: PageObject

Inherits:
ConfluenceClient show all
Defined in:
lib/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ConfluenceClient

#create_page_with_no_parent, #create_page_with_parent, #update_page_with_no_parent, #update_page_with_parent

Constructor Details

#initialize(title_or_id, spacekey) ⇒ PageObject

Returns a new instance of PageObject.



7
8
9
10
11
12
13
# File 'lib/page.rb', line 7

def initialize(title_or_id, spacekey)
  if title_or_id.is_a? Integer
    @title, @id, @version, @status, @created, @created_by, @last_updated, @url = get_page_info_by_id(title_or_id.to_s, spacekey)
  else
    @title, @id, @version, @status, @created, @created_by, @last_updated, @url = get_page_info_by_title(title_or_id, spacekey)
  end
end

Instance Attribute Details

#createdObject (readonly)

Returns the value of attribute created.



5
6
7
# File 'lib/page.rb', line 5

def created
  @created
end

#created_byObject (readonly)

Returns the value of attribute created_by.



5
6
7
# File 'lib/page.rb', line 5

def created_by
  @created_by
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/page.rb', line 5

def id
  @id
end

#last_updatedObject (readonly)

Returns the value of attribute last_updated.



5
6
7
# File 'lib/page.rb', line 5

def last_updated
  @last_updated
end

#statusObject (readonly)

Returns the value of attribute status.



5
6
7
# File 'lib/page.rb', line 5

def status
  @status
end

#titleObject (readonly)

Returns the value of attribute title.



5
6
7
# File 'lib/page.rb', line 5

def title
  @title
end

#versionObject (readonly)

Returns the value of attribute version.



5
6
7
# File 'lib/page.rb', line 5

def version
  @version
end

Instance Method Details

#add_labels(labels) ⇒ Object



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
# File 'lib/page.rb', line 95

def add_labels(labels)
  payload = String.new('[')
  json_label = '{
      "prefix": "global",
      "name": "__LABEL__"
    }'.freeze

  labels.each_with_index do |l, idx|
    jl = json_label.sub(/__LABEL__/, l)
    if idx == 0
      payload += jl
    else
      payload += ',' + jl
    end
  end
  payload = payload + ']'
  puts "Add label payload: #{payload}"

  begin
    res = RestClient.post "#{@@conf_url}/#{@@urn}/#{@id}/label?os_username=#{@@login}&os_password=#{@@pwd}", payload, :content_type => 'application/json'
  rescue RestClient::ExceptionWithResponse => e
    puts Nokogiri.XML(e.response)
  end
  if res.nil?
    puts "*** WARNING: Label update failed for page with ID: #{@id}"
    nil
  else
    puts JSON.parse(res)
    true
  end
end

#attach_binary_file(file_name, file_basename) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/page.rb', line 163

def attach_binary_file(file_name, file_basename)

  if File.exist?("#{file_basename}/#{file_name}")
    payload = {
        multipart: true,
        file: File.new("#{file_basename}/#{file_name}", 'rb'),
        comment: 'Automated Ruby import',
        minorEdit: true
    }
    url_mod = "#{@@conf_url}/#{@@urn}/#{@id}/child/attachment?os_username=#{@@login}&os_password=#{@@pwd}"
    begin
      RestClient.post(url_mod, payload, {"X-Atlassian-Token" => "nocheck"})
      true
    rescue RestClient::ExceptionWithResponse => e
      puts Nokogiri.XML(e.response)
      nil
    end
  else
    puts "*** WARNING: File can't be found for #{file_basename}/#{file_name}"
    nil
  end
end

#attachment_id(attachment_name) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/page.rb', line 198

def attachment_id(attachment_name)

  fname = attachment_name.dup
  fname = CGI.escape(fname)

  begin
    response = RestClient.get "#{@@conf_url}/#{@@urn}/#{@id}/child/attachment", {params: {
        :filename => fname, 'os_username' => @@login, 'os_password' => @@pwd
    }}

    response = JSON.parse(response)
    if response['results'].any?
      return response['results'][0]['id']
    end
  rescue RestClient::ExceptionWithResponse => e
    puts Nokogiri.XML(e.response)
  end
  nil
end

#delete_attachment(attach_id) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/page.rb', line 186

def delete_attachment(attach_id)
  begin
    RestClient.delete "#{@@conf_url}/#{@@urn}/#{attach_id}", {params: {
        :os_username => @@login, :os_password => @@pwd
    }}
  rescue RestClient::ExceptionWithResponse => e
    puts Nokogiri.XML(e.response)
    return nil
  end
  true
end

#delete_labels(labels) ⇒ Object

Note that we must use query parameters here because deleting labels with a “/” will fail otherwise.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/page.rb', line 128

def delete_labels(labels)
  labels.each do |l|
    puts "Deleting label: #{l}"
    begin
      res = RestClient.delete "#{@@conf_url}/#{@@urn}/#{@id}/label?name=#{l}&os_username=#{@@login}&os_password=#{@@pwd}"
    rescue RestClient::ExceptionWithResponse => e
      puts Nokogiri.XML(e.response)
    end
    if res.nil?
      puts "*** WARNING: Label removal failed for page with ID: #{@id}"
      return nil
    end
  end

  true

end

#delete_page(page_id) ⇒ Object



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

def delete_page(page_id)
  begin
    RestClient.delete "#{@@conf_url}/#{@@urn}/#{page_id}", {params: {
        :os_username => @@login, :os_password => @@pwd
    }}
  rescue RestClient::ExceptionWithResponse => e
    puts Nokogiri.XML(e.response)
    return nil
  end
  true
end

#get_all_attachments(page_id) ⇒ Object

Return an array of all page attachment information



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/page.rb', line 147

def get_all_attachments(page_id)

  url = "#{@@conf_url}/#{@@urn}/#{page_id}/child/attachment?os_username=#{@@login}&os_password=#{@@pwd}&status=current"

  begin
    atts = RestClient.get url, :content_type => 'application/json', :accept => 'json'
  rescue RestClient::ExceptionWithResponse => e
    puts Nokogiri.XML(e.response)
    nil
  end

  unless atts.nil?
    JSON.parse(atts)["results"]
  end
end

#labels(page_id) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/page.rb', line 72

def labels(page_id)
  begin
    res = RestClient.get "#{@@conf_url}/#{@@urn}/#{page_id}/label", {params: {
        :os_username => @@login, :os_password => @@pwd,
        :start => 0, :limit => 1000
    }}
  rescue RestClient::ExceptionWithResponse => e
    puts Nokogiri.XML(e.response)
    nil
  end
  size = JSON.parse(res)['size']
  if size > 0
    size -= 1
    labels = Array.new
    (0..size).each do |idx|
      labels << JSON.parse(res)['results'][idx]["name"]
    end
    labels
  else
    nil
  end
end

#rendered_bodyObject

Includes only the rendered HTML BODY of the page



38
39
40
41
42
43
44
45
46
47
# File 'lib/page.rb', line 38

def rendered_body
  begin
    res = RestClient.get "#{@@conf_url}/#{@@urn}/#{@id}", {params: {
        :expand => 'body.view', :os_username => @@login, :os_password => @@pwd
    }}
  rescue RestClient::ExceptionWithResponse => e
    puts Nokogiri.XML(e.response)
  end
  JSON.parse(res)['body']['view']['value']
end

#save_file_attachments(page_id, storage_path) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/page.rb', line 218

def save_file_attachments(page_id, storage_path)
  if File.writable? storage_path
    att_array = get_all_attachments(page_id)

    att_array.each do |line|
      download_hash = line.to_hash
      title = download_hash["title"]
      url   = @@conf_url + download_hash["_links"]["download"] + "&os_username=#{@@login}&os_password=#{@@pwd}"

      File.open(storage_path + title, 'wb') {|f|
        block = proc { |response|
          response.read_body do |chunk|
            f.write chunk.to_s
          end
        }
        RestClient::Request.execute(method: :get,
                                    url: url,
                                    block_response: block)
      }
    end
  else
    puts "*** ERROR: Cannot write to path: #{storage_path}"
    puts "   Skipping."
    return false
  end
  true
end

#storage_formatObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/page.rb', line 49

def storage_format
  begin
    res = RestClient.get "#{@@conf_url}/#{@@urn}/#{@id}", {params: {
        :expand => 'body.storage', :os_username => @@login, :os_password => @@pwd
    }}
  rescue RestClient::ExceptionWithResponse => e
    puts Nokogiri.XML(e.response)
  end
  JSON.parse(res)['body']['storage']['value']
end

#styled_viewObject

Includes entire HTML render (including HEADER etc.)



24
25
26
27
28
29
30
31
32
33
# File 'lib/page.rb', line 24

def styled_view
  begin
    res = RestClient.get "#{@@conf_url}/#{@@urn}/#{@id}", {params: {
        :expand => 'body.styled_view', :os_username => @@login, :os_password => @@pwd
    }}
  rescue RestClient::ExceptionWithResponse => e
    puts Nokogiri.XML(e.response)
  end
  JSON.parse(res)['body']['styled_view']['value']
end

#urlObject



15
16
17
18
19
# File 'lib/page.rb', line 15

def url
  unless @url.nil?
    @@conf_url + @url
  end
end