@@ -4,44 +4,61 @@ import org.joda.time.DateTime
4
4
import play .api .libs .functional .syntax ._
5
5
import play .api .libs .json ._
6
6
7
- case class Repository (name : String , full_name : String , description : String ,
8
- scm : String , created_on : DateTime , updated_on : DateTime ,
9
- httpsUrl : String , sshUrl : String , owner : String , size : Long ,
10
- has_issues : Boolean , is_private : Boolean , language : String )
7
+ case class Repository (name : String , full_name : String , description : String , scm : String ,
8
+ created_on : DateTime , updated_on : DateTime , owner : String , size : Long ,
9
+ has_issues : Boolean , is_private : Boolean , language : String ,
10
+ url : Seq [ RepositoryUrl ] )
11
11
12
12
object Repository {
13
13
val dateFormat = " yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZ"
14
14
implicit val jodaDateTimeReads = Reads .jodaDateReads(dateFormat)
15
15
16
16
implicit val reader : Reads [Repository ] = {
17
- val url1 = (__ \ " links" \ " clone" )(0 ).read[Url ]
18
- val url2 = (__ \ " links" \ " clone" )(1 ).read[Url ]
19
-
20
- val httpUrl = url1.filter(_.name == " https" ).orElse(url2).map(_.href)
21
- val sshUrl = url2.filter(_.name == " ssh" ).orElse(url1).map(_.href)
22
-
23
17
((__ \ " name" ).read[String ] and
24
18
(__ \ " full_name" ).read[String ] and
25
19
(__ \ " description" ).read[String ] and
26
20
(__ \ " scm" ).read[String ] and
27
21
(__ \ " created_on" ).read[DateTime ] and
28
22
(__ \ " updated_on" ).read[DateTime ] and
29
- httpUrl and
30
- sshUrl and
31
23
(__ \ " owner" \ " username" ).read[String ] and
32
24
(__ \ " size" ).read[Long ] and
33
25
(__ \ " has_issues" ).read[Boolean ] and
34
26
(__ \ " is_private" ).read[Boolean ] and
35
- (__ \ " language" ).read[String ]
27
+ (__ \ " language" ).read[String ] and
28
+ (__ \ " links" ).read[Map [String , JsValue ]].map(parseLinks)
36
29
)(Repository .apply _)
37
30
}
31
+
32
+ private def parseLinks (links : Map [String , JsValue ]): Seq [RepositoryUrl ] = {
33
+ links.flatMap {
34
+ case (linkName, linkMap) =>
35
+
36
+ val simpleLinks = for {
37
+ ref <- linkMap.asOpt[Map [String , String ]]
38
+ urlType <- RepositoryUrlType .find(linkName)
39
+ linkUrl <- ref.get(" href" )
40
+ } yield RepositoryUrl (urlType, linkUrl)
41
+
42
+ val complexLinks = for {
43
+ refs <- linkMap.asOpt[Seq [Map [String , String ]]].toSeq
44
+ ref <- refs
45
+ linkName <- ref.get(" name" )
46
+ urlType <- RepositoryUrlType .find(linkName)
47
+ linkUrl <- ref.get(" href" )
48
+ } yield RepositoryUrl (urlType, linkUrl)
49
+
50
+ simpleLinks ++ complexLinks
51
+ }.toSeq
52
+ }
38
53
}
39
54
40
- case class Url (name : String , href : String )
55
+ object RepositoryUrlType extends Enumeration {
56
+ val Https = Value (" https" )
57
+ val Ssh = Value (" ssh" )
41
58
42
- object Url {
43
- implicit val reader : Reads [Url ] = (
44
- (__ \ " name" ).read[String ] and
45
- (__ \ " href" ).read[String ]
46
- )(Url .apply _)
59
+ def find (urlType : String ): Option [Value ] = {
60
+ values.find(_.toString == urlType)
61
+ }
47
62
}
63
+
64
+ case class RepositoryUrl (urlType : RepositoryUrlType .Value , link : String )
0 commit comments