Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

Commit

Permalink
iss #22: models: GithubOrg, GithubUser, ProgrammingLang, ProgrammingL…
Browse files Browse the repository at this point in the history
…angStat, Repo
  • Loading branch information
maizy committed Jun 17, 2014
1 parent 8e72440 commit 937de58
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 30 deletions.
17 changes: 17 additions & 0 deletions app/hedgehog/UrlUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package hedgehog

import play.utils.UriEncoding.encodePathSegment
/**
* Copyright (c) Nikita Kovaliov, maizy.ru, 2014
* See LICENSE.txt for details.
*/
object UrlUtils {

def urlEncodePathSegment(segment: String) {
encodePathSegment(segment, "utf-8")
}

def urlEncode(param: String) {
java.net.URLEncoder.encode(param, "utf-8")
}
}
34 changes: 34 additions & 0 deletions app/hedgehog/models/GithubAccount.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package hedgehog.models

import hedgehog.UrlUtils._

/**
* Copyright (c) Nikita Kovaliov, maizy.ru, 2014
* See LICENSE.txt for details.
*/
trait GithubAccount {

def name: String

def avatarUrl: Option[String] = None

protected def baseWebUrl: String = GITHUB_BASE_WEB_URL

def webProfileUrl: String =
s"$baseWebUrl/${urlEncodePathSegment(name)}"

def getRepoUrl(repo: Repo): String =
s"$webProfileUrl/${urlEncodePathSegment(repo.fullName)}"
}


case class GithubUser(
name: String,
override val avatarUrl: Option[String])
extends GithubAccount


case class GithubOrg(
name: String,
override val avatarUrl: Option[String])
extends GithubAccount
14 changes: 14 additions & 0 deletions app/hedgehog/models/ProgrammingLang.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package hedgehog.models

/**
* Copyright (c) Nikita Kovaliov, maizy.ru, 2014
* See LICENSE.txt for details.
*
*/
class ProgrammingLang(val code: String)


class ProgrammingLangStat(
val lang: ProgrammingLang,
val extensions: Seq[String],
val bytes: Option[Long])
37 changes: 15 additions & 22 deletions app/hedgehog/models/Repo.scala
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
package hedgehog.models

import play.api.libs.json._
import play.api.Play.current
import scala.collection.JavaConverters._
/**
* Copyright (c) Nikita Kovaliov, maizy.ru, 2013-2014
* See LICENSE.txt for details.
*/
class Repo (val name: String, val ownerName: String) {
def url = s"$GITHUB_BASE_URL/$fullName"
def fullName = s"$ownerName/$name"

//TODO
def description: Option[String] = None
def isPrivate: Option[Boolean] = None
class Repo(
val name: String,
val owner: GithubAccount,
val description: Option[Boolean] = None,
val isPrivate: Option[Boolean] = None,
val primaryLang: Option[ProgrammingLang] = None,
val langsStat: Option[Seq[ProgrammingLangStat]] = None) {
def fullName = s"${owner.name}/$name"
lazy val langsStatIndex = langsStat.map(seq => seq.map{stat => (stat.lang.code, stat)}.toMap)
def url: String = owner.getRepoUrl(this)
}


object Repo {
def getCurrentRepos: Seq[Repo] =
current.configuration
.getStringList("sources.repos")
.map(_.asScala map Repo.fromFullName)
.getOrElse(List())

def fromFullName(fullName: String): Repo =
fullName.split("/").toList match {
case ownerName :: name :: Nil => new Repo(name, ownerName)
case _ => throw new ConfigError("Bad repo name format for \""+ fullName +"\"")
}
//FIXME
def getCurrentRepos: Seq[Repo] = List()

implicit val writer = new Writes[Repo] {
def writes(r: Repo) : JsValue = {
//FIXME: as documented
Json.obj(
"name" -> r.name,
"owner" -> r.ownerName,
"url" -> r.url,
//"owner" -> r.ownerName,
//"url" -> r.url,
"full_name" -> r.fullName,
"description" -> r.description,
"is_private" -> r.isPrivate
Expand Down
2 changes: 1 addition & 1 deletion app/hedgehog/models/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ package hedgehog
*/
package object models {
val GITHUB_HOST = "github.com"
val GITHUB_BASE_URL = s"https://$GITHUB_HOST"
val GITHUB_BASE_WEB_URL = s"https://$GITHUB_HOST"
}
15 changes: 8 additions & 7 deletions docs/Models.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
* Repo
* isPrivate: Boolean
* name
* owner: GithubAccount
* url
* fullName
* description
* primaryLang: Option[ProgrammingLang]
* langsStat: Option[Map[langCode: String, ProgrammingLang]]
* langsStat: Option[Seq[ProgrammingLang]]
* langsStatIndex: Option[Map[langCode: String, ProgrammingLang]]

<a name="prog_lang"/>
## Programming Language
Expand All @@ -24,14 +26,13 @@
* bytes: Option[Long]

<a name="github_account" />
## GitHubAccount
## GithubAccount

* GitHubAccount
* GithubAccount
* name
* profileUrl
* webProfileUrl
* avatarUrl
* _private_ repoBaseUrl
* getRepoUrl(r: Repo)

* _case class_ GitHubUser
* _case class_ GItHubCorp
* _case class_ GithubUser
* _case class_ GIthubOrg

0 comments on commit 937de58

Please sign in to comment.