Skip to content

Instantly share code, notes, and snippets.

@neysimoes
Created September 12, 2017 20:18
Show Gist options
  • Select an option

  • Save neysimoes/7fc49c89fd1c45029666017aca2bbe91 to your computer and use it in GitHub Desktop.

Select an option

Save neysimoes/7fc49c89fd1c45029666017aca2bbe91 to your computer and use it in GitHub Desktop.
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import com.typesafe.config.{Config, ConfigFactory}
import scalaj.http.{Http, HttpRequest, HttpResponse}
import scala.xml.XML
object HolydaysProvider {
def get(year: Option[String], state: Option[String], city: Option[String]): List[String] = {
val apiUrl: String = "http://www.calendario.com.br/api/api_feriados.php"
val config: Config = ConfigFactory.load()
val token: String = config.getString("elektro.calendario.token")
val baseRequest: HttpRequest = Http(apiUrl)
val response: HttpResponse[String] = baseRequest
.param("token", token)
.param("year", year.getOrElse(""))
.param("state", state.getOrElse(""))
.param("city", city.getOrElse(""))
.timeout(connTimeoutMs = 2000, readTimeoutMs = 5000)
.asString
val xmlString = response.body
// convert the `String` to a `scala.xml.Elem`
val xml = XML.loadString(xmlString)
// handle the xml as desired ...
val dates = xml \ "event" \ "date"
val formatador: DateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy")
dates.map { date =>
LocalDate.parse(date.text, formatador).toString
}.toList
}
}
import java.time.LocalDate
import HolydaysProvider
object WorkingDayHelper {
val holydays: List[String] = HolydaysProvider.get(Option("2017"), None, None)
def next(date: Option[LocalDate]): LocalDate = {
val nextDay = date.getOrElse(LocalDate.now).plusDays(1)
if(holydays.indexOf(nextDay.toString) < 0 && nextDay.getDayOfWeek.getValue <= 5) {
nextDay
} else {
next(Option(nextDay))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment