Skip to content

Instantly share code, notes, and snippets.

View amolpujari's full-sized avatar
💭
I may be slow to respond.

Amol Pujari amolpujari

💭
I may be slow to respond.
  • Pune
View GitHub Profile
@amolpujari
amolpujari / reverse_geocode_example.rb
Created October 18, 2012 06:06
Location model example
Loading development environment (Rails 3.2.3)
1.9.3p194 :001 > l = Location.new 'San Francisco'
=> #<Location id: nil, locatable_id: nil, locatable_type: nil, ip: nil, country: "US", state: "California", state_abbreviation: "CA", county: nil, city: "San Francisco", street: nil, address: "San Francisco", zip: "94103", lat: #<BigDecimal:6ea81b0,'0.37775002E2',18(45)>, lng: #<BigDecimal:6ea7f58,'-0.122418297E3',18(45)>, walkscore: nil, transit_score: nil, avg_school_rating: nil, created_at: nil, updated_at: nil>
1.9.3p194 :002 > ap l
#<Location:0x00000006b82e18> {
:id => nil,
:locatable_id => nil,
:locatable_type => nil,
:ip => nil,
:country => "US",
@amolpujari
amolpujari / mail_watcher.rb
Created October 29, 2012 17:16
ruby based mail watcher to trigger jenkin jobs passing args
require 'net/imap'
class MailWatcher
Server = 'imap.gmail.com'
Username = ''
Password = ''
Folder = 'INBOX'
PollingInterval = 60
CheckFileLocation = "#{File.dirname(__FILE__)}/../../tmp/checks/"
@amolpujari
amolpujari / handle_redirect_loop_if_appears.rb
Created October 31, 2012 11:17
handle_redirect_loop_if_appears
class ApplicationController < ActionController::Base
...
before_filter :handle_redirect_loop_if_appears
...
after_filter :set_session_back_url
...
def handle_redirect_loop_if_appears
@amolpujari
amolpujari / csv_reports.rb
Created December 19, 2012 08:51
quick write csv files CSVReports::Base.new objects, columns=[ :attr1, :attr2]
require 'csv'
module CSVReports
class Base
attr_accessor :columns, :column_separator, :row_separator
def initialize items, columns=[]
@items = items
self.columns = columns
Food
1405 - Amol/Renuka
184 - Omkar/Shruti
0 - Yogesh/Radhika
490 - Pushkar/Tejal
___________________
2079 that means 520 per couple
require 'active_support/core_ext'
class PricingCalc
attr_reader :bill, :usage
def initialize entire_usage
@bill = 0
@usage = 0
@entire_usage = entire_usage
end
#!/bin/bash
while :
do
clear
git --no-pager log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ci) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative --all
sleep 1
done
@amolpujari
amolpujari / concerns.rb
Last active December 15, 2015 22:48
Different ways of having modular active record models
# config/initializers/concerns.rb
class << ActiveRecord::Base
def concerned_with(*concerns)
concerns.each do |concern|
require_dependency "#{name.underscore}/#{concern}"
klass = concern.to_s.classify.constantize rescue nil
send(:include, klass) if klass.is_a? Module
end
end
end
@amolpujari
amolpujari / config.rb
Created April 8, 2013 08:19
Rails configurations
# config/initializers/config.rb
APP_CONFIG = YAML.load_file("#{Rails.root.to_s}/config/config.yml")[Rails.env]
def APP_CONFIG.[](key)
key = key.to_s
if Rails.env=='production' || Rails.env == 'staging'
p "APP_CONFIG['#{key}'] not defined." unless self.has_key? key
else
raise "APP_CONFIG['#{key}'] not defined." unless self.has_key? key
#test.rb
def mem_usage
`ps -o rss= -p #{Process.pid}`.to_i
end
def random_str(length=1)
(Object.new.hash.to_s << Object.new.hash.to_s)[0..(length-1)].to_s
end
class A