Permalink
Browse files

Check that user name is valid

  • Loading branch information...
1 parent 9397954 commit ccd8ce97ebe33e63c3b919122702dbf97c2ddc11 @bblaine committed Dec 9, 2011
Showing with 33 additions and 21 deletions.
  1. +33 −21 src/main/scala/Vain.scala
@@ -7,6 +7,8 @@ import scala.collection.mutable.ListBuffer
case class Repository(has_downloads: Boolean, watchers: Int, url: String, has_wiki: Boolean, fork: Boolean, forks: Int, created_at: Option[Date], size: Int, private_repo: Option[Boolean], open_issues: Int, pushed_at: Option[Date], name: String, owner: String, has_issues: Boolean)
case class Repositories(repos: List[Repository])
+case class User(gravatar_id: String, company: String, name: String, created_at: Option[Date], location: String, public_repo_count: Int, public_gist_count: Int, blog: String, following_count: Int, id: Int, usertype: String, permission: String, followers_count: Int, login: String, email: String)
+
object Main {
def fork(fork: Int): String = fork match {
case 0 => "(FORK)"
@@ -18,36 +20,46 @@ object Main {
def main(args: Array[String]) {
val github = new Github()
val name = args(0)
- val user = github.getUser(name)
+ val user = github.getUser(name)
- val login = (user \\ "login") match {
- case login: JField => login.value.values
- case _ => Nil
+ val valid = user match {
+ case validUser: JField => true
+ case invalidUser: JObject => false
+ case _ => false
}
- val followers = (user \\ "followers_count") match {
- case followers: JField => followers.value.values
- case _ => Nil
- }
+ if(valid){
+ val login = (user \\ "login") match {
+ case login: JField => login.value.values
+ case _ => Nil
+ }
- val repo_count = (user \\ "public_repo_count") match {
- case repo_count: JField => repo_count.value.values
- case _ => Nil
- }
+ val followers = (user \\ "followers_count") match {
+ case followers: JField => followers.value.values
+ case _ => Nil
+ }
- //TODO - Only get repos if user valid.
- val jsonRepos = github.getRepos(name, repo_count.toString.toInt)
+ val repo_count = (user \\ "public_repo_count") match {
+ case repo_count: JField => repo_count.value.values
+ case _ => Nil
+ }
- System.out.println(login + " - " + followers + " followers - " + repo_count + " public repositories")
+ //TODO - Only get repos if user valid.
+ val jsonRepos = github.getRepos(name, repo_count.toString.toInt)
- var listRepos = ListBuffer[Repository]()
+ System.out.println(login + " - " + followers + " followers - " + repo_count + " public repositories")
- for (i <- 0 until repo_count.toString.toInt) {
- listRepos += (jsonRepos \\ "repositories")(i).extract[Repository]
- }
+ var listRepos = ListBuffer[Repository]()
+
+ for (i <- 0 until repo_count.toString.toInt) {
+ listRepos += (jsonRepos \\ "repositories")(i).extract[Repository]
+ }
- val orderedrepos = listRepos.sortBy(_.watchers).reverse
+ val orderedrepos = listRepos.sortBy(_.watchers).reverse
- orderedrepos.foreach(repo => printf("%-25s %12d %12s %12d %12s %6s\n", repo.name, repo.watchers, " watchers", repo.forks, " forks", fork(repo.forks)))
+ orderedrepos.foreach(repo => printf("%-25s %12d %12s %12d %12s %6s\n", repo.name, repo.watchers, " watchers", repo.forks, " forks", fork(repo.forks)))
+ }else{
+ println("Invalid user")
+ }
}
}

0 comments on commit ccd8ce9

Please sign in to comment.