Class: Lionel::Export

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/lionel/export.rb

Defined Under Namespace

Classes: CardMap

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Configurable

#configuration, #configured?, included, #save_configuration

Constructor Details

#initialize(options = {}) ⇒ Export

Returns a new instance of Export.



17
18
19
# File 'lib/lionel/export.rb', line 17

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/lionel/export.rb', line 5

def options
  @options
end

Class Method Details

.builderObject



13
14
15
# File 'lib/lionel/export.rb', line 13

def self.builder
  @builder || ExportBuilder.default
end

.builder=(builder) ⇒ Object



9
10
11
# File 'lib/lionel/export.rb', line 9

def self.builder=(builder)
  @builder = builder
end

Instance Method Details

#authenticateObject



116
117
118
119
120
121
# File 'lib/lionel/export.rb', line 116

def authenticate
  return if @authenticated
  authenticate_trello
  authenticate_google
  @authenticated
end

#authenticate_googleObject



128
129
130
131
# File 'lib/lionel/export.rb', line 128

def authenticate_google
  google_session
  @worksheet = Lionel::ProxyWorksheet.new(spreadsheet.worksheets[0])
end

#authenticate_trelloObject



123
124
125
126
# File 'lib/lionel/export.rb', line 123

def authenticate_trello
  trello_session.configure
  @board = Trello::Board.find(trello_board_id)
end

#boardObject



28
29
30
# File 'lib/lionel/export.rb', line 28

def board
  @board ||= Trello::Board.find(trello_board_id)
end

#builderObject



100
101
102
# File 'lib/lionel/export.rb', line 100

def builder
  self.class.builder
end

#card_column_rowsObject



112
113
114
# File 'lib/lionel/export.rb', line 112

def card_column_rows
  @card_column_rows ||= {}
end

#card_columns(card) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/lionel/export.rb', line 104

def card_columns(card)
  card_column_rows[card.id] ||= {}.tap do |columns|
    builder.columns.each do |col_name, block|
      columns[col_name] = card.instance_exec(self, &block)
    end
  end
end

#card_mapObject



88
89
90
# File 'lib/lionel/export.rb', line 88

def card_map
  @card_map ||= CardMap.new(cards, worksheet)
end

#cardsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lionel/export.rb', line 32

def cards
  # iterate over active lists rather
  # than retrieving all historical cards;
  # trello api returns association proxy
  # that does not respond to "flatten"
  @cards ||= begin
    case options.fetch('filter', 'open-lists')
    when 'open-cards'
      retrieve_open_cards
    when 'open-lists'
      retrieve_open_cards_in_open_lists
    end.map { |c| Lionel::ProxyCard.new(c) }
  end
end

#dataObject



21
22
23
24
25
26
# File 'lib/lionel/export.rb', line 21

def data
  {
    trello_board_id: trello_board_id,
    google_doc_id: google_doc_id
  }
end

#downloadObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/lionel/export.rb', line 68

def download
  raise_missing_builder_error unless builder

  Lionel.logger.info "Exporting trello board '#{board.name}' (#{trello_board_id}) to " + "google doc '#{spreadsheet.title}' (#{google_doc_id})"

  card_map.each do |row, card|
    begin
      Timeout.timeout(5) do
        row_data = card_columns(card).map do |col, value|
          worksheet[col,row] = value
        end.join(" | ")
        Lionel.logger.info "row[#{row}]: " + row_data
      end
    rescue Timeout::Error, Trello::Error => e
      Lionel.logger.warn e.inspect
      Lionel.logger.warn card.inspect
    end
  end
end

#google_sessionObject



133
134
135
# File 'lib/lionel/export.rb', line 133

def google_session
  @google_session ||= GoogleDrive.(configuration.google_token)
end

#processObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lionel/export.rb', line 55

def process
  download

  if options['print']
    Lionel.logger.info "DRY RUN..."
    Lionel.logger.info "Results were not uploaded to Google Drive"
  else
    Lionel.logger.info "Uploading..."
    upload
    Lionel.logger.info "Done!"
  end
end

#raise_missing_builder_errorObject



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/lionel/export.rb', line 155

def raise_missing_builder_error
  message = <<-ERROR.gsub(/^ {6}/, '')
    The export is not configured. Example:

    Lionel.export do
      A { id }
      B { name }
      C { url }
    end
  ERROR
  raise MissingBuilderError.new(message)
end

#retrieve_open_cardsObject



141
142
143
# File 'lib/lionel/export.rb', line 141

def retrieve_open_cards
  board.cards(filter: :open)
end

#retrieve_open_cards_in_open_listsObject



145
146
147
148
149
150
151
152
153
# File 'lib/lionel/export.rb', line 145

def retrieve_open_cards_in_open_lists
  [].tap do |c|
    board.lists(filter: :open).each do |list|
      list.cards(filter: :open).each do |card|
        c << card
      end
    end
  end
end

#rowsObject



96
97
98
# File 'lib/lionel/export.rb', line 96

def rows
  worksheet.rows
end

#spreadsheetObject



47
48
49
# File 'lib/lionel/export.rb', line 47

def spreadsheet
  @spreadsheet ||= google_session.spreadsheet_by_key(google_doc_id)
end

#trello_sessionObject



137
138
139
# File 'lib/lionel/export.rb', line 137

def trello_session
  @trello_session ||= TrelloAuthentication.new
end

#uploadObject



92
93
94
# File 'lib/lionel/export.rb', line 92

def upload
  worksheet.save
end

#worksheetObject



51
52
53
# File 'lib/lionel/export.rb', line 51

def worksheet
  @worksheet ||= get_worksheet
end