Skip to content

Commit

Permalink
alerts-server: BitsoBook functionality moved to Book
Browse files Browse the repository at this point in the history
  • Loading branch information
tg44 authored and AlexITC committed Oct 6, 2018
1 parent 3bd37b1 commit 2fdeeb1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 21 deletions.
23 changes: 10 additions & 13 deletions alerts-server/app/com/alexitc/coinalerts/models/Book.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,18 @@ object Book {
} yield Book(market, currency)
}
}
}

object BitsoBook {

/**
* BITSO represents a book in the reversed order than us
*/
def fromString(string: String): Option[Book] = {
* BITSO represents a book in the reversed order than us
*/
def fromBitsoString(string: String): Option[Book] = {
Option(string.toUpperCase.split("_"))
.filter(_.length == 2)
.flatMap { parts =>
for {
currency <- Currency.from(parts(0))
market <- Market.from(parts(1))
} yield Book(market, currency)
}
.filter(_.length == 2)
.flatMap { parts =>
for {
currency <- Currency.from(parts(0))
market <- Market.from(parts(1))
} yield Book(market, currency)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.alexitc.coinalerts.services.external
import javax.inject.Inject

import com.alexitc.coinalerts.config.ExternalServiceExecutionContext
import com.alexitc.coinalerts.models.{BitsoBook, Book}
import com.alexitc.coinalerts.models.Book
import com.alexitc.coinalerts.tasks.models.Ticker
import com.bitso.{Bitso, BitsoTicker}
import org.slf4j.LoggerFactory
Expand All @@ -21,7 +21,7 @@ class BitsoService @Inject() (
override def availableBooks(): Future[List[Book]] = {
val result = Future {
bitso.getTicker.toList.flatMap { ticker =>
BitsoBook.fromString(ticker.getBook)
Book.fromBitsoString(ticker.getBook)
.orElse {
logger.warn(s"Unable to create book from string = [${ticker.getBook}]")
None
Expand Down Expand Up @@ -51,7 +51,7 @@ class BitsoService @Inject() (
}

private def createTicker(bitsoTicker: BitsoTicker): Option[Ticker] = {
BitsoBook.fromString(bitsoTicker.getBook)
Book.fromBitsoString(bitsoTicker.getBook)
.map { book =>
Ticker(book, bitsoTicker.getLast)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.alexitc.coinalerts.services.external
import javax.inject.Inject

import com.alexitc.coinalerts.config.ExternalServiceExecutionContext
import com.alexitc.coinalerts.models.{BitsoBook, Book, Currency, Market}
import com.alexitc.coinalerts.models.{Book, Currency, Market}
import com.alexitc.coinalerts.tasks.models.Ticker
import org.slf4j.LoggerFactory
import play.api.libs.functional.syntax._
Expand Down Expand Up @@ -92,7 +92,7 @@ class KucoinService @Inject() (

private def createBook(string: String): Option[Book] = {
// the book format is reversed to the one in our app
BitsoBook.fromString(string.replace("-", "_"))
Book.fromBitsoString(string.replace("-", "_"))
.orElse {
logger.warn(s"Unable to create book from string = [$string]")
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class BitsoTickerCollector @Inject() (bitsoService: BitsoService) extends Ticker
override val exchange: Exchange = Exchange.BITSO

override def getTickerList: Future[List[Ticker]] = {
bitsoService.getTickerList
bitsoService.getTickerList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class BookSpec extends WordSpec with MustMatchers {
val bookString = "LTC_BTC"
val expectedBookString = "BTC_LTC"

val actualBook = BitsoBook.fromString(bookString).get
val actualBook = Book.fromBitsoString(bookString).get

actualBook.string mustEqual expectedBookString
}
Expand All @@ -18,7 +18,7 @@ class BookSpec extends WordSpec with MustMatchers {
val bookString = "R_BTC"
val expectedBookString = "BTC_R"

val actualBook = BitsoBook.fromString(bookString).get
val actualBook = Book.fromBitsoString(bookString).get

actualBook.string mustEqual expectedBookString
}
Expand Down

0 comments on commit 2fdeeb1

Please sign in to comment.