Skip to content

Instantly share code, notes, and snippets.

@xnull
Last active June 10, 2016 13:39
Show Gist options
  • Save xnull/21243a045360bb05de0b to your computer and use it in GitHub Desktop.
Save xnull/21243a045360bb05de0b to your computer and use it in GitHub Desktop.
Scala example
package test
import test.common._
import scala.concurrent.Future
object Github extends Github(new HttpClient()) {
private val SearchApiUrl = "https://api.github.com/search/repositories"
}
class Github(httpClient: HttpClient) {
private val log = SimpleLogger(this)
implicit private val ec = ExecutionContextManager.Ec
def search(query: GithubQuery): Future[GithubDto] = {
log.debug(s"Github search: $query")
httpClient.sendRequest(buildUrl(query)) map { response =>
JsonUtil.fromJsonWithUnderscore(response, classOf[GithubDto])
}
}
private def buildUrl(query: GithubQuery): String = {
s"""${Github.SearchApiUrl}?
|q=${query.criteria}
|&page=${query.paging.page}&per_page=${query.paging.perPage}
|&sort=${query.sort.sortType}
|&order=${query.order.order}
""".stripMargin.replaceAll("\n", "").trim
}
}
case class GithubDto(items: List[GithubRepositoryDto])
case class GithubRepositoryDto
(
id: Int,
name: String,
htmlUrl: String,
description: String,
homepage: String,
language: String
)
case class GithubQuery
(
criteria: String,
paging: GithubQueryPaging = GithubQueryPaging(0, 10),
sort: GithubQuerySort = Stars,
order: GithubQueryOrder = Desc
)
case class GithubQueryPaging(page: Int, perPage: Int)
sealed case class GithubQuerySort(sortType: String)
object GithubQuerySort {
object Stars extends GithubQuerySort("stars")
object Forks extends GithubQuerySort("forks")
object Updated extends GithubQuerySort("updated")
}
sealed case class GithubQueryOrder(order: String)
object GithubQueryOrder {
object Asc extends GithubQueryOrder("asc")
object Desc extends GithubQueryOrder("desc")
}
package test.twitter
import test.common.{ApplicationConfig, ExecutionContextManager, SimpleLogger}
import org.springframework.social.twitter.api.impl.TwitterTemplate
import scala.collection.JavaConversions._
import scala.concurrent.Future
/**
* https://dev.twitter.com/rest/public/search
*/
object Twitter extends Twitter(
template = new TwitterTemplate(ApplicationConfig.twitterApiKey, ApplicationConfig.twitterApiSecret)
)
class Twitter(template: TwitterTemplate) {
private val log = SimpleLogger(this)
implicit private val ec = ExecutionContextManager.Ec
def search(query: String): Future[List[TweetDto]] = {
Future {
log.debug(s"Search: $query")
val search = template.searchOperations().search(query)
search.getTweets.toList.map { tweet =>
TweetDto(tweet.getId, tweet.getText, tweet.getFromUser)
}
}
}
}
case class TweetDto(id: Long, text: String, fromUser: String)
import test.common.{ExecutionContextManager, SimpleLogger}
import test.github.{Github, GithubQuery, GithubRepositoryDto}
import test.twitter.{TweetDto, Twitter}
import scala.concurrent.Future
object TestService extends TestService(Github, Twitter)
class TestService(github: Github, twitter: Twitter) {
private val log = SimpleLogger(this)
implicit private val ec = ExecutionContextManager.Ec
def search(searchQuery: String): Future[List[ProjectInfo]] = {
log.debug(s"Search github project tweets, query: $searchQuery")
val query = GithubQuery(searchQuery)
github.search(query) flatMap { gitHubResult =>
val tweets = gitHubResult.items.map { repository =>
twitter.search(repository.htmlUrl) map { repositoryTweets =>
ProjectInfo(repository, repositoryTweets)
}
}
Future.sequence(tweets)
}
}
}
case class ProjectInfo(githubProject: GithubRepositoryDto, tweets: List[TweetDto])
import test.common.{ExecutionContextManager, JsonUtil, SimpleLogger}
import scala.util.{Failure, Success}
object TestApp extends App {
private val log = SimpleLogger(this)
implicit private val ec = ExecutionContextManager.Ec
log.info("Start an application")
try {
TestService.search("Reactive") onComplete {
case Failure(exception) =>
println(exception)
case Success(projects) =>
println(JsonUtil.toJson(projects))
println("Completed. Please stop program (press ctrl + c)")
}
} catch {
case e: Throwable =>
log.error("Can't get the data", e)
}
}
@schebotarev
Copy link

not bad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment