Skip to content

Instantly share code, notes, and snippets.

View kivanio's full-sized avatar
🏠
Working from home

Kivanio Barbosa kivanio

🏠
Working from home
View GitHub Profile
@kivanio
kivanio / emailed_zip_file_extractor.rb
Created December 22, 2016 17:50 — forked from jkotchoff/emailed_zip_file_extractor.rb
Rails example of how to connect to the Gmail API using Ruby and download a Zip File attachment and then unzip contents from the zip archive
class EmailedZipFileExtractor
# This class downloads an email from gmail and extracts a file out of a .zip attachment
require 'google/apis/gmail_v1'
require 'googleauth'
require 'zip'
# These credentials come from creating an OAuth Web Application client ID
# in the Google developer console
#
@kivanio
kivanio / betfair_request_ssoid.rb
Created December 22, 2016 17:24 — forked from sergio1990/betfair_request_ssoid.rb
Request ssoid for accesing API-NG betfair.com
require "net/https"
require "uri"
crt = File.read("client-2048.crt")
key = File.read("client-2048.key")
app_key = "YOUR_APP_KEY"
username = "YOUR_USERNAME"
password = "YOUR_PASSWORD"
@kivanio
kivanio / test.rb
Created December 20, 2016 11:08 — forked from SamSaffron/test.rb
require 'net/http'
require 'openssl'
def test_connection(ip_address, uri, delays)
Net::HTTP.start(ip_address, uri.port,
use_ssl: true,
verify_mode: OpenSSL::SSL::VERIFY_NONE,
keep_alive_timeout: 50
) do |http|
while true
@kivanio
kivanio / converter_script.rb
Created October 24, 2016 19:34 — forked from kalmanh/converter_script.rb
Ruby script to convert .mdb db file into .csv
# Export data from Microsoft Access .mdb database into .csv files
# using https://github.com/brianb/mdbtools
# Install with homebrew - "brew install mdbtools"
class ConverterScript
tables = `mdb-tables -d , your_db_name.mdb`.chop.split(",")
tables.each do |table|
exists = system("mdb-export your_db_name.mdb '#{table}'")
`mdb-export your_db_name.mdb '#{table}' > #{table.gsub(" ", "_")}.csv` if exists
library(dplyr)
library(ggplot2)
library(httr)
setwd("C:/Dropbox/Projects/20160206_Soccer_Scores")
if (!file.exists("20160206_Soccer_Scores.csv.gz")) {
cat("must reread")
}
class Customer < ActiveRecord::Base
after_create :send_welcome_email, unless: :auto_registered?
has_one :auto_created, dependent: :destroy
private
def send_welcome_email
CustomerMailer.welcome_email(self).deliver
end
def auto_registered?
class CustomersController < ApplicationController
# …
def create
@customer = Customer.new(customer_params)
respond_to do |format|
if @customer.save
format.html { redirect_to @customer,
notice: 'Customer was successfully created.' }
format.json { render :show, status: :created,
class CustomersController < ApplicationController
# …
def create
if auto_create_request?
@customer = Customer.auto_create(customer_params)
else
@customer = Customer.register(customer_params)
end
respond_to do |format|
class Customer < ActiveRecord::Base
has_one :auto_created, dependent: :destroy
def self.register(params)
new(params).tap do |customer|
customer.save!
customer.send_welcome_email
end
end
@kivanio
kivanio / README.md
Created March 3, 2016 18:15 — forked from derwiki/README.md
Ruby module that you can use in a `before_action` on sensitive controllers for which you'd like a usage audit trail

Adding an audit log to your Rails app

If you have any sort of administrative interface on your web site, you can easily imagine an intruder gaining access and mucking about. How do you know the extent of the damage? Adding an audit log to your app is one quick solution. An audit log should record a few things:

  • controller entry points with parameter values
  • permanent information about the user, like user_id
  • transient information about the user, like IP and user_agent

Using the Rails framework, this is as simple as adding a before_action to your admin controllers. Here’s a basic version that I’m using in production.