Skip to content

Instantly share code, notes, and snippets.

View mehdi-farsi's full-sized avatar

Mehdi FARSI mehdi-farsi

View GitHub Profile
@mehdi-farsi
mehdi-farsi / failed_job.rake
Created January 30, 2014 11:11
Requeue failed job
namespace :resque do
nemaspace :failed_job do
desc 'restart the N latest failed jobs and remove them from failed jobs queue.'
task :restart, [:n_latest] => [:environment] do |t, args|
args.with_defaults(n_latest: 20)
start_at = Resque::Failure.count - args[:n_latest] - 1
finished_at = Resque::Failure.count - 1
(start_at..finished_at).each do |index|
Resque::Failure.requeue_and_remove(index)
end
@mehdi-farsi
mehdi-farsi / command
Last active August 29, 2015 14:07
Parse and display an unformatted JSON on Terminal
## Requirements:
##
## ruby installed
$> gem install awesome_print
Fetching: awesome_print-1.2.0.gem (100%)
Successfully installed awesome_print-1.2.0
Parsing documentation for awesome_print-1.2.0
Installing ri documentation for awesome_print-1.2.0
Done installing documentation for awesome_print after 0 seconds
@mehdi-farsi
mehdi-farsi / shell.sh
Last active August 29, 2015 14:07
Change EXIF orientation using CLI
# Required:
# Mac OS
$> brew install exiftool
# Ubuntu && Debian
$> sudo apt-get install libimage-exiftool-perl
# Example: Set orientation to Normal (0°)
@mehdi-farsi
mehdi-farsi / dir.rb
Last active August 29, 2015 14:07
Fetch filenames using filter and REGEX
# In the following example some simple default options are set.
class Dir
@list_options = { hidden_files: /^\./, visible_files: /^[^\.]/, files: // }
def self.list(path = '.', option = :visible_files)
entries(path).grep(@list_options[option])
end
end
# Examples
@mehdi-farsi
mehdi-farsi / extract_and_store.rb
Created October 29, 2014 14:58
Extract and store a value from string using REGEXP
require 'minitest/autorun'
class ExtractAndSortTest < MiniTest::Unit::TestCase
def setup
@lines = [
'Article 1 -- (45)',
'Article 2 -- (42)',
'Article 3 -- (62)',
'Article 4 -- ()'
]
@mehdi-farsi
mehdi-farsi / scraper.rb
Last active August 29, 2015 14:08
Simple Web Scrapping Script Example Using Ruby And Nokogiri
# Example: List of all sports of the Olympic Games
# HTML Structure:
#
# <div id="content">
# <ul>
# <li>
# <a href="...">SPORT'S NAME</a>
# </li>
# </ul>
# </div>
@mehdi-farsi
mehdi-farsi / date.rb
Created November 2, 2014 15:39
Date Helpers
class Time
def self.yesterday
Time.now - (24 * 60 * 60)
end
def self.tomorrow
Time.now + (24 * 60 * 60)
end
def self.day_after_tomorrow
@mehdi-farsi
mehdi-farsi / web.rb
Created November 5, 2014 23:05
[Web Scraping/Crawling] Calling redirect URL if 301/302 HTTP Status Code
# Concrete case: Google search result URLs
#
# Requirement:
# gem install nokogiri
require 'nokogiri'
require 'net/http'
require 'uri'
def get(url)
@mehdi-farsi
mehdi-farsi / string.rb
Last active August 29, 2015 14:08
Ruby Trick To Extract an Email From a String Using Regex
# The goal of this code snippet isn't to make an email validator..
# So the REGEX is not steady but works for simple cases.
#
# You can extract whatever you want by changing the Regex. :)
line = "Congratulation ! here is your email : mehdi-farsi@long-and_boring-company_name.com. Are you happy ?"
email = line[/(\w|[\.\-])+@(\w|[\.\-])+\.[a-zA-Z]+/]
@mehdi-farsi
mehdi-farsi / composition_over_inheritance.rb
Created November 11, 2014 00:24
Example of "Composition Over Inheritance" Concept
class Vehicle; end
class Car < Vehicle
def initialize
# A composition to separate standalone Engine class.
# That permit to create a class Bicycle without give it
# the property of an engine Vehicle.
@engine = GasolineEngine.new
end