Skip to content

Instantly share code, notes, and snippets.

@NiranjanSarade
NiranjanSarade / pdf_parser.rb
Created December 6, 2022 10:03
Sample pdf Parsing script to parse Invoice pdf to get the Balance Due amount at the end of the page
require 'pdf-reader'
regex = /Balance Due:(\s)+[$].*/
reader = PDF::Reader.new("Sep2022Invoice.pdf") # this is an example
p reader.page_count # e.g. 3
txt = reader.pages.last.text.split(/w+/).last # Balance Due: $44,480.07
p regex.match(txt).to_s.split.last # $44,480.07
@NiranjanSarade
NiranjanSarade / large_file_read.rb
Created June 21, 2014 15:30
Read large csv file (in GBs) in chunks of memory - replace old text with new text and create new file.
require 'rubygems'
class File
def self.sequential_read(file_path,chunk_size=nil)
open(file_path) do |f|
f.each_chunk(chunk_size) do |chunk|
yield chunk
end
end
end
@NiranjanSarade
NiranjanSarade / exchange_rates.rb
Created June 15, 2014 06:36
Historical Currency Exchange Rates with openexchangerates.org- Getting USD to other currencies conversion rates with openexchangerates api
require 'rubygems'
require 'json'
require 'active_support/core_ext'
require 'date'
require 'rest-client'
require 'fastercsv'
APP_ID = "<your_app_id>"
# e.g. Get monthy currency (USD) exchange rates from 31st Jan 2008 to 31st Dec 2010
# http://openexchangerates.org/api/historical/2009-10-10.json?&app_id=<your_app_id>
@NiranjanSarade
NiranjanSarade / currency_conversion.rb
Last active August 29, 2015 14:02
Historical Exchange Rates with exchange-rates.org - Getting USD to any currency(e.g. INR) historical rates
require 'rubygems'
require 'active_support/core_ext'
require 'date'
require 'rest-client'
# e.g. Get monthy exchange rates - USD to INR from 2008 to 2013
# http://www.exchange-rates.org/Rate/USD/INR/06-13-2014
start_date = Date.parse("2007-21-31")
(1..72).each do |month|
@NiranjanSarade
NiranjanSarade / oracle sequence
Created April 26, 2012 14:25
Avoid oracle sequence during ActiveRecord model record insert
ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.class_eval do
alias_method :orig_next_sequence_value, :next_sequence_value
def next_sequence_value(sequence_name)
if sequence_name == 'autogenerated'
id
else
orig_next_sequence_value(sequence_name)
end
end
end
@NiranjanSarade
NiranjanSarade / DeviseRecoverableModuleExtension
Created March 26, 2012 14:22
DeviseRecoverableModuleExtension - set failed_attempts to 0 on reset password by token
module Devise
module Models
module Recoverable
module ClassMethods
def reset_password_by_token(attributes={})
recoverable = find_or_initialize_with_error_by(:reset_password_token, attributes[:reset_password_token])
recoverable.reset_password!(attributes[:password], attributes[:password_confirmation]) if recoverable.persisted?
recoverable.failed_attempts = 0
recoverable.save(:validate => false)
recoverable
@NiranjanSarade
NiranjanSarade / gc.c
Created March 5, 2012 10:39
ruby 1.9.3 interpreter
VALUE rb_data_typed_object_alloc(VALUE klass, void *datap, const rb_data_type_t *type)
{
NEWOBJ(data, struct RTypedData);
if (klass) Check_Type(klass, T_CLASS);
OBJSETUP(data, klass, T_DATA);
data->data = datap;
data->typed_flag = 1;
@NiranjanSarade
NiranjanSarade / ruby.h
Created March 5, 2012 10:37
ruby 1.9.3 interpreter
#define TypedData_Make_Struct(klass, type, data_type, sval) (\
(sval) = ALLOC(type),\
memset((sval), 0, sizeof(type)),\
TypedData_Wrap_Struct((klass),(data_type),(sval))\
)
#define TypedData_Wrap_Struct(klass,data_type,sval)\
rb_data_typed_object_alloc((klass),(sval),(data_type))
@NiranjanSarade
NiranjanSarade / iseq.c
Created March 5, 2012 10:34
ruby 1.9.3 interpreter
VALUE rb_iseq_new_main(NODE *node, VALUE filename, VALUE filepath)
{
rb_thread_t *th = GET_THREAD();
VALUE parent = th->base_block->iseq->self;
return rb_iseq_new_with_opt(node, rb_str_new2("<main>"), filename, filepath, INT2FIX(0),
parent, ISEQ_TYPE_MAIN, &COMPILE_OPTION_DEFAULT);
}
@NiranjanSarade
NiranjanSarade / gist:915666
Created April 12, 2011 15:05
convert_from_LF_to_CRLF
def self.convert_file_to_include_CRLF unix_file_name, win_file_name
if RUBY_PLATFORM =~ /win32/
path = "#{RAILS_ROOT}/tmp".gsub('/', '\\')
else
path = "#{RAILS_ROOT}/tmp"
end
success = system <<-CMD
cd #{path}
sed 's/$/\r/' #{unix_file_name} > #{win_file_name}