Skip to content

Instantly share code, notes, and snippets.

@joaquimadraz
joaquimadraz / missile_launcher.rb
Created May 13, 2018 12:01
Dream Cheeky USB Missile Launcher
# REFERENCES
# https://github.com/codedance/Retaliation/blob/master/retaliation.py
# http://www.rkblog.rk.edu.pl/w/p/controlling-usb-missile-launchers-python/
# http://matthias.vallentin.net/blog/2007/04/writing-a-linux-kernel-driver-for-an-unknown-usb-device/
require 'libusb'
class Launcher
ID_PRODUCT = 0x0701
ID_VENDOR = 0x0a81
# source: http://blacklane.github.io/2016/04/23/lessons-learned-from-some-of-the-best-ruby-codebases-part-1/
class Foo < Module
def initialize
@module = Module.new
end
def included(descendant)
@module.class_eval do
schema = Compel.integer.min(1900).max(2016)
result = schema.validate(2017)
puts result.errors
# => ["cannot be greater than 2016"]
object = {
first_name: 'Joaquim',
birth_date: '1989-0',
address: {
line_one: 'Lisboa',
post_code: '1100',
country_code: 'PT'
}
}
@joaquimadraz
joaquimadraz / compel_array_validation.rb
Created January 16, 2016 12:14
compel_array_validation.rb
schema = Compel.array.items(
Compel.hash.keys({
a: Compel.string.required,
b: Compel.string.format(/^abc$/)
})
)
object = [
{ a: 'A', b: 'abc' },
{ a: 'B' },
@joaquimadraz
joaquimadraz / user_create_use_case.rb
Last active August 29, 2015 14:28
User create use case, validate, complete_date and save
module UseCases
module User
module Create
class Validate < RestMyCase::Base
# all necessary validations to persit
context_reader :user,
:user_attributes
@joaquimadraz
joaquimadraz / user_model_example.rb
Last active August 29, 2015 14:28
User model example
class User < ActiveRecord::Model
default_scope { where(state: 'active') }
scope :inactive, -> { where(active: false) }
validates_presence_of :first_name, :last_name, :email, :password, :invitation_token
validates :first_name, length: { minimum: 2 }
validates :last_name, length: { minimum: 2 }
@joaquimadraz
joaquimadraz / api_level_validations_with_grape.rb
Last active August 29, 2015 14:28
API level validations with Grape
require 'grape'
module Blog
class Web < Grape::API
...
desc "Create post comment"
params do
requires :id, type: Integer
@joaquimadraz
joaquimadraz / internal_task.rb
Created May 30, 2015 10:24
internal_task.rb
require 'internal_task/version'
module InternalTask
extend Rake::DSL
namespace :internal_task do
desc 'Say hello'
task :hello do
@joaquimadraz
joaquimadraz / ze.rb
Last active August 29, 2015 14:21
merge same key array of hashes
# input = [
# {:alarma=>{:foo2=>"bar2"}},
# {:alarma=>{:foo=>"bar"}},
# {:alarma=>{:test2=>{:foo=>"bar"}}},
# {:cenas=>"teste"},
# {:alarma=>{:foo2=>"bar2", :foo=>"bar"}}
# ]
input = [
{:foo=>{:foo=>{:bar=>"stuff"}}},