Class: Supportal::Confluence

Inherits:
Object
  • Object
show all
Defined in:
lib/supportal/confluence.rb

Instance Method Summary collapse

Constructor Details

#initialize(user, pass, instance) ⇒ Confluence

Returns a new instance of Confluence.



5
6
7
8
9
# File 'lib/supportal/confluence.rb', line 5

def initialize(user, pass, instance)
  @host = instance
  @user = user
  @pass = pass
end

Instance Method Details

#content_type_filter(type) ⇒ Object



16
17
18
# File 'lib/supportal/confluence.rb', line 16

def content_type_filter(type)
  type.sub(/com\.atlassian\.confluence\.plugins\.confluence\-questions:.*/, 'question')
end

#fetch_latest_blog_posts(limit, space) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/supportal/confluence.rb', line 24

def fetch_latest_blog_posts(limit, space)
  trim_to = 300 # Trim body of blog post to first X characters.
  path = '/rest/api/content/search?cql='
  cql = 'space=' + space + '+AND+type=blogpost+order+by+created+desc&limit=' + limit.to_i.to_s + '&expand=body.export_view,version'
  posts = Supportal::Request.get_json(@host + path + cql, @user, @pass)
  posts['results'].map do |post|
    {
      heading: highlight_filter(post['title']),
      body: strip_tags(post['body']['export_view']['value'])[0, trim_to] + '...',
      link: @host + post['_links']['webui'],
      date: post['version']['when']
    }
  end
end

#highlight_filter(str) ⇒ Object



20
21
22
# File 'lib/supportal/confluence.rb', line 20

def highlight_filter(str)
  strip_tags(str).gsub(/@@@hl@@@/, '<strong>').gsub(/@@@endhl@@@/, '</strong>')
end

#search(str, root_doc_page_ids, space, label) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/supportal/confluence.rb', line 39

def search(str, root_doc_page_ids, space, label)
  root_page_cql = root_doc_page_ids.map { |root_page| "(container=#{root_page} OR ancestor=#{root_page})" }.join(" OR ")
  path = '/rest/api/search?limit=10&expand=version&cql='
  cql = CGI.escape("text~\"#{str}\" AND (space=#{space} OR label=\"#{label}\") AND (((#{root_page_cql}) AND (type=blogpost OR type=page OR type=comment)) OR (type=\"com.atlassian.confluence.plugins.confluence-questions:question\" OR type=\"com.atlassian.confluence.plugins.confluence-questions:answer\"))")
  body = Supportal::Request.get_json(@host + path + cql, @user, @pass)
  body['results'].map do |result|
    {
      title: highlight_filter(result['title']),
      link: @host + result['url'],
      date: result['lastModified'],
      type: content_type_filter(result['content']['type'])
    }
  end
end

#strip_tags(str) ⇒ Object



11
12
13
14
# File 'lib/supportal/confluence.rb', line 11

def strip_tags(str)
  # TODO: Do this properly.
  str.gsub(/<\/?[^>]*>/, ' ')
end