Class: Asciidoctor::Confluence::Publisher

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

Constant Summary collapse

SUCCESSFUL_CREATE_RESULT =
'The page has been successfully created. It is available here: '
SUCCESSFUL_UPDATE_RESULT =
'The page has been successfully updated. It is available here: '

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Publisher

Returns a new instance of Publisher.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/asciidoctor/confluence.rb', line 17

def initialize(options)
  @confluence_options = options[:confluence]
  @asciidoctor_options = options
  @asciidoctor_options[:to_file] = false
  @asciidoctor_options[:header_footer] = false
  @asciidoctor_options[:backend] = 'xhtml'

  if (options[:input_files].is_a? ::Array) && (options[:input_files].length == 1)
    @input_file = options[:input_files][0]
  else
    @input_file = options[:input_files]
  end
end

Instance Method Details

#get_action_stringObject



60
61
62
63
# File 'lib/asciidoctor/confluence.rb', line 60

def get_action_string
  @confluence_options[:update] ? action = 'updated' : action = 'created'
  action
end

#publishObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/asciidoctor/confluence.rb', line 31

def publish
  document = Asciidoctor.convert_file @input_file, @asciidoctor_options
  page = Page.new @confluence_options[:space_key], @confluence_options[:title], document, @confluence_options[:page_id]
  api = ConfluenceAPI.new @confluence_options, page

  begin
    response = api.create_or_update_page @confluence_options[:update], @confluence_options[:page_id]

    response_body = JSON.parse response.body
    if response.success?
      url = response_body['_links']['base']+response_body['_links']['webui']

      if @confluence_options[:update]
        $stdout.puts SUCCESSFUL_UPDATE_RESULT + url
      else
        $stdout.puts SUCCESSFUL_CREATE_RESULT + url
      end
      return 0
    else
      action = get_action_string
      show_error action, response_body['message']
      return 1
    end
  rescue Exception => e
    show_error get_action_string, e.message
  end
  return 0
end

#show_error(action, message) ⇒ Object



65
66
67
# File 'lib/asciidoctor/confluence.rb', line 65

def show_error(action, message)
  $stderr.puts "An error occurred, the page has not been #{action} because:\n#{message}"
end