View crawler_example.md

Elixir simple crawler example

This example uses thease projects:

  1. mochiweb
  2. mochiweb_xpath

The HtmlPageReader goes to html's page and transform that content with mochiweb_html.parse

defmodule Usecase.HtmlPageReader do
View UserAPI.scala
package com.opportunity.simple.restapi
import javax.persistence.EntityManagerFactory
import com.opportunity.objectives.model.User
import com.opportunity.objectives.usecases.user.{ListUsersUsecase, LoginByFacebookUserUsecase, SaveNewUserUsecase}
import com.opportunity.simple.restapi.response.ProtocolResponse
import com.typesafe.config.Config
class UserAPI(val apiConfig: Config, val entityManagerFactory: EntityManagerFactory) extends Respondable with Authenticable with PersistibleUser {
View SaveNewFriendsUsecase.scala
package com.opportunity.objectives.usecases.user
import com.opportunity.objectives.exceptions.UserInvalidException
import com.opportunity.objectives.model.{Friend, User}
import com.opportunity.objectives.persistence.adapter.user.NewUserPersistenceAdapter
class SaveNewFriendsUsecase(persistenceAdapter: NewUserPersistenceAdapter) {
def save(user:User, friends:List[Friend]): User = {
persistenceAdapter.findUserById(user.id) match {
View SaveNewFriendsUsecaseSpec.scala
/**
* https://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html
* https://vimeo.com/43612849
* https://etorreborre.github.io/specs2/guide/SPECS2-3.7/org.specs2.guide.Matchers.html
*
**/
package com.opportunity.objectives.usecases.user
import com.opportunity.objectives.exceptions.UserInvalidException
import com.opportunity.objectives.model.{Friend, User}
View macro.exs
Code.eval_quoted(
quote do: unquote(1) + unquote(1 + 1)
)
#code to be parsed 1 + (1 + 1)
#result of execution {3, []}
View Boot.scala
def main(args: Array[String]) {
val port = restApiConfig.getInt("port")
println(restApiConfig.getString("resourceBase"))
val server = new Server(port)
val context = new WebAppContext()
context setContextPath "/"
context.setResourceBase(restApiConfig.getString("resourceBase"))
context.setInitParameter(ScalatraListener.LifeCycleKey, "com.opportunity.simple.restapi.ScalatraBootstrap")
context.addEventListener(new ScalatraListener)
View UserSpec.scala
package com.opportunity.simple.restapi.user
import com.opportunity.simple.restapi.basic.BasicBehaviour
class UserSpec extends BasicBehaviour {
"The user endpoint" should {
args(sequential=true)
"Create new user" ! userEndpointForTest().executeTestCreateNewUser()
"Try create a user with same email" ! userEndpointForTest().executeTestTryCreateAUserWithSameEmail()
View UserAPI.scala
post("/") {
contentType = "application/json"
authenticated { jsonObject =>
val entityManager = entityManagerFactory.createEntityManager()
val userRepository = new UserRepository(entityManager)
val userPersistenceAdapter = new NewUserPersistenceAdapterJPA(userRepository)
val usecase = new NewUserUsecase(userPersistenceAdapter)
val savedUser = usecase.save( User( jsonObject ) )
View SpringDataScalaTransactionControl.md

Exemplo de controle de transação com SpringData e Scala

Implementação JDBC

package com.codesimples.jdbc

import com.typesafe.scalalogging.Logger
import org.springframework.transaction.support.{TransactionCallbackWithoutResult, TransactionTemplate}
import org.springframework.transaction.{PlatformTransactionManager, TransactionStatus}
View UserMeasures.scala
case class UserMeasures(state: Map[String,Any]) {
def gender : String = {
state.getOrElse("usr_genero", "").asInstanceOf[String]
}
def measures: List[Map[String,Any]] = {
state.getOrElse("measures", List[Map[String,Any]]()).asInstanceOf[List[Map[String,Any]]]
}