Permalink
Browse files

(Big) code refactor

  • Loading branch information...
1 parent fcce1ac commit e20f169d16b1452cd319d4a1fd5a65d7d67c1c27 @Dinduks committed Sep 14, 2012
View
36 app/controllers/Application.scala
@@ -27,42 +27,12 @@ object Application extends Controller {
val githubApiUrl = Play.current.configuration.getString("github.apiUrl").get
val githubGetRepoCommitsPath = Play.current.configuration.getString("github.getRepoCommitsPath").get
+ val repo = new Repository(Some(username), Some(repositoryName), None, None)
Async {
- WS.url(githubGetRepoCommitsPath.format(githubApiUrl, username, repositoryName)).get().map { response =>
- val commits = (response.json)
- val commitsCount = commits.as[List[JsValue]].length
- val contributorsList = getSortedCommittersList(commits)
- Ok(views.html.showRepoInfo(username, repositoryName, contributorsList, commitsCount))
+ repo.getCommits(githubApiUrl, githubGetRepoCommitsPath).map { repo =>
+ Ok(views.html.showRepoInfo(repo))
}
}
}
-
- def getSortedCommittersList(commits: JsValue): Map[String, Contributor] = {
- val commitsList = commits.as[List[JsValue]]
- var committersList: Map[String, Contributor] = Map()
-
- for(commit <- commitsList) {
- committersList.get((commit \ "commit" \ "author" \ "email").toString) match {
- case Some(committer) => {
- committer.contributionsCounter = committer.contributionsCounter + 1
- }
- case None => {
- val commitAuthor = commit \ "commit" \ "author"
- val author = commit \ "author"
- val contributor = Contributor(
- (author \ "login").asOpt[String],
- (commitAuthor \ "name").asOpt[String],
- (commitAuthor \ "email").asOpt[String],
- (author \ "avatar_url").asOpt[String],
- 1
- )
-
- committersList += ((commitAuthor \ "email").toString -> contributor)
- }
- }
- }
-
- committersList
- }
}
View
17 app/models/Commit.scala
@@ -1,3 +1,18 @@
package models
-class Commit
+class Commit(
+ val message: Option[String] = None,
+ val sha: Option[String] = None,
+ val author: Option[User] = None
+)
+
+object Commit {
+ def apply(
+ message: Option[String] = None,
+ sha: Option[String] = None,
+ author: Option[User] = None
+ ) = {
+ new Commit(message, sha, author)
+ }
+}
+
View
2 app/models/Contributor.scala
@@ -36,7 +36,7 @@ object Contributor {
name,
email,
url,
- (avatarUrl),
+ avatarUrl,
contributionsCounter
)
}
View
76 app/models/Repository.scala
@@ -5,32 +5,72 @@ import play.api.libs.json._
import play.api.libs.concurrent._
class Repository(
- val owner: Option[String] = None,
- val name: Option[String] = None,
- val language: Option[String] = None,
- val description: Option[String] = None,
- val commits: Option[Commit] = None,
- val contributors: Option[Contributor] = None
-)
+ val owner: Option[String] = None,
+ val name: Option[String] = None,
+ val language: Option[String] = None,
+ val description: Option[String] = None,
+ var commits: List[Commit] = List(),
+ val contributors: List[Contributor] = List()
+) {
+ def getCommits(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]
+ ))
+ )
+ }
+
+ computeContributors
+ }
+ }
+
+ // 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 {
def apply(
- owner: Option[String] = None,
- name: Option[String] = None,
- language: Option[String] = None,
- description: Option[String] = None
+ owner: Option[String] = None,
+ name: Option[String] = None,
+ language: Option[String] = None,
+ description: Option[String] = None,
+ commits: List[Commit] = List(),
+ contributors: List[Contributor] = List()
) = {
- new Repository(owner, name, language, description, None, None)
+ new Repository(owner, name, language, description, commits, contributors)
}
def searchRepo(keyword: String, githubApiUrl: String, githubSearchReposPath: String): Promise[List[Repository]] = {
WS.url(githubSearchReposPath.format(githubApiUrl, keyword)).get().map { response =>
- val json = response.json \ "repositories"
- val owner = json \ "owner"
- val name = json \ "name"
- val language = json \ "language"
- val description = json \ "description"
-
(response.json \ "repositories").as[List[JsValue]].map { repo =>
Repository(
(repo \ "owner").asOpt[String],
View
31 app/models/User.scala
@@ -1,10 +1,29 @@
package models
-case class User(
- username: Option[String] = None,
- name: Option[String] = None,
- email: Option[String] = None,
- url: Option[String] = None,
- avatarUrl: Option[String] = None
+import revontulet._
+
+class User(
+ val username: Option[String] = None,
+ val name: Option[String] = None,
+ val email: Option[String] = None,
+ val url: Option[String] = None,
+ val avatarUrl: Option[String] = None
)
+object User {
+ def apply(
+ username: Option[String] = None,
+ name: Option[String] = None,
+ email: Option[String] = None,
+ url: Option[String] = None,
+ avatarUrl: Option[String] = None
+ ) = {
+ val url = username match {
+ case None => None
+ case Some(username) => Some("https://github.com/%s" format Util.trim(username, '"'))
+ }
+
+ new User(username, name, email, url, avatarUrl)
+ }
+}
+
View
2 app/views/helper/contributorInfo.scala.html
@@ -4,7 +4,7 @@
@if(contributor.url) { <a href="@contributor.url.getOrElse("https://github.com")"> }
- @Util.trim(contributor.username.getOrElse(contributor.name.get), '"')
+ @Util.trim(contributor.username.getOrElse(contributor.name.get.toString), '"')
-
@contributor.contributionsCounter commit among @commitsCount
View
11 app/views/showRepoInfo.scala.html
@@ -1,17 +1,14 @@
-@(username : String,
- repositoryName : String,
- contributorsList: Map[String, Contributor],
- commitsCount: Int)
+@(repo: Repository)
@import views.html.helper._
-@main(username + "/" + repositoryName + " - Repository info") {
+@main(repo.owner + "/" + repo.name + " - Repository info") {
<h2>Contributors</h2>
<div>
<ul>
- @for((username, contributor) <- contributorsList) {
+ @for(contributor <- repo.contributors) {
<li>
- @contributorInfo(contributor, commitsCount)
+ @contributorInfo(contributor, repo.commits.length)
</li>
}
</ul>

0 comments on commit e20f169

Please sign in to comment.