Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enrich Repository class #25

Merged
merged 5 commits into from
Jun 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 147 additions & 1 deletion src/main/scala/github4s/Decoders.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package github4s

import github4s.free.domain.Commit
import github4s.free.domain._
import io.circe._, io.circe.generic.auto._, io.circe.jawn._, io.circe.syntax._
import cats.data.Xor

Expand All @@ -19,4 +19,150 @@ object Decoders {
} yield Commit(sha, message, date, url, login, avatar_url, author_url)
}

implicit val decodeRepository: Decoder[Repository] = Decoder.instance { c ⇒
for {
id ← c.downField("id").as[Int]
name ← c.downField("name").as[String]
full_name ← c.downField("full_name").as[String]
owner ← c.downField("owner").as[User]
priv ← c.downField("private").as[Boolean]
description ← c.downField("description").as[String]
fork ← c.downField("fork").as[Boolean]
created_at ← c.downField("created_at").as[String]
updated_at ← c.downField("updated_at").as[String]
pushed_at ← c.downField("pushed_at").as[String]
homepage ← c.downField("homepage").as[Option[String]]
language ← c.downField("language").as[Option[String]]
organization ← c.downField("organization").as[Option[User]]
size ← c.downField("size").as[Int]
stargazers_count ← c.downField("stargazers_count").as[Int]
watchers_count ← c.downField("watchers_count").as[Int]
forks_count ← c.downField("forks_count").as[Int]
open_issues_count ← c.downField("open_issues_count").as[Int]
open_issues ← c.downField("open_issues").as[Int]
watchers ← c.downField("watchers").as[Int]
network_count ← c.downField("network_count").as[Int]
subscribers_count ← c.downField("subscribers_count").as[Int]
has_issues ← c.downField("has_issues").as[Boolean]
has_downloads ← c.downField("has_downloads").as[Boolean]
has_wiki ← c.downField("has_wiki").as[Boolean]
has_pages ← c.downField("has_pages").as[Boolean]
url ← c.downField("url").as[String]
html_url ← c.downField("html_url").as[String]
git_url ← c.downField("git_url").as[String]
ssh_url ← c.downField("ssh_url").as[String]
clone_url ← c.downField("clone_url").as[String]
svn_url ← c.downField("svn_url").as[String]
forks_url ← c.downField("forks_url").as[Option[String]]
keys_url ← c.downField("keys_url").as[Option[String]]
collaborators_url ← c.downField("collaborators_url").as[Option[String]]
teams_url ← c.downField("teams_url").as[Option[String]]
hooks_url ← c.downField("hooks_url").as[Option[String]]
issue_events_url ← c.downField("issue_events_url").as[Option[String]]
events_url ← c.downField("events_url").as[Option[String]]
assignees_url ← c.downField("assignees_url").as[Option[String]]
branches_url ← c.downField("branches_url").as[Option[String]]
tags_url ← c.downField("tags_url").as[Option[String]]
blobs_url ← c.downField("blobs_url").as[Option[String]]
git_tags_url ← c.downField("git_tags_url").as[Option[String]]
git_refs_url ← c.downField("git_refs_url").as[Option[String]]
trees_url ← c.downField("trees_url").as[Option[String]]
statuses_url ← c.downField("statuses_url").as[Option[String]]
languages_url ← c.downField("languages_url").as[Option[String]]
stargazers_url ← c.downField("stargazers_url").as[Option[String]]
contributors_url ← c.downField("contributors_url").as[Option[String]]
subscribers_url ← c.downField("subscribers_url").as[Option[String]]
subscription_url ← c.downField("subscription_url").as[Option[String]]
commits_url ← c.downField("commits_url").as[Option[String]]
git_commits_url ← c.downField("git_commits_url").as[Option[String]]
comments_url ← c.downField("comments_url").as[Option[String]]
issue_comment_url ← c.downField("issue_comment_url").as[Option[String]]
contents_url ← c.downField("contents_url").as[Option[String]]
compare_url ← c.downField("compare_url").as[Option[String]]
merges_url ← c.downField("merges_url").as[Option[String]]
archive_url ← c.downField("archive_url").as[Option[String]]
downloads_url ← c.downField("downloads_url").as[Option[String]]
issues_url ← c.downField("issues_url").as[Option[String]]
pulls_url ← c.downField("pulls_url").as[Option[String]]
milestones_url ← c.downField("milestones_url").as[Option[String]]
notifications_url ← c.downField("notifications_url").as[Option[String]]
labels_url ← c.downField("labels_url").as[Option[String]]
releases_url ← c.downField("releases_url").as[Option[String]]
deployments_url ← c.downField("deployments_url").as[Option[String]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use here camel case for the variables?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Historically" I have been naming parameters in the same way that Github does, in order to facilitate its use by people familiarised with the official API. I would like to keep consequently respecting this criteria along the entire project.

} yield Repository(
id = id,
name = name,
full_name = full_name,
owner = owner,
`private` = priv,
description = description,
fork = fork,
created_at = created_at,
updated_at = updated_at,
pushed_at = pushed_at,
homepage = homepage,
language = language,
organization = organization,
status = RepoStatus(
size = size,
stargazers_count = stargazers_count,
watchers_count = watchers_count,
forks_count = forks_count,
open_issues_count = open_issues_count,
open_issues = open_issues,
watchers = watchers,
network_count = network_count,
subscribers_count = subscribers_count,
has_issues = has_issues,
has_downloads = has_downloads,
has_wiki = has_wiki,
has_pages = has_pages
),
urls = RepoUrls(
url = url,
html_url = html_url,
git_url = git_url,
ssh_url = ssh_url,
clone_url = clone_url,
svn_url = svn_url,
forks_url = forks_url,
keys_url = keys_url,
collaborators_url = collaborators_url,
teams_url = teams_url,
hooks_url = hooks_url,
issue_events_url = issue_events_url,
events_url = events_url,
assignees_url = assignees_url,
branches_url = branches_url,
tags_url = tags_url,
blobs_url = blobs_url,
git_tags_url = git_tags_url,
git_refs_url = git_refs_url,
trees_url = trees_url,
statuses_url = statuses_url,
languages_url = languages_url,
stargazers_url = stargazers_url,
contributors_url = contributors_url,
subscribers_url = subscribers_url,
subscription_url = subscription_url,
commits_url = commits_url,
git_commits_url = git_commits_url,
comments_url = comments_url,
issue_comment_url = issue_comment_url,
contents_url = contents_url,
compare_url = compare_url,
merges_url = merges_url,
archive_url = archive_url,
downloads_url = downloads_url,
issues_url = issues_url,
pulls_url = pulls_url,
milestones_url = milestones_url,
notifications_url = notifications_url,
labels_url = labels_url,
releases_url = releases_url,
deployments_url = deployments_url
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we are in the urls class, it makes sense to remove the _url suffix from all fields?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same than above.

)
)
}

}
2 changes: 1 addition & 1 deletion src/main/scala/github4s/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import cats.{ Monad, Id, Eval, MonadError }
import github4s.free.interpreters.Interpreters
import scala.concurrent.{ ExecutionContext, Future }

