Ruby on Rails操作Google Calendar(二)

 Ruby on Rails操作Google Calendar(一)里面讲了如何创建新Calendar,下面继续更新操作

前面说过Google Calendar是通过entry ID来识别Calendar的,所以如果希望更新Calendar首先需要得到这个ID,下面这个方法实现获取当前用户的所有Calendar对象:  

def get_calendars    

http = Net::HTTP.new('www.google.com', 80)    

response, data = http.get("http://www.google.com/calendar/feeds/" + @user_id, @headers)    

case response    

when Net::HTTPSuccess, Net::HTTPRedirection      

redirect_response, redirect_data = http.get(response['location'], @headers)      

case response      

when Net::HTTPSuccess, Net::HTTPRedirection        

doc = REXML::Document.new redirect_data       

doc.elements.each('//entry')do |e|

         id = e.elements['id'].text         

         title = e.elements['title'].text         

         url = e.elements['link'].attributes['href']         

         @calendars << GCalendar.new(title, url.sub!("http://www.google.com",''))       

end        

return redirect_response      

else         response.error!      

end     else       response.error!    

end  

end

class GCalendar  

attr_reader :id, :title, :url  

def initialize(id, title, url)    

@id = id    

@title = title    

@url = url  

end

end 

上面的get_calendars方法实现通过get方法获取用户所有的Calendar并以GCalendar对象的形式,存储在@calendars数组中同时返回response。我们可以通过get_calendars返回的response.body中得到所有Calendar的<entry>内容,当然也包含ID。取得需要修改的Calendar entry ID 之后update就可以实现了。

 

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

1.创建update <entry> 的xml内容

def update_calendar_template(calendar={})

content = <<EOF   <entry xmlns='http://www.w3.org/2005/Atom' xmlns:gCal='http://schemas.google.com/gCal/2005' xmlns:gd='http://schemas.google.com/g/2005'>   <id>http://www.google.com/calendar/feeds/default/owncalendars/full/#{calendar[:id]}</id>   <published>2008-07-02T09:48:52.745Z</published>   <updated>2008-07-02T09:48:52.000Z</updated>   <title type='text'>#{calendar[:title]}</title>   <summary type='text'>#{calendar[:summary]}</summary>   <link rel='alternate' type='application/atom+xml' href='http://www.google.com/calendar/feeds/#{calendar[:id]}/private/full'/>   <link rel='http://schemas.google.com/acl/2007#accessControlList' type='application/atom+xml' href='http://www.google.com/calendar/feeds/#{calendar[:id]}/acl/full'/>   <link rel='self' type='application/atom+xml' href='http://www.google.com/calendar/feeds/default/owncalendars/full/#{calendar[:id]}'/>   <link rel='edit' type='application/atom+xml' href='http://www.google.com/calendar/feeds/default/owncalendars/full/#{calendar[:id]}'/>   <author>      <name>#{calendar[:title]}</name>   </author>   <gCal:timezone value='#{calendar[:timezone]}'/>   <gCal:hidden value='false'/>   <gCal:color value='#2952A3'/>   <gCal:selected value='true'/>   <gCal:accesslevel value='owner'/>   <gd:where valueString='#{calendar[:where]}'/>   </entry>

EOF

end

2.把update <entry>xml内容put 到google服务器

def put_calendar(xml,url)  

http = Net::HTTP.new('www.google.com', 80)  

response, data = http.put(url, xml, @headers)  

case response    

when Net::HTTPSuccess, Net::HTTPRedirection    

redirect_response, redirect_data = http.put(response['location'], xml, @headers)    

case response      

when Net::HTTPSuccess, Net::HTTPRedirection      

return redirect_response    

else      

response.error!    

end  

else    

response.error!  

end

end

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值