Skip to content

Instantly share code, notes, and snippets.

View ferromir's full-sized avatar

Fernando Romero ferromir

View GitHub Profile
@ferromir
ferromir / gist:1dfdd4c67cd7089f1f5b
Created March 20, 2015 05:52
ACR - Java - Final usage
// Design your class for inheritance and document it, otherwise forbid it using final
final class ChessAlgorithm {
. . .
}
/* Result:
Chess.java:6: Can't subclass final classes: class ChessAlgorithm
class BetterChessAlgorithm extends ChessAlgorithm {
@ferromir
ferromir / gist:3ba599741eb5b2a3cd83
Created March 20, 2015 05:35
ACR - Ruby - SQL Injection
# This bypasses id == 1 condition and deletes all records
params[:id] = "1) OR 1=1--"
User.delete_all("id = #{params[:id]}")
# Produce:
# DELETE FROM "users" WHERE (id = 1) OR 1=1--)
# Bad
Project.where("name = '#{params[:name]}'")
@ferromir
ferromir / gist:982e01e1e8f022817241
Created March 20, 2015 05:27
ACR - Ruby - Log sensitive data
# Given an user
@user = Article.new(params[:user])
# Bad
logger.debug "New user: #{@user.attributes.inspect}"
# Good
logger.debug "New user: #{@user.attributes.id}"
@ferromir
ferromir / gist:1ee4a071b719f9aa0bac
Last active August 29, 2015 14:17
ACR - Ruby - Mass asignment
# http://code.tutsplus.com/tutorials/mass-assignment-rails-and-you--net-31695
# Given the mass asignment:
attrs = {:first => "John", :last => "Doe", :email => "[email protected]"}
user = User.new(attrs)
user.first #=> "John"
user.last #=> "Doe"
user.email #=> "[email protected]"
@ferromir
ferromir / gist:c200a0c87e97d098ca26
Last active August 29, 2015 14:15
APPEKHO-1741 - Curls for error replication
curl http://10.229.48.35:8282/people \
--request POST \
--header "Content-Type: application/json" \
--data-binary '
{
"focusset_id": "54760a5ae4b04f6a80bde81a",
"from_date": "1416729600000",
"to_date": "1424200529143",
"ekhoscore_data": {
"aug_weights": {
val payload = """
{
aug_weights: {
interest: 0.20,
impact: 0.0,
emotion: 0.10,
opinion: 0.0
},
terms_and_weights: [
{
@ferromir
ferromir / api_v2_schema.json
Last active August 29, 2015 14:07
Ekho APIv2 JSON schema
{
"$schema": "http://json-schema.org/draft-04/schema#",
"account": {
"type": "object",
"properties": {
"account_type": { "enum": [ "individual", "organization" ]
},
"name": { "type": "string" },
"email": { "type": "string", "format": "email" },
@ferromir
ferromir / toOwner.scala
Last active August 29, 2015 14:07
MongoDB doc with embedded array to case classes objects
case class Pet(name: String, kind: String)
case class Owner(name: String, pets: List[Pet])
implicit def toOwner(doc: foos.T) = Owner(
name = doc.as[String]("name"),
pets = doc.as[MongoDBList]("pets").map(_.asInstanceOf[BasicDBObject]).map { p =>
Pet(p.as[String]("name"), p.as[String]("kind"))
} toList
)
@ferromir
ferromir / gist:8da462c44a1cb41f2466
Created September 16, 2014 00:13
Usage of spray directives
def listQueryHistogram(username: String, focussetId: String) =
(ekhoAuth(GET, Some(username), Some(focussetId)) &
params &
parameters('queries) &
detach())
{
// The first two are the values extracted by params and parameterers directives
(params, queries) => ctx =>
val theList = queries.split(",").toList
@ferromir
ferromir / Build.scala
Last active August 29, 2015 14:06
Github dependency
import java.io.File
import sbt._
import Keys._
object EkhoBuild extends Build {
lazy val root = Project("root", file(".")).dependsOn(ekhoFramework)
// Github dependencies
lazy val ekhoFramework = RootProject(