idFromUrl static method

String idFromUrl(
  1. dynamic url
)

Retrieve a submission ID from a given URL.

Note: when url is a String, it must end with a trailing '/'. This is a bug and will be fixed eventually.

Implementation

static String idFromUrl(/*String, Uri*/ url) {
  Uri uri;
  if (url is String) {
    uri = Uri.parse(url);
  } else if (url is Uri) {
    uri = url;
  } else {
    throw DRAWArgumentError('idFromUrl expects either a String or Uri as'
        ' input');
  }
  var submissionId = '';
  final parts = uri.path.split('/');
  final commentsIndex = parts.indexOf('comments');
  if (commentsIndex == -1) {
    submissionId = parts[parts.length - 2];
  } else {
    submissionId = parts[commentsIndex + 1];
  }
  return submissionId;
}