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

#attach_binary_file(file_name, file_basename) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/page.rb', line 89

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



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

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



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/page.rb', line 112

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_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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/page.rb', line 73

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

#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



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/page.rb', line 144

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