Skip to content

Instantly share code, notes, and snippets.

@avramirez
avramirez / pre-request-jwt.js
Created September 21, 2021 08:03 — forked from corbanb/pre-request-jwt.js
JWT tokenize - Postman Pre-Request Script
function base64url(source) {
// Encode in classical base64
encodedSource = CryptoJS.enc.Base64.stringify(source);
// Remove padding equal characters
encodedSource = encodedSource.replace(/=+$/, '');
// Replace characters according to base64url specifications
encodedSource = encodedSource.replace(/\+/g, '-');
encodedSource = encodedSource.replace(/\//g, '_');
@avramirez
avramirez / AsynchronousOperation.swift
Created July 18, 2018 05:13 — forked from calebd/AsynchronousOperation.swift
Concurrent NSOperation in Swift
import Foundation
/// An abstract class that makes building simple asynchronous operations easy.
/// Subclasses must implement `execute()` to perform any work and call
/// `finish()` when they are done. All `NSOperation` work will be handled
/// automatically.
open class AsynchronousOperation: Operation {
// MARK: - Properties
@avramirez
avramirez / EnumarationTest.scala
Last active May 5, 2017 10:54
How not to properly use scala Enumeration
object TestEnum extends Enumeration {
case class MyEnum(name: String) extends Val(name)
val ENUM_1 = MyEnum("0100")
val ENUM_2 = MyEnum("0200")
}
//defined module TestEnum
TestEnum.values
//res0: TestEnum.ValueSet = TestEnum.ValueSet(0100, 0200)
@avramirez
avramirez / Future-retry.scala
Created November 22, 2016 15:20 — forked from viktorklang/Future-retry.scala
Asynchronous retry for Future in Scala
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import akka.pattern.after
import akka.actor.Scheduler
/**
* Given an operation that produces a T, returns a Future containing the result of T, unless an exception is thrown,
* in which case the operation will be retried after _delay_ time, if there are more possible retries, which is configured through
* the _retries_ parameter. If the operation does not succeed and there is no retries left, the resulting Future will contain the last failure.
@avramirez
avramirez / ForComprehensionFutureWithTypeCasting.scala
Last active September 21, 2020 18:25
Scala For comprehension on Future with Type Casting
/*
Resources :
http://www.scala-lang.org/files/archive/spec/2.11/08-pattern-matching.html#pattern-binders
Bug on SCALA for comprehension + pattern matching
https://issues.scala-lang.org/browse/SI-900
*/
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
scala> :paste
// Entering paste mode (ctrl-D to finish)
val a : Option[String] = Some("Shit")
val b : Option[String] = Some("LetterB")
for (av <- a;bv <- b){
println(s"AV -> $av")
println(s"BV -> $bv")
}
@avramirez
avramirez / TestCustomSlick_3.0_CodeGen.scala
Created August 28, 2015 08:57
Testing custom slick 3.0 code gen
import java.net.URI
import scala.concurrent.{ExecutionContext, Await}
import scala.concurrent.duration.Duration
import slick.backend.DatabaseConfig
import slick.driver.JdbcProfile
import slick.util.ConfigExtensionMethods.configExtensionMethods
import slick.codegen.SourceCodeGenerator
class CustomizedCodeGenerator(model: slick.model.Model) extends SourceCodeGenerator(model) {
override def code = super.code + indent("""
@avramirez
avramirez / TestCustomSlick_2.0_CodeGen.scala
Last active August 28, 2015 08:57
Testing custom slick 2.0 code gen
package demo
import scala.slick.model.Model
import scala.slick.jdbc.meta.createModel
import scala.slick.driver.H2Driver
import Config._
/**
* This customizes the Slick code generator. We only do simple name mappings.
* For a more advanced example see https://github.com/cvogt/slick-presentation/tree/scala-exchange-2013
*/
@avramirez
avramirez / FutureTestException.scala
Last active September 28, 2017 11:32
Testing Scala Future with For comprehension
import scala.util.{ Failure, Success }
import scala.language.implicitConversions
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
val a = Future{println("a"); "a"}
val b = Future{println("b"); "b"}
val c = Future{throw new Exception("Test")}
val d = Future{
Thread.sleep(3000)
@avramirez
avramirez / XPathQueryInScalaUsingJavax.scala
Last active August 29, 2015 14:26
How to do a XPath qeury in scala. Using the native javax.xml library
import javax.xml.xpath._
import org.xml.sax.InputSource
import java.io.StringReader
import javax.xml.parsers._
import javax.xml.transform.dom.DOMSource
import java.io.StringWriter
import javax.xml.transform.stream.StreamResult
import javax.xml.transform.TransformerFactory
import javax.xml.namespace.NamespaceContext
import java.io.ByteArrayInputStream