lib/trello_helper.rb
##
# Return the JSON backup of +board+
Return the JSON backup of +board+
Return the JSON backup of + board +
def dump_board_json(board)
    $stderr.puts("Backing up board #{board.name}...")
    board = board(board) unless board.respond_to? :id
    board_json_url = board.url.gsub(/\/[^\/]+$/, '.json')
    board_json_other_url = "#{board.url}.json"
    api_call_uri = Addressable::URI.parse("https://trello.com/1/boards/#{board.id}")
    api_call_uri.query_values = { fields: 'all',
                                  actions: 'all',
                                  actions_limit: '1000',
                                  action_fields: 'all',
                                  cards: 'all',
                                  card_fields: 'all',
                                  card_attachments: 'true',
                                  labels: 'all',
                                  lists: 'all',
                                  list_fields: 'all',
                                  members: 'all',
                                  member_fields: 'all',
                                  checklists: 'all',
                                  checklist_fields: 'all',
                                  organization: 'false' }
    i = 0
    request_timeout = 30
    while true
      request = Trello::Request.new :get, board_json_url, {}, nil
      response = nil
      begin
        response = long_request_execute(Trello.auth_policy.authorize(request), request_timeout)
        return response.body if response.code == 200
      rescue RestClient::RequestTimeout => e
        err_msg = "Error with dump_board_json backing up board '#{board.name}' at URL #{request.uri}: " + (e.http_code.nil? ? "HTTP Timeout?" : "HTTP response code: #{e.http_code}, response body: #{e.http_body}")
        $stderr.puts err_msg
      end
      request = Trello::Request.new :get, board_json_other_url, {}, nil
      begin
        response = long_request_execute(Trello.auth_policy.authorize(request), request_timeout)
        return response.body if response.code == 200
      rescue RestClient::RequestTimeout => e
        err_msg = "Error with dump_board_json backing up board '#{board.name}' at alternate URL #{request.uri}: " + (e.http_code.nil? ? "HTTP Timeout?" : "HTTP response code: #{e.http_code}, response body: #{e.http_body}")
        $stderr.puts err_msg
      end
      $stderr.puts "Retrying with API-based backup URL to work around failed request."
      request = Trello::Request.new :get, api_call_uri, {}, nil
      begin
        response = long_request_execute(Trello.auth_policy.authorize(request), request_timeout)
        return response.body if response.code == 200
      rescue RestClient::RequestTimeout => e
        err_msg = "Error with dump_board_json backing up board '#{board.name}' using all URL endpoints and API call: " + (e.http_code.nil? ? "HTTP Timeout?" : "HTTP response code: #{e.http_code}, response body: #{e.http_body}")
      end
      if i >= DEFAULT_RETRIES
        raise err_msg
      end
      $stderr.puts err_msg
      retry_sleep i
      request_timeout += 15
      i += 1
    end
  end
def dump_board_json ( board ) $stderr . puts ( "Backing up board #{board.name}..." ) board = board ( board ) unless board . respond_to? :id board_json_url = board . url . gsub ( / \/ [^ \/ ]+$ / , '.json' ) board_json_other_url = "#{board.url}.json" api_call_uri = Addressable :: URI . parse ( "https://trello.com/1/boards/#{board.id}" ) api_call_uri . query_values = { fields : 'all' , actions : 'all' , actions_limit : '1000' , action_fields : 'all' , cards : 'all' , card_fields : 'all' , card_attachments : 'true' , labels : 'all' , lists : 'all' , list_fields : 'all' , members : 'all' , member_fields : 'all' , checklists : 'all' , checklist_fields : 'all' , organization : 'false' } i = 0 request_timeout = 30 while true request = Trello :: Request . new :get , board_json_url , { } , nil response = nil begin response = long_request_execute ( Trello . auth_policy . authorize ( request ) , request_timeout ) return response . body if response . code == 200 rescue RestClient :: RequestTimeout => e err_msg = "Error with dump_board_json backing up board '#{board.name}' at URL #{request.uri}: " + ( e . http_code . nil? ? "HTTP Timeout?" : "HTTP response code: #{e.http_code}, response body: #{e.http_body}" ) $stderr . puts err_msg end request = Trello :: Request . new :get , board_json_other_url , { } , nil begin response = long_request_execute ( Trello . auth_policy . authorize ( request ) , request_timeout ) return response . body if response . code == 200 rescue RestClient :: RequestTimeout => e err_msg = "Error with dump_board_json backing up board '#{board.name}' at alternate URL #{request.uri}: " + ( e . http_code . nil? ? "HTTP Timeout?" : "HTTP response code: #{e.http_code}, response body: #{e.http_body}" ) $stderr . puts err_msg end $stderr . puts "Retrying with API-based backup URL to work around failed request." request = Trello :: Request . new :get , api_call_uri , { } , nil begin response = long_request_execute ( Trello . auth_policy . authorize ( request ) , request_timeout ) return response . body if response . code == 200 rescue RestClient :: RequestTimeout => e err_msg = "Error with dump_board_json backing up board '#{board.name}' using all URL endpoints and API call: " + ( e . http_code . nil? ? "HTTP Timeout?" : "HTTP response code: #{e.http_code}, response body: #{e.http_body}" ) end if i >= DEFAULT_RETRIES raise err_msg end $stderr . puts err_msg retry_sleep i request_timeout += 15 i += 1 end end
Return the JSON backup of +board+
Return the JSON backup of + board +
# This can take a Trello::Board or a board ID # Yes, we have to try two different URL schemes because one works # for some boards, one works for others, and then we STILL get to # fall back to the API call if neither work. :/ # API request to pull down the same content as the export URL, but limited to 100 actions
board