class TrelloFs::TrelloApi

Public Class Methods

new(repository) click to toggle source
# File lib/trello-fs/trello_api.rb, line 6
def initialize(repository)
  @repository = repository
  @boards = {}
end

Public Instance Methods

board(board_id) click to toggle source
# File lib/trello-fs/trello_api.rb, line 17
def board(board_id)
  @boards[board_id] ||= new_board(board_id)
end
boards() click to toggle source
# File lib/trello-fs/trello_api.rb, line 11
def boards
  @repository.board_ids.map do |board_id|
    board(board_id)
  end
end
download_board_json(board_id) click to toggle source
# File lib/trello-fs/trello_api.rb, line 93
def download_board_json(board_id)
  url = "https://api.trello.com/1/boards/#{board_id}?key=#{@repository.developer_public_key}&token=#{@repository.member_token}&cards=open&lists=open&labels=all&card_checklists=all&card_attachments=true&organization=true&labels_limit=999"
  JSON.parse(open(url).read)
end
new_board(board_id) click to toggle source
# File lib/trello-fs/trello_api.rb, line 21
def new_board(board_id)
  json = download_board_json(board_id)
  board = OpenStruct.new(name: json['name'],
                         desc: json['desc'],
                         id: json['id'],
                         url: json['url'])
  lists = {}

  board.organization_name = json['organization']['displayName'] if json['organization']

  board.labels = json['labels'].
    select {|lbl| !lbl['name'].empty? }.
    map do |label|
      @repository.labels[label['name']] ||= OpenStruct.new(name: label['name'],
                                                           id: label['id'],
                                                           cards: [])
    end

  json['lists'].each do |list|
    lists[list['id']] = OpenStruct.new(name: list['name'],
                                       id: list['id'],
                                       board: board,
                                       cards: [])
  end

  cards = json['cards'].
    select {|card| lists[card['idList']] }.
    map do |card|
    list = lists[card['idList']]
    c = OpenStruct.new(id: card['id'],
                       name: card['name'],
                       desc: card['desc'],
                       url: card['url'],
                       short_link: card['shortLink'],
                       list: list)
    list.cards << c

    c.labels = card['labels'].
      select {|lbl| @repository.labels[lbl['name']] }.
      map do |label|
      label = @repository.labels[label['name']]
      label.cards << c
      label
    end

    c.checklists = card['checklists'].map do |checklist|
      OpenStruct.new(
        name: checklist['name'],
        items: checklist['checkItems'].map do |item|
          OpenStruct.new(name: item['name'], state: item['state'])
        end
      )
    end

    c.attachments = card['attachments'].map do |attachment|
      a = OpenStruct.new(name: attachment['name'],
                         url: attachment['url'],
                         id: attachment['id'],
                         card: c)
      @repository.attachments << a
      a
    end

    @repository.cards[card['shortLink']] = c
    c
  end

  board.lists = lists.values

  board
end