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 / display_resque_failed_job.rb
Last active December 31, 2015 15:09
Display the 20 last Resque failed jobs in console.
start_at = Resque::Failure.count - 20
finish_at = Resque::Failure.count
Resque::Failure.all(start_at, finish_at).each do |job|
puts "----------"
puts "#{job["failed_at"]}"
puts "#{job["payload"]}"
puts "#{job["error"]}"
job["backtrace"].each do |line|
puts "#{line}"
end
@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 / my_fir.rb
Last active December 23, 2022 08:07
Display a Fir Tree On Terminal
def usage
abort 'Wrong number of argument: ruby my_fir.rb FIR_SIZE'
end
def display_trunk(width)
trunk_width = trunk_height = ((width / 2) - 2).round
(1..trunk_height).each { puts ("|" * trunk_width).center(width) }
end
def display_level(width, line_length)
@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)