Permalink
Browse files

Refactor the Repository class (liek a baws 😎)

  • Loading branch information...
1 parent f0af05c commit 5f06962d22ae363e5c7507cb436b1f96078c6309 @Dinduks committed Sep 14, 2012
Showing with 45 additions and 41 deletions.
  1. +1 −1 app/controllers/Application.scala
  2. +44 −40 app/models/Repository.scala
@@ -29,7 +29,7 @@ object Application extends Controller {
val repo = new Repository(Some(username), Some(repositoryName), None, None)
Async {
- repo.getCommits(githubApiUrl, githubGetRepoCommitsPath).map { repo =>
+ repo.getAllInfo(githubApiUrl, githubGetRepoCommitsPath).map { repo =>
Ok(views.html.showRepoInfo(repo))
}
}
@@ -9,52 +9,17 @@ class Repository(
val name: Option[String] = None,
val language: Option[String] = None,
val description: Option[String] = None,
- var commits: List[Commit] = List(),
+ val commits: List[Commit] = List(),
val contributors: List[Contributor] = List()
) {
- def getCommits(githubApiUrl: String, githubGetRepoCommitsPath: String): Promise[Repository] = {
+ def getAllInfo(githubApiUrl: String, githubGetRepoCommitsPath: String): Promise[Repository] = {
WS.url(githubGetRepoCommitsPath.format(githubApiUrl, owner.get, name.get)).get().map { response =>
- commits = (response.json.as[List[JsValue]]).map { commit =>
- Commit(
- (commit \ "message").asOpt[String],
- (commit \ "sha").asOpt[String],
- Some(User(
- (commit \ "author" \ "login").asOpt[String],
- (commit \ "commit" \ "author" \ "name").asOpt[String],
- (commit \ "commit" \ "author" \ "email").asOpt[String],
- (commit \ "author" \ "avatar_url").asOpt[String]
- ))
- )
- }
+ val commits = Repository.parseJsonCommits(response.json.as[List[JsValue]])
+ val contributors = Repository.computeContributors(commits)
- computeContributors
+ Repository(this.owner, this.name, this.language, this.description, commits, contributors)
}
}
-
- // TODO: Use getOrElse
- def computeContributors: Repository = {
- var contributorsMap: Map[String, Contributor] = Map()
- for (commit <- commits) {
- contributorsMap.get(commit.author.get.email.get) match {
- case Some(contributor) => {
- contributor.contributionsCounter = contributor.contributionsCounter + 1
- }
- case None => {
- val contributor = Contributor(
- (commit.author.get.username),
- (commit.author.get.name),
- (commit.author.get.email),
- (commit.author.get.avatarUrl),
- 1
- )
-
- contributorsMap += (commit.author.get.email.get -> contributor)
- }
- }
- }
-
- Repository(owner, name, language, description, commits, contributorsMap.map{ case(k, v) => (v) }.toList)
- }
}
object Repository {
@@ -81,5 +46,44 @@ object Repository {
}
}
}
+
+ def parseJsonCommits(jsonCommits: List[JsValue]): List[Commit] = {
+ jsonCommits.map { commit =>
+ Commit(
+ (commit \ "message").asOpt[String],
+ (commit \ "sha").asOpt[String],
+ Some(User(
+ (commit \ "author" \ "login").asOpt[String],
+ (commit \ "commit" \ "author" \ "name").asOpt[String],
+ (commit \ "commit" \ "author" \ "email").asOpt[String],
+ (commit \ "author" \ "avatar_url").asOpt[String]
+ ))
+ )
+ }
+ }
+
+ def computeContributors(commits: List[Commit]): List[Contributor] = {
+ var contributorsMap: Map[String, Contributor] = Map()
+ for (commit <- commits) {
+ contributorsMap.get(commit.author.get.email.get) match {
+ case Some(contributor) => {
+ contributor.contributionsCounter = contributor.contributionsCounter + 1
+ }
+ case None => {
+ val contributor = Contributor(
+ (commit.author.get.username),
+ (commit.author.get.name),
+ (commit.author.get.email),
+ (commit.author.get.avatarUrl),
+ 1
+ )
+
+ contributorsMap += (commit.author.get.email.get -> contributor)
+ }
+ }
+ }
+
+ contributorsMap.map{ case(k, v) => (v) }.toList
+ }
}

0 comments on commit 5f06962

Please sign in to comment.