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.



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

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.



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

def created
  @created
end

#created_byObject (readonly)

Returns the value of attribute created_by.



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

def created_by
  @created_by
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#last_updatedObject (readonly)

Returns the value of attribute last_updated.



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

def last_updated
  @last_updated
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#add_labels(labels) ⇒ Object



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

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



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/page.rb', line 167

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



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/page.rb', line 202

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



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/page.rb', line 190

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.



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

def delete_labels(labels)
  labels.each do |l|
    puts "Deleting label: #{l}"
    begin
      uri = "#{@@conf_url}/#{@@urn}/#{@id}/label?name=#{l}&os_username=#{@@login}&os_password=#{@@pwd}"
      uri = Addressable::URI.parse(uri).normalize.to_s
      res = RestClient.delete uri

    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



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

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



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/page.rb', line 151

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

#labelsObject



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

def labels
  begin
    res = RestClient.get "#{@@conf_url}/#{@@urn}/#{@id}/label", {params: {
        :os_username => @@login, :os_password => @@pwd,
        :start => 0, :limit => 500
    }}
  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



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

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



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

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



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

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.)



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

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



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

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