Tuesday, February 24, 2009

concat multiple arrays

hi,
joining multiple arrays is easy using "+"

we can do that using a single method as follows,

class Array
def concat_multi *lists
lists.each {|list| self.concat(list) }
end
end

a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
d = [10, 11]

a.concat_multi(b, c, d)
p a

Thursday, February 19, 2009

Convert a String into Time

require'rubygems'
=> true
require'time'
=> true

a="Thu Feb 19 18:16:33 +0530 2009"
=> "Thu Feb 19 18:16:33 +0530 2009"
a=Time.parse(a)
=> Thu Feb 19 18:16:33 +0530 2009

a.day
=> 19
a.year
=> 2009
a.month
=> 2
a.hour
=> 18
a.min
=> 16
a.sec
=> 33


thanks..

Monday, February 16, 2009

Weekday or not

hi,
wanna find whether a day is week day or not??

i have tried tht here. ,


date=date.today
[0,6].include?(date.wday)
------------------------------------------------------------------------
  require 'date'

class Date
def weekend?
self.wday == 0 || self.wday == 6
end
end

d1 = "9 December 2008"
d2 = "13 December 2008"

p Date.parse(d1).weekend? # false
p Date.parse(d2).weekend? # true
------------------------------------------------------------------------

I have used the code for calculation of weekdays between 2 dates.,

require 'date'
d1 = Date.new( 2008, 11, 1 )
d2 = Date.new( 2008, 12, 31 )

WEEKDAY_NUMBERS = [1,2,3,4,5]
weekdays = (d1..d2).select{ |d| WEEKDAY_NUMBERS.include?( d.wday ) }
p weekdays.length

----------------------------------------------------

Friday, February 13, 2009

Displaying Rss in rails

hi,
i have displayed an array as rss feeds, using rails..

here s the code..
In the controller,

h={}
h[:summary] =summary
h[:description]=description
h[:dtstart]=dtstart.to_s
h[:url]=url
@arr << h

render :layout =>false
response.headers["Content-Type"]= "application/xml; charset=utf-8"

In the View file., Filename is , *.rxml

xml.instruct!
xml.rss "version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/" do
xml.channel do |feed|
feed.title("My great blog!")
feed.description("description")
feed.link("ur url link")

@arr.each { |p|
feed.item do |item|
item.title p[:summary]
item.description p[:description]
item.link p[:url]
item.pubdate p[:dtstart]
end
}
end
end

now it is displayed as xml feed in the view file..
Thanks .

Tuesday, February 10, 2009

ICAL-RSS convertion using vpim gem

hi,
i have done the same rss to ical ,vise-versa using vpim gem..

here is the gem home..
http://vpim.rubyforge.org/

This is my code..
require 'rubygems'
require 'vpim/icalendar'
require'feed_tools'
require'digest/md5'

feed = FeedTools::Feed.open("http://rss.cnn.com/rss/edition_world.rss")
cal = Vpim::Icalendar.create2
feed.items.each do |item|
cal.add_event do |e|
e.dtstart item.time
e.dtend item.time
e.summary item.title.to_s
e.description item.description.to_s
e.url item.link
e.uid Digest::MD5.hexdigest("#{item.title} #{item.link}")
e.access_class "PUBLIC"
e.transparency 'OPAQUE'
now = Time.now
e.created now
e.lastmod now
e.organizer do |o|
o.cn = "Example Organizer, Mr."
o.uri = "mailto:organizer@example.com"
end
end
end
icsfile = cal.encode
f=File.open('2.ics','w')
f.write(icsfile)
puts '--- Encode:'

puts icsfile

puts '--- Decode:'

cal = Vpim::Icalendar.decode(icsfile).first

cal.components do |e|
puts e.summary
puts e.description
puts e.dtstart.to_s
puts e.dtend.to_s
puts e.url
end

Monday, February 9, 2009

Ical to Rss Converter

hi,
first execute the rss to ical converter, store it in a file, then use this script, to convert it into rss.

require 'rubygems'
require 'net/http'
require 'uri'
require 'time'

class Time
def self.gcalschema(tzid) # We may not be handling Time Zones in the best way...
tzid =~ /(\d\d\d\d)(\d\d)(\d\d)T(\d\d)(\d\d)(\d\d)Z/ ? # yyyymmddThhmmss
Time.xmlschema("#{$1}-#{$2}-#{$3}T#{$4}:#{$5}:#{$6}") :
nil
end
end


class ICal

