Class: SyncReadme::ConfluenceSync

Inherits:
Object
  • Object
show all
Defined in:
lib/sync_readme/confluence_sync.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ConfluenceSync

Returns a new instance of ConfluenceSync.



6
7
8
9
10
11
12
13
14
# File 'lib/sync_readme/confluence_sync.rb', line 6

def initialize(config)
  @page_id = config.page_id
  @client = Faraday.new(url: config.url) do |faraday|
    faraday.request  :url_encoded
    # faraday.response :logger                  # log requests to STDOUT
    faraday.adapter  Faraday.default_adapter # make requests with Net::HTTP
    faraday.basic_auth(config.username, config.password)
  end
end

Instance Method Details

#get_pageObject



21
22
23
24
# File 'lib/sync_readme/confluence_sync.rb', line 21

def get_page
  response = @client.get("rest/api/content/#{@page_id}", expand: 'body.view,version')
  JSON.parse(response.body)
end

#increment_version(page) ⇒ Object



51
52
53
# File 'lib/sync_readme/confluence_sync.rb', line 51

def increment_version(page)
  page['version']['number'] + 1
end

#update(params) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/sync_readme/confluence_sync.rb', line 26

def update(params)
  @client.put do |request|
    request.url "rest/api/content/#{@page_id}"
    request.headers['Content-Type'] = 'application/json'
    request.body = params.to_json
  end
end

#update_page_content(content) ⇒ Object



16
17
18
19
# File 'lib/sync_readme/confluence_sync.rb', line 16

def update_page_content(content)
  page = get_page
  update(updated_page_params(page, content))
end

#updated_page_params(page, new_content) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sync_readme/confluence_sync.rb', line 34

def updated_page_params(page, new_content)
  {
    version: {
      number: increment_version(page),
      minorEdit: true
    },
    title: page['title'],
    type: 'page',
    body: {
      storage: {
        value: new_content,
        representation: 'storage'
      }
    }
  }
end