Skip to content

Instantly share code, notes, and snippets.

View rajeevkannav's full-sized avatar

Rajeev Sharma rajeevkannav

View GitHub Profile
require 'mechanize'
@username = '[email protected]'
@password = 'hi2u'
@download_path = File.expand_path 'downloads'
@wget_cookie = File.expand_path(File.dirname(__FILE__)) + '/wget-cookies.txt'
unless File.directory? @download_path
puts "@{download_path} doesn't exist!"
exit
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')
start = Time.now
1000000.times do |job|
@a = "sachin"
end
stop1 = Time.now
puts @a
print 'diff --- ' , (stop1 - start)
puts ''
@rajeevkannav
rajeevkannav / gist:7702119
Created November 29, 2013 06:09
Vcard Parser
require 'rubygems'
require 'vpim'
class String
alias_method :each, :each_line
end
class VcardParser
attr_accessor :full_name, :last_name, :first_name, :title, :organisation, :email, :phones, :url, :address, :note,
:fax_phone, :home_phone, :mobile_phone, :office_phone
class Post < ActiveRecord::Base
#Validations
validates :data, presence: true, length: {in: 6..200}
validates :source, presence: true, length: {in: 3..200}
validates :state, presence: true, length: {in: 0..20}
#Callbacks
after_save :padded_data
#Associations
has_many :subcategories, class_name: 'Category'
has_one :category, :through => :subcategories
@rajeevkannav
rajeevkannav / backup.rb
Last active January 1, 2016 12:09
Rake Task - table rename - DUMP - MySQL Table
namespace :db do
desc "swap table names and backup data to sql."
task :backup => [:environment] do
# can return if any condition fails.
begin
base_path = Rails.root
backup_location = File.join(base_path, 'backups')
FileUtils.mkdir_p(backup_location) # create backup folder if doesn't exists
filename = (Dir.glob("#{backup_location}/*.{sql}").count + 1).to_s + '_appended_filename.sql'
backup_file = File.join(backup_location, filename)
@rajeevkannav
rajeevkannav / mysqldump.rb
Created February 27, 2014 21:13
mysqldump
namespace :mysql do
desc "simple mysql db dump" +
'Can be used as rake mysql:dumpher[backup_location, expected_dumpfile_size, email_notification_required]' +
'For e.g In My case rake mysql:dumpher[/home/rajeev/Public,20,true] RAILS_ENV=production'
task :dumpher, [:target_bucket, :expected_dump_size, :notify] => [:environment] do |t, args|
args.with_defaults(host: 'localhost', target_bucket: '/', expected_dump_size: 25)
_db_config = ActiveRecord::Base.configurations[Rails.env]
_db = _db_config['database']
file = "#{_db}_#{Time.now.strftime('%Y_%m_%d_%H_%M_%S')}.sql.gz"

Commands examples

If the namespace is not used then the commands will perform on top of the default database. bundle exec rake db:create bundle exec rake db:migrate

By using the namespace we are going to use all the configuration for our alternate DB. bundle exec rake store:db:create bundle exec rake store:db:migrate

@rajeevkannav
rajeevkannav / gist:47f7ab1aeba41bd011b2
Last active August 29, 2015 14:04
DoubleLinkedListRuby
class Node
attr_accessor :prev_node, :next_node
def initialize(data)
@data = data
end
def has_next?()
@next_node ? true : false
end
#!/bin/sh
## Description:
## install script of s3fs
##
## Usage:
## install-s3fs.sh [mountpoint] [bucketName] [accessKeyId] [secretAccessKey]
MOUNT_POINT=$1
BUCKET_NAME=$2