forked from code-check/github-api-scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.scala
133 lines (105 loc) · 3.96 KB
/
Search.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package codecheck.github.models
import org.json4s.JValue
import org.json4s.JArray
sealed trait SearchSort {
def name: String
override def toString = name
}
sealed abstract class SearchRepositorySort(val name: String) extends SearchSort
object SearchRepositorySort {
case object stars extends SearchRepositorySort("stars")
case object forks extends SearchRepositorySort("forks")
case object updated extends SearchRepositorySort("updated")
val values = Array(stars, forks, updated)
def fromString(str: String) = values.filter(_.name == str).head
}
sealed abstract class SearchCodeSort(val name: String) extends SearchSort
object SearchCodeSort {
case object indexed extends SearchCodeSort("indexed")
val values = Array(indexed)
def fromString(str: String) = values.filter(_.name == str).head
}
sealed abstract class SearchIssueSort(val name: String) extends SearchSort
object SearchIssueSort {
case object created extends SearchIssueSort("created")
case object updated extends SearchIssueSort("updated")
case object comments extends SearchIssueSort("comments")
val values = Array(created, updated, comments)
def fromString(str: String) = values.filter(_.name == str).head
}
sealed abstract class SearchUserSort(val name: String) extends SearchSort
object SearchUserSort {
case object followers extends SearchUserSort("followers")
case object repositories extends SearchUserSort("repositories")
case object joined extends SearchUserSort("joined")
val values = Array(followers, repositories, joined)
def fromString(str: String) = values.filter(_.name == str).head
}
sealed trait SearchInput extends AbstractInput {
def q: String
def sort: Option[SearchSort]
def order: SortDirection
def query = s"?q=$q" + sort.map(sortBy => s"&sort=$sortBy&order=$order").getOrElse("")
}
case class SearchRepositoryInput (
val q: String,
val sort: Option[SearchRepositorySort] = None,
val order: SortDirection = SortDirection.desc
) extends SearchInput
case class SearchRepositoryResult(value: JValue) extends AbstractJson(value) {
def total_count: Long = get("total_count").toLong
def incomplete_results: Boolean = boolean("incomplete_results")
lazy val items = (value \ "items") match {
case JArray(arr) => arr.map(Repository(_))
case _ => Nil
}
}
case class SearchCodeInput (
q: String,
sort: Option[SearchCodeSort] = None,
order: SortDirection = SortDirection.desc
) extends SearchInput
case class SearchCodeItem(value: JValue) extends AbstractJson(value) {
def name: String = get("name")
def path: String = get("path")
def sha: String = get("sha")
def url: String = get("url")
def git_url: String = get("git_url")
def html_url: String = get("html_url")
def score: Double = get("score").toDouble
lazy val repository = Repository(value \ "repository")
}
case class SearchCodeResult(value: JValue) extends AbstractJson(value) {
def total_count: Long = get("total_count").toLong
def incomplete_results: Boolean = boolean("incomplete_results")
lazy val items = (value \ "items") match {
case JArray(arr) => arr.map(SearchCodeItem(_))
case _ => Nil
}
}
case class SearchIssueInput (
q: String,
sort: Option[SearchIssueSort] = None,
order: SortDirection = SortDirection.desc
) extends SearchInput
case class SearchIssueResult(value: JValue) extends AbstractJson(value) {
def total_count: Long = get("total_count").toLong
def incomplete_results: Boolean = boolean("incomplete_results")
lazy val items = (value \ "items") match {
case JArray(arr) => arr.map(Issue(_))
case _ => Nil
}
}
case class SearchUserInput (
q: String,
sort: Option[SearchUserSort] = None,
order: SortDirection = SortDirection.desc
) extends SearchInput
case class SearchUserResult(value: JValue) extends AbstractJson(value) {
def total_count: Long = get("total_count").toLong
def incomplete_results: Boolean = boolean("incomplete_results")
lazy val items = (value \ "items") match {
case JArray(arr) => arr.map(User(_))
case _ => Nil
}
}