Ruby on Rails操作Google Calendar(一)

最近遇到个项目需要把Google Calendar嵌入到现行系统当中作为用户的日程表,随即彻底研究了一下Google Calendar的API。实际上,Google Calenar的说明文档从实例到视频全权概括,可以说是相当详细了。但是唯独对Ruby链接Calendar的描述只有寥寥几句,也许对于Google来说Ruby远不及Java,PHP等等语言有影响力。

简单理解一下Google Calendar的数据类型。在Google Calendar中主要有两部分内容是可以由开发者通过API直接操作的:第一,当然是Calendar;其次,活动Event。在Google Calendar中一个用户可以同时创建多个Calendar,其中有一个为用户自身的默认Calendar。创建的同时,Google Calendar为每一个Calendar设置唯一的Entry ID以便区分。同理,每一个被创建的Event也有自己的EntryID。在Calendar和Event创建之后如果需要执行update,Del操作,只要获取它们的相应ID对应的内容向服务器发出操作请求即可。对于外部的开发者而言一个Calendar/Event的内容就是一个<Entry>xml

Calendar:

<entry>    

<id>http://www.google.com/calendar/feeds/default/allcalendars/full/rf1c66uld6dgk2t5lh43svev6g%40group.calendar.google.com</id>

<published>2007-07-11T22:10:30.258Z</published>    

<updated>2007-07-11T21:50:15.000Z</updated>    

<title type="text">Little Giants</title>    

<summary type="text">This calendar contains practice times and this season's Little League game schedule.  Go Little Giants!</summary>    

<link rel="alternate" type="text/html" href="http://www.google.com/calendar/feeds/rf1c66uld6dgk2t5lh43svev6g%40group.calendar.google.com/private/full"/>    

<link rel="http://schemas.google.com/acl/2007#accessControlList" type="application/atom+xml" href="http://www.google.com/calendar/feeds/rf1c66uld6dgk2t5lh43svev6g%40group.calendar.google.com/acl/full"/>    

<link rel="self" type="application/atom+xml" href="http://www.google.com/calendar/feeds/default/allcalendars/full/rf1c66uld6dgk2t5lh43svev6g%40group.calendar.google.com"/>    

<author>      

<name>Little Giants</name>    

</author>    

<gCal:timezone value="America/Los_Angeles"/>    

<gCal:hidden value="false"/>    

<gCal:color value="#5A6986"/>    

<gCal:selected value="false"/>    

<gCal:accesslevel value="owner"/>    

<gd:where valueString="San Fransisco"/>  

</entry>

Event:

<entry>

<id>http://www.google.com/calendar/feeds/jo@gmail.com/private-magicCookie/full/entryID</id> <published>2006-03-30T22:00:00.000Z</published>

<updated>2006-03-28T05:47:31.000Z</updated>

<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2005#event'></category>

<title type='text'>Lunch with Darcy</title>

<content type='text'>Lunch to discuss future plans.</content>

<link rel='alternate' type='text/html'       href="http://www.google.com/calendar/event?eid=aTJxcnNqbW9tcTJnaTE5cnMybmEwaW04bXMgbWFyY2guam9AZ21haWwuY29t" mce_href="http://www.google.com/calendar/event?eid=aTJxcnNqbW9tcTJnaTE5cnMybmEwaW04bXMgbWFyY2guam9AZ21haWwuY29t" title='alternate'>

</link> <link rel='self' type='application/atom+xml'       href="http://www.google.com/calendar/feeds/jo@gmail.com/private-magicCookie/full/entryID" mce_href="http://www.google.com/calendar/feeds/jo@gmail.com/private-magicCookie/full/entryID">

</link>

 <author> <name>Jo March</name> <email>jo@gmail.com</email> </author>

<gd:transparency value='http://schemas.google.com/g/2005#event.opaque'></gd:transparency> <gd:eventStatus value='http://schemas.google.com/g/2005#event.confirmed'></gd:eventStatus> <gd:comments> <gd:feedLink href="http://www.google.com/calendar/feeds/jo@gmail.com/private-magicCookie/full/entryID/comments/" mce_href="http://www.google.com/calendar/feeds/jo@gmail.com/private-magicCookie/full/entryID/comments/"></gd:feedLink> </gd:comments>

<gd:when startTime='2006-03-30T22:00:00.000Z' endTime='2006-03-30T23:00:00.000Z'></gd:when> <gd:where></gd:where>

</entry>

仔细读读上面的xml就可以了解Calendar/Event包含那些主要的属性,那些属性是可以通过API修改的了。

 


 

便于外部调用,我们把下面的方法都写在一个Google_Calendar Class当中,我把它放在hepler里面了也不知道对不对,反正运行没有问题。

首先,操作Google Calendar首先需要登陆权限,实现方法如下:  

def login(email, pwd, source='googlecalendar.rubyforge.org-googlecalendar-default')    

# service   The string cl, which is the service name for Google Calendar.    

@user_id = email    

response = Net::HTTPS.post_form(URI.parse("https://www.google.com/accounts/ClientLogin"), { 'Email' => email, 'Passwd' => pwd, 'source' => source, 'accountType' => 'HOSTED_OR_GOOGLE', 'service' => 'cl'})    

response.error! unless response.kind_of? Net::HTTPSuccess    

@token = response.body.split(/=/).last    

@headers = { 'Authorization' => "GoogleLogin auth=#{@token}",'Content-Type'  => 'application/atom+xml' }     

return @token  

end  


创建新Calendar:

1.创建Calendar xml模板  参数:calendar={}是一个Map包含:title,:summary,:timezone,:where

def new_calendar_template(calendar={})

content = <<EOF  

<entry xmlns='http://www.w3.org/2005/Atom'        xmlns:gd='http://schemas.google.com/g/2005'        xmlns:gCal='http://schemas.google.com/gCal/2005'>  

<title type='text'>#{calendar[:title]}</title>  

<summary type='text'>#{calendar[:summary]}</summary>  

<gCal:timezone value='#{calendar[:timezone]}'></gCal:timezone>  

<gCal:hidden value='false'></gCal:hidden>  

<gCal:color value='#2952A3'></gCal:color>  

<gCal:selected value='true'/>  

<gCal:accesslevel value='owner'/>  

 <gd:where valueString='#{calendar[:where]}'>  

</gd:where>

</entry>

EOF

end

2.Post xml到google服务器 url='/calendar/feeds/default/owncalendars/full'

def post_calendar(xml,url)  

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

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

when Net::HTTPSuccess, Net::HTTPRedirection    

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

 case response      

when Net::HTTPSuccess, Net::HTTPRedirection      

 return redirect_response    

else      

response.error!    

end  

else    

response.error!  

end

end

如果创建成功post_calendar方法返回response 201 中包含创建Calendar的xml内容。失败则返回错误号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值