Skip to content

Instantly share code, notes, and snippets.

View electron0zero's full-sized avatar
🎯
Focusing

Suraj Nath electron0zero

🎯
Focusing
View GitHub Profile
@electron0zero
electron0zero / merge_csv.rb
Created March 19, 2018 06:58 — forked from bahelms/merge_csv.rb
Merge multiple csv files into one in Ruby
require "csv"
def csv_headers
["Your", "Headers", "Here"]
end
files = Dir["file_names_*.csv"].sort_by { |f| "if you want to sort the files" }
file_contents = files.map { |f| CSV.read(f) }
csv_string = CSV.generate do |csv|
@electron0zero
electron0zero / gist:e00cc69ad9345048b4c42c8b1cf31a63
Created March 16, 2018 11:16 — forked from ericsaboia/gist:994614
Changelog ActiveRecord:Base Rails 3.1
# ActiveRecord::Base#dup and ActiveRecord::Base#clone semantics have changed to closer match normal Ruby dup and clone semantics.
# Calling ActiveRecord::Base#clone will result in a shallow copy of the record, including copying the frozen state. No callbacks will be called.
# Calling ActiveRecord::Base#dup will duplicate the record, including calling after initialize hooks. Frozen state will not be copied, and all associations will be cleared. A duped record will return true for new_record?, have a nil id field, and is saveable.
@electron0zero
electron0zero / json_validator.rb
Created March 16, 2018 09:57 — forked from joost/json_validator.rb
Rails 3 / Rails 4 JSON validator
# Put this code in lib/validators/json_validator.rb
# Usage in your model:
# validates :json_attribute, presence: true, json: true
#
# To have a detailed error use something like:
# validates :json_attribute, presence: true, json: {message: :some_i18n_key}
# In your yaml use:
# some_i18n_key: "detailed exception message: %{exception_message}"
class JsonValidator < ActiveModel::EachValidator
# Benchmarking answers to http://stackoverflow.com/questions/9333952/in-ruby-how-to-make-the-string-include-method-ignore-case/9334066
# http://www.gutenberg.org/cache/epub/1661/pg1661.txt
strings = IO.read('sherlock.txt').scan(/\w+/) # 109,222 words
known = 500.times.map do
strings.sample.downcase.chars.map{ |c| rand<0.5 ? c.upcase : c }.join
end
words = known.flat_map{ |s| [s, s+"-"] } # \w does not include -
class Address
def initialize address, display_name, domain
@address = address
@display_name = display_name
@domain = domain
end
attr_reader :address, :display_name, :domain
def self.from_header h # returns an Array of Addresses
@electron0zero
electron0zero / graphite-jenkins.py
Created March 12, 2018 17:06 — forked from russss/graphite-jenkins.py
Quick script to send Jenkins successful build durations to Graphite
#!/usr/bin/env python
""" Sends Jenkins build duration statistics to Graphite. """
import requests
import json
from graphite import Graphite # This is our closed-source library but you get the idea.
JENKINS_URL='http://jenkins'
GRAPHITE_HOST='10.x.x.x'
GRAPHITE_PREFIX='jenkins.main.build_time.'
@electron0zero
electron0zero / get-jenkins-build-time.groovy
Created March 12, 2018 16:58 — forked from batmat/get-jenkins-build-time.groovy
Quick Jenkins system groovy script to compute how many minutes of build you had in the last N hours
def numberOfHoursBack = 7*24
def totalBuildTime =
Jenkins.instance.getItems(Job.class).collect { job ->
job.getBuilds().byTimestamp(System.currentTimeMillis()-numberOfHoursBack*60*60*1000, System.currentTimeMillis())
}
.flatten()
.collect { build -> build.getDuration() }
.sum()
@electron0zero
electron0zero / main.scss
Created March 7, 2018 19:24 — forked from gembarrett/main.scss
SASS version of kgrz's responsive stylesheet for Jekyll
/* A modified default Jekyll theme
/*
/* Improvements include:
/*
/* 1. Responsive Template: Added media-queries so that it works well
/* on a wide-range of screens.
/*
The MIT License (MIT)

Image source

https://gyazo.com/eb5c5741b6a9a16c692170a41a49c858.png

Try resize it!

  • ![](https://gyazo.com/eb5c5741b6a9a16c692170a41a49c858.png | width=100)
@electron0zero
electron0zero / Rakefile
Last active April 24, 2018 20:04 — forked from mlafeldt/Rakefile
My Jekyll Rake tasks
# frozen_string_literal: true
# Rake tasks for Jekyll
require 'rake/clean'
require 'stringex'
DEFAULT_TEXT = "Add first paragraph here \n\n\<\!--more--\>\n\nAdd more content here"
PAGES_DIR = 'pages'
POSTS_DIR = '_posts'
DRAFTS_DIR = '_drafts'