Skip to content

Commit

Permalink
Support cats.Id (closes #163)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenFradet committed Apr 15, 2019
1 parent 5441724 commit 62aef5e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ object Dependencies {
// Scala
val catsEffect = "1.2.0"
val circe = "0.11.1"
val lruMap = "0.3.0-M2"
val lruMap = "0.3.0-M3"
val scalaj = "2.4.1"

// Scala (test only)
Expand Down
10 changes: 9 additions & 1 deletion src/main/scala/com.snowplowanalytics/forex/Forex.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import java.math.{BigDecimal, RoundingMode}

import scala.util.{Failure, Success, Try}

import cats.{Eval, Monad}
import cats.{Eval, Id, Monad}
import cats.effect.Sync
import cats.data.{EitherT, OptionT}
import cats.implicits._
Expand Down Expand Up @@ -48,6 +48,14 @@ object CreateForex {
.getClient[Eval](config)
.map(client => Forex(config, client))
}

implicit def idCreateForex(implicit C: ZonedClock[Id]): CreateForex[Id] =
new CreateForex[Id] {
def create(config: ForexConfig): Id[Forex[Id]] =
OerClient
.getClient[Id](config)
.map(client => Forex(config, client))
}
}

/** Companion object to get Forex object */
Expand Down
11 changes: 10 additions & 1 deletion src/main/scala/com.snowplowanalytics/forex/Transport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package com.snowplowanalytics.forex

import cats.Eval
import cats.{Eval, Id}
import cats.effect.Sync
import cats.syntax.either._
import io.circe.Decoder
Expand Down Expand Up @@ -57,6 +57,15 @@ object Transport {
}
}

/**
* Id http Transport to use in cases where you don't care about side-effects.
* @return an Id Transport
*/
implicit def idHttpTransport: Transport[Id] = new Transport[Id] {
def receive(endpoint: String, path: String): Id[Either[OerResponseError, OerResponse]] =
buildRequest(endpoint, path)
}

implicit def eitherDecoder: Decoder[Either[OerResponseError, OerResponse]] =
implicitly[Decoder[OerResponseError]].either(implicitly[Decoder[OerResponse]])

Expand Down
10 changes: 7 additions & 3 deletions src/main/scala/com.snowplowanalytics/forex/ZonedClock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ package com.snowplowanalytics.forex

import java.time.ZonedDateTime

import cats.Eval
import cats.{Eval, Id}
import cats.effect.Sync

trait ZonedClock[F[_]] {
Expand All @@ -26,7 +26,11 @@ object ZonedClock {
def now(): F[ZonedDateTime] = Sync[F].delay(ZonedDateTime.now)
}

implicit def unsafeZonedClock: ZonedClock[Eval] = new ZonedClock[Eval] {
def now(): Eval[ZonedDateTime] = Eval.now(ZonedDateTime.now)
implicit def evalZonedClock: ZonedClock[Eval] = new ZonedClock[Eval] {
def now(): Eval[ZonedDateTime] = Eval.later(ZonedDateTime.now)
}

implicit def idZonedClock: ZonedClock[Id] = new ZonedClock[Id] {
def now(): Id[ZonedDateTime] = ZonedDateTime.now
}
}

0 comments on commit 62aef5e

Please sign in to comment.