avatar

Andres Jaimes

Creating an sbt project from scratch

Files and project structure sbt projects use the same structure as Maven. Creating a project from scratch involve the following steps: mkdir some-new-project cd some-new-project mkdir -p src/{main,test}/{resources,scala} mkdir project Create a new file called build.sbt and add the following contents: name := "SomeNewProject" version := "1.0" scalaVersion := "2.13.5" Our final folder structure has to look like this: . ├── build.sbt ├── project └── src ├── main │ ├── resources │ └── scala └── test ├── resources └── scala Done.

Sending a message using telegram and scala

In this article we are going to use scala to send a message via Telegram. The process is very simple and pretty much involves setting up a bot and making a GET request to Telegram’s api. The Telegram app on the phone has more features than its desktop counterpart. I recommend you to use the phone version to make sure you have access to all the required features. On Telegram 1.

How to create a self-contained jar file (fat jar) using sbt

A fat jar is a jar file that contains any dependencies required by a Java or Scala program. Deployment becomes an easy task if we only have to deal with one single file. In this article we’ll go through the steps required to create a fat jar using Scala and sbt. The following procedure has been tested using Scala 2.12.8, sbt 1.2.8 and jdk 11. 1. project/assembly.sbt Create project/assembly.

Implementing put and delete operations using BatchWriteItem

The BatchWriteItem operation allows us to work with groups of up to 25 items per request. Additionally, we can put and delete items to one or multiple tables. Amazon DynamoDB’s page says: A single call to BatchWriteItem can write up to 16 MB of data, which can comprise as many as 25 put or delete requests. Individual items to be written can be as large as 400 KB. In this article we are going to go through the process of putting and deleting items to a table that looks like this:

Inserting and updating items to DynamoDB lists and maps

Should I use a map or a list? A simple rule: If you need to update or remove an item, lists only allow you to do it by index. While maps let you do it by key. Their simplified representation goes like this: { "someMap": { "key1": "value1", "key2": "value2", }, "someList": ["value1", "value2"] } Inserting and updating to a map The following example adds or updates an instance of “SomeItem” as a json string into a map field called someMapField.

Testing scala classes and controllers

This article discusses different approaches for testing classes, services, and PlayFramework controllers using scalatest. Setting up the project dependencies First step is to add to build.sbt the next dependency: "org.scalatest" %% "scalatest" % "3.2.2" The following template unit test uses the WordSpec style which offers a natural way for writing tests. scalatest offers more styles, but I find this one more expressive. PlaySpec (found in the PaylFramework) extends WordSpec and has a similar behavior.

Enumerations

Using case object Unlike other languages, Scala does not have a keyword for enumerations. One way to implement them is by using case objects. For example: sealed trait OperatingSystem extends Product with Serializable object OperatingSystem { case object FreeBsd extends OperatingSystem case object Debian extends OperatingSystem case object Alpine extends OperatingSystem case object Arch extends OperatingSystem case object Osx extends OperatingSystem } To use it, just define a value of type OperatingSystem.

Akka Actors

The Play Framework is built upon Akka actors, but does everything so that you don’t really need to use them. Despite this, actors are easy to integrate with Play, precisely because it is built on them (there is already an actor system for you to use) A few exceptions are: websockets handling (doing that using actors is a breeze) scheduling tasks for doing daily or weekly jobs Akka actors use messages to communicate between among them.

Check multiple option values in Scala

Two main approaches for doing this, the second one being for me the most appropriate. Check values using regular if conditions: if (opt1.isDefined && opt2.isDefined && opt3.isDefined) // do something Check values using match: (opt1, opt2, opt3) match { case(Some(val1), Some(val2), Some(val3)) => ??? case _ => ??? }

Creating and validating JWT JSON web tokens

This article goes through the process of creating and validating JWT’s (JSON web tokens) using Scala. Our implementation uses the awesome io.jsonwebtoken library, and can be added to a sbt project like this: libraryDependencies ++= Seq("io.jsonwebtoken" % "jjwt" % "0.9.1") Creating a token We are going to use Scala’s apply/unapply functions for this implementation. This will allow us to use matchers for checking JWT’s. import java.time.Instant import java.util.{Date, UUID} import io.