Class: TrelloFs::AttachmentBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/trello-fs/attachment_builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(card_builder, attachment) ⇒ AttachmentBuilder

Returns a new instance of AttachmentBuilder.



11
12
13
14
# File 'lib/trello-fs/attachment_builder.rb', line 11

def initialize(card_builder, attachment)
  @card_builder = card_builder
  @attachment = attachment
end

Instance Attribute Details

#attachmentObject (readonly)

Returns the value of attribute attachment.



5
6
7
# File 'lib/trello-fs/attachment_builder.rb', line 5

def attachment
  @attachment
end

#card_builderObject (readonly)

Returns the value of attribute card_builder.



5
6
7
# File 'lib/trello-fs/attachment_builder.rb', line 5

def card_builder
  @card_builder
end

Class Method Details

.new_by_attachment(repository, attachment) ⇒ Object



7
8
9
# File 'lib/trello-fs/attachment_builder.rb', line 7

def self.new_by_attachment(repository, attachment)
  self.new(CardBuilder.new_by_card(repository, attachment.card), attachment)
end

Instance Method Details

#already_downloaded?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/trello-fs/attachment_builder.rb', line 52

def already_downloaded?
  File.exist? path
end

#board_builderObject



64
65
66
# File 'lib/trello-fs/attachment_builder.rb', line 64

def board_builder
  @card_builder.board_builder
end

#buildObject



16
17
18
19
20
# File 'lib/trello-fs/attachment_builder.rb', line 16

def build
  return if already_downloaded?

  download_and_save
end

#downloadObject



56
57
58
# File 'lib/trello-fs/attachment_builder.rb', line 56

def download
  open(@attachment.url).read
end

#download_and_saveObject



22
23
24
25
26
27
28
29
30
# File 'lib/trello-fs/attachment_builder.rb', line 22

def download_and_save
  data = download
  return unless data

  FileUtils.mkpath(full_folder_path) unless File.exist? full_folder_path
  File.open(path, 'wb') do |file|
    file.write data
  end
end

#file_nameObject



48
49
50
# File 'lib/trello-fs/attachment_builder.rb', line 48

def file_name
  @attachment.name.gsub(/ /, '_')
end

#folder_pathObject



40
41
42
# File 'lib/trello-fs/attachment_builder.rb', line 40

def folder_path
  File.join('Attachments', board_builder.folder_name, StringToFileName.convert(@card_builder.card_name))
end

#full_folder_pathObject



44
45
46
# File 'lib/trello-fs/attachment_builder.rb', line 44

def full_folder_path
  File.join(repository.path, folder_path)
end

#pathObject



32
33
34
# File 'lib/trello-fs/attachment_builder.rb', line 32

def path
  File.join(full_folder_path, file_name)
end

#relative_pathObject



36
37
38
# File 'lib/trello-fs/attachment_builder.rb', line 36

def relative_path
  File.join(folder_path, file_name)
end

#repositoryObject



60
61
62
# File 'lib/trello-fs/attachment_builder.rb', line 60

def repository
  @card_builder.repository
end