attr_accessor :hash, :raw
def initialize(ical_data)
self.raw = ical_data
self.hash = self.parse_ical_data(self.raw)
end

def parse_ical_data(data)
data.gsub!(/\\\n/, "\\n")
data.gsub!(/[\n\r]+ /, "\\n")
lines = data.split(/[\n\r]+/)
structure = [{}]
keys_path = []
last_is_array = false
lines.each do |line|
line.gsub!(/\\n/, "\n")
pair = line.split(':')
name = pair.shift
value = pair.join(':')
case name
when 'BEGIN' #Begin Section
if structure[-1].has_key?(value)
if structure[-1][value].is_a?(Array)
structure[-1][value].push({})
last_is_array = true
else
structure[-1][value] = [structure[-1][value], {}]
last_is_array = true
end
else
structure[-1][value] = {}
end
keys_path.push(value)
structure.push({})
when 'END' #End Section
if last_is_array
structure[-2][keys_path.pop][-1] = structure.pop
last_is_array = false
else
structure[-2][keys_path.pop] = structure.pop
end
else #Within last Section
structure[-1][name] = value
end
end
structure[0]
end
end

class Parse
attr_accessor :url, :ical, :xml, :product_id, :version, :scale, :method, :time_zone_name, :time_zone_offset, :events
def parse_from_ical
rawdata = self.calendar_raw_data
return nil unless rawdata
self.ical = ICal.new(rawdata)
self.version = self.ical.hash['VCALENDAR']['VERSION']
self.scale = self.ical.hash['VCALENDAR']['CALSCALE']
self.method = self.ical.hash['VCALENDAR']['METHOD']
self.product_id = self.ical.hash['VCALENDAR']['PRODID']
self.ical.hash['VCALENDAR']['VEVENT'] = [self.ical.hash['VCALENDAR']['VEVENT']] unless self.ical.hash['VCALENDAR']['VEVENT'].is_a?(Array)
self.ical.hash['VCALENDAR']['VEVENT'].each do |e|
if !e.nil?
st = e['DTSTART']
et = e['DTEND']
#et = (Time.gcalschema("#{e["DTEND;TZID=#{self.time_zone_name}"] || "#{e['DTEND;VALUE=DATE']}T000000"}Z") || Time.gcalschema(e['DTEND'])) + tzadjust
# DTSTART;TZID=America/New_York:20070508T070000
@xtr=self.add_event(Event.new(
:start_time => st,
:end_time => et,
:location => e['LOCATION'],
:created_at => Time.gcalschema(e['CREATED']),
:updated_at => Time.gcalschema(e['LAST-MODIFIED']),
:summary => e['SUMMARY'],
:description => e['DESCRIPTION'],
:recurrance_rule => e['RRULE']
), false) # (disable sorting until done)
@events.reject! {|e| e.start_time.nil?}
@events.sort! {|a,b| a.start_time <=> b.start_time }
puts @xtr
end
end
end

def calendar_raw_data
f=File.open("rss2ical.ics","r")
a=f.readlines
return a.to_s
end

def add_event(event, sortit=true)
self.events = [] unless self.events.is_a?(Array)
self.events << event
@events.sort! {|a,b| a.start_time <=> b.start_time } if sortit
event
end
end

class Event
attr_accessor :start_time, :end_time, :location, :created_at, :updated_at, :summary, :description, :recurrance_rule
def initialize(attributes={})
attributes.each do |key, value|
self.send("#{key.to_s}=", value)
end
end
end

a=Parse.new
a.parse_from_ical

Saturday, February 7, 2009

RSS to Ical Converter

hi,
this program gets the rss feed using the feed_tools gem.
then converts into ical. then i ve written it in a file.
hope it will be useful.


require'rubygems'
require'feed_tools'
require'digest/md5'
require 'icalendar'
require 'date'

feed = FeedTools::Feed.open("http://timesofindia.indiatimes.com/rssfeeds/-2128932452.cms")
cal = Icalendar::Calendar.new
cal.custom_property("METHOD","PUBLISH")
feed.items.each do |item|
event = Icalendar::Event.new
event.url=item.link
event.uid=Digest::MD5.hexdigest("#{item.title} #{item.link}")
event.dtstart = item.time.strftime("%Y%m%dT%H%M%S")
event.dtend = item.time.strftime("%Y%m%dT%H%M%S")
event.summary = item.title.to_s
event.description = item.description.to_s
event.klass = "PUBLIC"
cal.add_event(event)
end
ical= cal.to_ical
f=File.open("rss2ical.ics","w")
f.write(ical)

puts ical.to_html