Skip to content

Instantly share code, notes, and snippets.

View agnaldo4j's full-sized avatar
🏢
Working

Agnaldo de Oliveira agnaldo4j

🏢
Working
View GitHub Profile
@agnaldo4j
agnaldo4j / crawler_example.md
Last active October 15, 2016 11:20
Crawler example with mochiweb_html and mochiweb_xpath.

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
@agnaldo4j
agnaldo4j / UserAPI.scala
Created January 16, 2016 15:34
Exemplo de endpoint acessando usecases
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 {
@agnaldo4j
agnaldo4j / SaveNewFriendsUsecase.scala
Created January 12, 2016 02:20
Exemplo de um usecase
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 {
@agnaldo4j
agnaldo4j / SaveNewFriendsUsecaseSpec.scala
Created January 12, 2016 01:33
Exemplo de teste usando specs2 e arquitetura limpa
/**
* 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}
@agnaldo4j
agnaldo4j / macro.exs
Last active January 7, 2016 01:01
Executando código dinâmico em elixir
Code.eval_quoted(
quote do: unquote(1) + unquote(1 + 1)
)
#code to be parsed 1 + (1 + 1)
#result of execution {3, []}
@agnaldo4j
agnaldo4j / Boot.scala
Created October 31, 2015 22:23
Implementação de servidor jetty embarcado com Java WebSocket API e Scalatra. :-)
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)
@agnaldo4j
agnaldo4j / UserSpec.scala
Last active October 31, 2015 12:21
Estrutura testes de integração para API. Objetivo esconder ferramentaria e simplificar os testes
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()
@agnaldo4j
agnaldo4j / UserAPI.scala
Created October 25, 2015 16:26
Separação tecnologia da implementação da regra do negócio. Com isso mudar de tecnologia de persistência e framework web fica bem mais fácil
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 ) )
@agnaldo4j
agnaldo4j / SpringDataScalaTransactionControl.md
Last active August 29, 2015 17:34
Controle de transação com SpringData, SpringJPA e Scala

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}
@agnaldo4j
agnaldo4j / UserMeasures.scala
Created December 18, 2014 11:40
Exemplo de objeto aplicando somente ações a estrutura de dados imutável.
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]]]
}