# THIS MODULE IS USED TO CHECK FROM http://www.trynt.com/creditcard-validation-api
# API TO CHECK WHETHER CREDIT CARD NUMBER IS NOT A DUMMY CARD NUMBER.
require 'net/http'
require 'uri'
require 'rexml/document'
module CreditCardVarification
include REXML
def is_credit_card_valid?(credit_card_no, year, month)
begin
dummy_card = %w(371449635398431 378734493671000 5610591081018250 30569309025904 38520000023237 6011111111111117 6011000990139424
3530111333300000 3566002020360505 5555555555554444 5105105105105100 4111111111111111 4012888888881881 4222222222222 76009244561
5019717010103742 6331101999990016)
action = get_body_from_url(credit_card_no, year, month)
if defined? Document.new(action).elements['Trynt/Credit-Card-Validation/Result'].text
result = Document.new(action).elements['Trynt/Credit-Card-Validation/Result'].text
unless result.nil?
if result.eql?("Appears Valid") && !dummy_card.include?credit_card_no.to_s)
return true
end
end
end
return false
rescue
return false
end
end
def get_body_from_url(credit_card_no, year, month)
url = "http://www.trynt.com/credit-card-validation-api/v1/?cc=#{credit_card_no}&y=#{year}&m=#{month}"
url = URI.parse(url)
res = Net::HTTP.get_response(url)
return res.body
end
end
Thursday, June 4, 2009
Using Thread to store session variable
# THE BELOW MENTIONED FUNCTION WILL BE USED TO STORE THE SESSION VALUE SO THAT SESSION VARIABLES CAN BE USED IN MODEL...
module UserInfo
def get_current_user_in_model
Thread.current[:user]
end
def self.get_current_user_in_model=(user)
Thread.current[:user] = user
end
end
module UserInfo
def get_current_user_in_model
Thread.current[:user]
end
def self.get_current_user_in_model=(user)
Thread.current[:user] = user
end
end
Tuesday, May 26, 2009
Insert At Cursor -- Javascript
function insertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
}
// calling the function
insertAtCursor(document.formName.fieldName, 'this value');
Friday, May 15, 2009
Different ways of Validating Date of Birth in Model
def validate
if (date_of_birth.month==2 && date_of_birth.month.day > 29)
errors.add(:birthdate, "Invalida date")
end
end
OR
validates_each :date_of_birth do |record, attr, value|
record.errors.add attr, "is not a valid date. You must be at least 18 years old to sign in." if value.blank?
end
OR
def validate
if date_of_birth.month.blank? || date_of_birth.day.blank? || date_of_birth.year.blank?
errors.add_to_base "day, month and year cannot be blank."
end
end
if (date_of_birth.month==2 && date_of_birth.month.day > 29)
errors.add(:birthdate, "Invalida date")
end
end
OR
validates_each :date_of_birth do |record, attr, value|
record.errors.add attr, "is not a valid date. You must be at least 18 years old to sign in." if value.blank?
end
OR
def validate
if date_of_birth.month.blank? || date_of_birth.day.blank? || date_of_birth.year.blank?
errors.add_to_base "day, month and year cannot be blank."
end
end
Validates_presence_of : customise field name
validates_presence_of :id_country, :message => "Country can't be blank"
class Adresse < ActiveRecord::Base
HUMANIZED_COLLUMNS = {:id_country => "Country"}
def self.human_attribute_name(attribute)
HUMANIZED_COLLUMNS[attribute.to_sym] || super
end
end
class Adresse < ActiveRecord::Base
HUMANIZED_COLLUMNS = {:id_country => "Country"}
def self.human_attribute_name(attribute)
HUMANIZED_COLLUMNS[attribute.to_sym] || super
end
end
Monday, April 27, 2009
Gets Days from the difference between two dates.
Example:
> a = "2009-04-01 18:33:05"
> b = Time.now
> c = b.to_date - a.to_date
O/P: Rational(4, 1)
> c.to_i
=> 4
OR
> c = (b.to_date - a.to_date).to_i
=> 4
> a = "2009-04-01 18:33:05"
> b = Time.now
> c = b.to_date - a.to_date
O/P: Rational(4, 1)
> c.to_i
=> 4
OR
> c = (b.to_date - a.to_date).to_i
=> 4
Generate Unique id
class Unique
def self.generate_unique_id(len)
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
generate_unique_id = ""
1.upto(len) { |i| generate_unique_id << chars[rand(chars.size-1)] }
return generate_unique_id
end
end
Example:
irb> Unique.generate_unique_id(18)
=> "CCDCa6Fv1AKsIaYAAA"
def self.generate_unique_id(len)
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
generate_unique_id = ""
1.upto(len) { |i| generate_unique_id << chars[rand(chars.size-1)] }
return generate_unique_id
end
end
Example:
irb> Unique.generate_unique_id(18)
=> "CCDCa6Fv1AKsIaYAAA"
Subscribe to:
Posts (Atom)