Sha256: b958c8f6c929f10253c3831d31cc518ecfc4a531ce28ddd4d9723bbaa8b96432

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

module Trellodon
  class Executor
    BOARD_ID_REGEX = /\/b\/([^\/]+)\//
    private_constant :BOARD_ID_REGEX

    def initialize(opts)
      @api_key = opts.fetch(:api_key)
      @api_token = opts.fetch(:api_token)
      @formatter = opts.fetch(:formatter)
      @logger = opts.fetch(:logger) { Config.logger }
      @scheduler = opts.fetch(:scheduler)
      check_credentials!

      @client = Client.new(api_key: @api_key, api_token: @api_token)
    end

    def download(board_pointer)
      extract_board_id(board_pointer)

      startup_time = Time.now
      logger.debug "Fetching board 🚀️️"
      board = scheduler.post do
        client.fetch_board(board_id).tap { formatter.board_added(_1) }
      end.value

      logger.debug "Fetching #{board.card_ids.size} cards from the board with comments and attachments 🐢"
      board.card_ids.map do |card_id|
        scheduler.post do
          client.fetch_card(card_id).tap { formatter.card_added(_1) }
        end
      end.map(&:value)

      formatter.finish
      logger.debug "All Trello API requests finished in #{(Time.now - startup_time).to_i} seconds ⌛"
    end

    private

    attr_reader :client, :board_id, :logger, :scheduler, :formatter

    def check_credentials!
      return if @api_key.to_s.size.positive? && @api_token.to_s.size.positive?

      raise ArgumentError, "Missing credentials. Please fill out both api_key, api_token first."
    end

    def extract_board_id(board_pointer)
      @board_id = if URI::DEFAULT_PARSER.make_regexp.match?(board_pointer)
        parse_board_url(board_pointer)
      else
        board_pointer
      end
    end

    def parse_board_url(board_url)
      rel_path = URI.parse(board_url).request_uri
      match_data = rel_path.match(BOARD_ID_REGEX)
      raise ArgumentError, "Wrong trello board url" if match_data.nil? || match_data.size != 2

      match_data[1]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trellodon-0.4.0 lib/trellodon/executor.rb