object implicits extends Interpreters with EvalInstances with IdInstances with FutureInstances
object implicits extends Interpreters with EvalInstances with IdInstances with FutureInstances

trait EvalInstances {

Expand Down
76 changes: 75 additions & 1 deletion src/main/scala/github4s/free/domain/Repository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,79 @@ package github4s.free.domain

case class Repository(
id: Int,
name: String
name: String,
full_name: String,
owner: User,
`private`: Boolean,
description: String,
fork: Boolean,
urls: RepoUrls,
created_at: String,
updated_at: String,
pushed_at: String,
homepage: Option[String] = None,
language: Option[String] = None,
status: RepoStatus,
organization: Option[User] = None
)

case class RepoStatus(
size: Int,
stargazers_count: Int,
watchers_count: Int,
forks_count: Int,
open_issues_count: Int,
open_issues: Int,
watchers: Int,
network_count: Int,
subscribers_count: Int,
has_issues: Boolean,
has_downloads: Boolean,
has_wiki: Boolean,
has_pages: Boolean
)

case class RepoUrls(
url: String,
html_url: String,
git_url: String,
ssh_url: String,
clone_url: String,
svn_url: String,
forks_url: Option[String] = None,
keys_url: Option[String] = None,
collaborators_url: Option[String] = None,
teams_url: Option[String] = None,
hooks_url: Option[String] = None,
issue_events_url: Option[String] = None,
events_url: Option[String] = None,
assignees_url: Option[String] = None,
branches_url: Option[String] = None,
tags_url: Option[String] = None,
blobs_url: Option[String] = None,
git_tags_url: Option[String] = None,
git_refs_url: Option[String] = None,
trees_url: Option[String] = None,
statuses_url: Option[String] = None,
languages_url: Option[String] = None,
stargazers_url: Option[String] = None,
contributors_url: Option[String] = None,
subscribers_url: Option[String] = None,
subscription_url: Option[String] = None,
commits_url: Option[String] = None,
git_commits_url: Option[String] = None,
comments_url: Option[String] = None,
issue_comment_url: Option[String] = None,
contents_url: Option[String] = None,
compare_url: Option[String] = None,
merges_url: Option[String] = None,
archive_url: Option[String] = None,
downloads_url: Option[String] = None,
issues_url: Option[String] = None,
pulls_url: Option[String] = None,
milestones_url: Option[String] = None,
notifications_url: Option[String] = None,
labels_url: Option[String] = None,
releases_url: Option[String] = None,
deployments_url: Option[String] = None
)
33 changes: 33 additions & 0 deletions src/test/scala/github4s/unit/DecodersSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package github4s.unit

import cats.scalatest.{ XorValues, XorMatchers }
import github4s.free.domain.{ Commit, Repository }
import github4s.utils.FakeResponses
import github4s.Decoders._
import org.scalatest._
import io.circe.parser._

class DecodersSpec
extends FlatSpec
with Matchers
with XorMatchers
with XorValues
with FakeResponses {

"Commit decoder" should "return a list of commits when the JSON is valid" in {
decode[List[Commit]](listCommitsValidResponse) shouldBe right
}

it should "return an empty list for an empty JSON" in {
decode[List[Commit]](emptyListResponse).value.isEmpty shouldBe true
}

"Repository decoder" should "return a valid repo for a valid JSON" in {
decode[Repository](getRepoResponse) shouldBe right
}

it should "return error for an empty JSON" in {
decode[Repository](emptyListResponse) shouldBe left
}

}
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version in ThisBuild := "0.3-SNAPSHOT"
version in ThisBuild := "0.5-SNAPSHOT"