Class: Growi::ImageConverter::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/growi/image_converter/page.rb

Overview

page を扱うクラス

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_id, client, dry_run: true) ⇒ Page

Returns a new instance of Page.



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

def initialize(page_id, client, dry_run: true)
  @dry_run = dry_run
  @client = client
  @data = get(page_id)
  @body = Body.new(data.revision.body)
  @attached_files = []
end

Instance Attribute Details

#attached_filesObject

Returns the value of attribute attached_files.



14
15
16
# File 'lib/growi/image_converter/page.rb', line 14

def attached_files
  @attached_files
end

#bodyObject

Returns the value of attribute body.



14
15
16
# File 'lib/growi/image_converter/page.rb', line 14

def body
  @body
end

#dataObject

Returns the value of attribute data.



14
15
16
# File 'lib/growi/image_converter/page.rb', line 14

def data
  @data
end

Instance Method Details

#attach_file(file) ⇒ Object

Raises:

  • (StandardError)


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

def attach_file(file)
  if @dry_run
    attachment_params = { _id: 'dry-run-' + data._id + '-' + SecureRandom.hex(10), creator: 'dry-run-user' }
    return { attachment: GrowiAttachment.new(attachment_params) }
  end

  req = GApiRequestAttachmentsAdd.new page_id: data._id, file: file
  api_return = @client.request(req)

  raise StandardError, 'Failed to atach file.' unless api_return.ok

  api_return.data
end

#attach_files(markdown_images_group_by_url, tempdir) ⇒ Object



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

def attach_files(markdown_images_group_by_url, tempdir)
  markdown_images_group_by_url.each do |url, markdown_images|
    begin
      image_file = Growi::ImageConverter::Esa.get_image_from_esa url, tempdir
      attached_file = attach_file image_file
      attached_files.push(Growi::ImageConverter::AttachedImageFile.new(markdown_images, attached_file))
    rescue StandardError => e
      puts 'PageID: ' + data._id + ', Image URL: ' + url + ', Message: ' + e.message
      next
    end
  end
end

#get(page_id) ⇒ Object

Raises:

  • (StandardError)


16
17
18
19
20
21
22
23
# File 'lib/growi/image_converter/page.rb', line 16

def get(page_id)
  req = GApiRequestPagesGet.new page_id: page_id
  api_return = @client.request(req)

  raise StandardError, 'Failed to get page data.' unless api_return.ok

  api_return.data
end

#replace_markdown_imageObject



52
53
54
# File 'lib/growi/image_converter/page.rb', line 52

def replace_markdown_image
  attached_files.each { |attached_file| body.replace_markdown_image(attached_file) }
end

#updateObject

Raises:

  • (StandardError)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/growi/image_converter/page.rb', line 56

def update
  if attached_files.empty?
    puts 'PageID: ' + data._id + ', Result: Through'
    return
  end

  if @dry_run
    puts 'PageID: ' + data._id + ', Result: Converted'
    return
  end

  req = GApiRequestPagesUpdate.new(
    page_id: data._id,
    revision_id: data.revision._id,
    body: body,
    grant: data.grant
  )
  api_return = @client.request(req)

  raise StandardError, 'Failed to update page.' unless api_return.ok

  puts 'PageID: ' + data._id + ', Result: Converted'
end