Skip to content

Commit

Permalink
#36 Implemented searchRepositories v1
Browse files Browse the repository at this point in the history
  • Loading branch information
sukeshni committed May 21, 2015
1 parent 7839654 commit a4e89b4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/scala/codecheck/github/api/GitHubAPI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class GitHubAPI(token: String, client: AsyncHttpClient, tokenType: String = "tok
with MilestoneOp
with WebhookOp
with CollaboratorOp
with SearchOp
{

private val endpoint = "https://api.github.com"
Expand Down
25 changes: 25 additions & 0 deletions src/main/scala/codecheck/github/models/Search.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package codecheck.github.models

import org.json4s.JValue
import codecheck.github.models.SortDirection

sealed abstract class SearchSort(val name: String) {
override def toString = name
}
object SearchSort {
case object stars extends SearchSort("stars")
case object forks extends SearchSort("forks")
case object updated extends SearchSort("updated")
}

case class SearchInput (
q: String,
sort: Option[SearchSort] = None,
order: SortDirection = SortDirection.desc
) extends AbstractInput

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 = Repository(value \ "items")
}
22 changes: 22 additions & 0 deletions src/main/scala/codecheck/github/operations/SearchOp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package codecheck.github.operations

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

import codecheck.github.api.GitHubAPI
import codecheck.github.models.SearchInput
import codecheck.github.models.SearchRepositoryResult

trait SearchOp {
self: GitHubAPI =>

def searchRepositories(input: SearchInput): Future[Option[SearchRepositoryResult]] = {
val path = s"/search/repositories?q=${input.q}&sort=${input.sort}&order=${input.order}"
exec("GET", path ).map { res =>
res.statusCode match {
case 200 => Some(SearchRepositoryResult(res.body))
case 404 => None
}
}
}
}
21 changes: 21 additions & 0 deletions src/test/scala/SearchOpSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import org.scalatest.path.FunSpec
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import codecheck.github.models.SortDirection
import codecheck.github.models.SearchInput
import codecheck.github.models.SearchSort
import codecheck.github.models.SearchRepositoryResult

class SearchOpSpec extends FunSpec
with Constants
{
val input = SearchInput("tetris",sort=Some(SearchSort.stars),order=SortDirection.desc)
describe("searchRepositories") {
it("with valid SearchInput should succeed") {
Await.result(api.searchRepositories(input), TIMEOUT).map { res =>
assert(res.total_count >= 1)
println("RESULT" + res)
}
}
}
}

0 comments on commit a4e89b4

Please sign in to comment.