Class: Netvibes
- Inherits:
-
Object
- Object
- Netvibes
- Defined in:
- lib/netvibes/api.rb,
lib/netvibes/config.rb
Overview
Netvibes API implementation class :include: CREDITS
Before anything else :
require 'rubygems'
require 'netvibes'
How to login :
api = Netvibes.new
api.login(my_login, mypassword)
if (remember_me)
api.save_credentials
end
How to iter through tabs and How to download (and cache) tab icon (or anything else) :
api.refresh
api.tabs.each do |tab|
puts tab['title']
puts 'Icon is cached here : ' + api.download(tab['icon'])
end
How to know if a tab has some module (:postit, :todolist, :feed) :
api.tabs.each do |tab|
if api.tab_has?(tab['id'], :postit)
puts "Tab #{tab['title']} has some post-it"
end
end
How to iter through each modules in a tab :
api.tabs.each do |tab|
api.each_postit_in_tab(tab['id']) do |postit|
puts "Postit: #{postit['data']['title']}"
end
end
Constant Summary collapse
- URL =
{ :base => URI.parse('http://www.netvibes.com'), :login => URI.parse('http://www.netvibes.com/user/signIn.php'), :data => URI.parse('http://www.netvibes.com/get/userData.php'), :save => URI.parse('http://www.netvibes.com/save/userData.php') }
- NAME =
'Netvibes'
- VERSION =
'0.5'
- COPYRIGHT =
'Copyright (C) 2007 Florent Solt'
- DESC =
'Netvibes API'
- AUTHOR =
'Florent Solt'
- EMAIL =
'[email protected]'
- HOMEPAGE =
'http://gnetvibes.rubyforge.org'
- LICENSE =
'BSD'
- HOME =
File.join(ENV['HOME'], ".#{Netvibes::NAME.downcase}")
- CACHE =
File.join(HOME, "cache")
- DATA =
File.join(HOME, "data")
- CREDS =
File.join(DATA, 'creds')
- MODULES =
{ :postit => 'PostIt', :todolist => 'TodoList', :feed => 'RssReader' }
Instance Attribute Summary collapse
-
#modules ⇒ Object
readonly
Returns the value of attribute modules.
-
#tabs ⇒ Object
readonly
Returns the value of attribute tabs.
Instance Method Summary collapse
-
#add(tab, type, options) ⇒ Object
Add new module * tab : Id of the tad to add to * type : symbol of the module type.
-
#delete(id) ⇒ Object
Delete module * id : Id of the module.
-
#download(uri) ⇒ Object
Download a file if not present and add it to the local cache (in CACHE) * uri : Thing to download Return the path to the file.
-
#dump(key) ⇒ Object
Dump all session data to the filesystem to the DATA directory * key : String used to crypt the data.
-
#each_feed_in_tab(tab) ⇒ Object
Iter through modules in a tab (See each_modules_in_tab).
-
#each_modules_in_tab(tab, kind) ⇒ Object
Iter through modules (of certain kind) in a tab * tab : Id of the tab * kind : symbol of the module type.
-
#each_postit_in_tab(tab) ⇒ Object
Iter through modules in a tab (See each_modules_in_tab).
-
#each_todo_list_in_tab(tab) ⇒ Object
Iter through modules in a tab (See each_modules_in_tab).
-
#initialize ⇒ Netvibes
constructor
A new instance of Netvibes.
-
#load(key) ⇒ Object
Load session data from the filesystem in the DATA directory * key : String used to uncrypt the data.
-
#load_credentials ⇒ Object
Load credentials from a previous saved session, and then refresh the data.
-
#login(email, password) ⇒ Object
Login to netvibes website * email : email adress used to login * password : password :).
-
#refresh ⇒ Object
Refresh all the session data.
-
#save(id, options) ⇒ Object
Save module * id : the Id of the module * options : Hash ot data.
-
#save_credentials ⇒ Object
Save credentials (email adress, session id, user id) to the filesystem.
-
#tab_has?(tab, kind) ⇒ Boolean
Ask if a tab has some module * tab : Id of the tab * kind : symbol of the module type.
Constructor Details
#initialize ⇒ Netvibes
Returns a new instance of Netvibes.
51 52 53 54 55 56 57 58 |
# File 'lib/netvibes/api.rb', line 51 def initialize @active_user_id = nil @active_session_id = nil @data = nil @email = nil @tabs = nil @modules == nil end |
Instance Attribute Details
#modules ⇒ Object (readonly)
Returns the value of attribute modules.
49 50 51 |
# File 'lib/netvibes/api.rb', line 49 def modules @modules end |
#tabs ⇒ Object (readonly)
Returns the value of attribute tabs.
49 50 51 |
# File 'lib/netvibes/api.rb', line 49 def tabs @tabs end |
Instance Method Details
#add(tab, type, options) ⇒ Object
Add new module
-
tab : Id of the tad to add to
-
type : symbol of the module type
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/netvibes/api.rb', line 120 def add(tab, type, ) case type when :postit mod = { 'history' => nil, 'mode' => 'new', 'moduleRndld' => rand, 'name' => 'PostIt', 'share' => 0, 'status' => 1, 'tab' => tab.to_i, 'title' => [:title], 'data' => { 'color' => 'yellow', 'text' => [:text]}} else raise 'Not implemented' end Net::HTTP.start(URL[:data].host, URL[:data].port) do |http| headers = {'Cookie' => "activeUserID=#{@active_user_id}; activeSessionID=#{@active_session_id}"} post = Net::HTTP::Post.new(URL[:save].path, headers) post.set_form_data(module2post(mod)) http.request(post) end end |
#delete(id) ⇒ Object
Delete module
-
id : Id of the module
139 140 141 142 143 144 145 146 147 |
# File 'lib/netvibes/api.rb', line 139 def delete(id) id = id.to_i Net::HTTP.start(URL[:data].host, URL[:data].port) do |http| headers = {'Cookie' => "activeUserID=#{@active_user_id}; activeSessionID=#{@active_session_id}"} post = Net::HTTP::Post.new(URL[:save].path, headers) post.set_form_data({'id' => id, 'mode' => 'close'}) http.request(post) end end |
#download(uri) ⇒ Object
Download a file if not present and add it to the local cache (in CACHE)
-
uri : Thing to download
Return the path to the file
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/netvibes/api.rb', line 234 def download(uri) if uri =~ /^\// url = URL[:base] + uri else url = URI.parse(uri) end file = File.join(CACHE, Digest::SHA1.new(url.to_s).hexdigest) if not File.exists?(file) req = Net::HTTP::Get.new(url.path) res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) } File.open(file, 'w') do |fd| fd.puts res.body end end return file end |
#dump(key) ⇒ Object
Dump all session data to the filesystem to the DATA directory
-
key : String used to crypt the data
197 198 199 200 201 |
# File 'lib/netvibes/api.rb', line 197 def dump(key) File.open(File.join(DATA, @email), 'w') do |fd| fd.write crypt(@data.inspect, key) end end |
#each_feed_in_tab(tab) ⇒ Object
Iter through modules in a tab (See each_modules_in_tab)
181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/netvibes/api.rb', line 181 def each_feed_in_tab(tab) each_modules_in_tab(tab, :feed) do |feed| history = feed['data']['history'] if feed['data']['history'] feed['unread'] = history.split(':')[1..-1].select { |e| e == "0" }.size elsif feed['data']['nbTitles'] feed['unread'] = feed['data']['nbTitles'].to_i else feed['unread'] = -1 end yield feed end end |
#each_modules_in_tab(tab, kind) ⇒ Object
Iter through modules (of certain kind) in a tab
-
tab : Id of the tab
-
kind : symbol of the module type
163 164 165 166 167 168 |
# File 'lib/netvibes/api.rb', line 163 def each_modules_in_tab(tab, kind) # :yields: module raise "Unknown module : #{kind}" if not MODULES.keys.include?(kind) @data['modules'].select{|m| m['tab'].to_i == tab.to_i}.each do |m| yield m if kind.nil? or MODULES[kind] == m['name'] end end |
#each_postit_in_tab(tab) ⇒ Object
Iter through modules in a tab (See each_modules_in_tab)
171 172 173 |
# File 'lib/netvibes/api.rb', line 171 def each_postit_in_tab(tab) # :yields: module each_modules_in_tab(tab, :postit) { |postit| yield postit } end |
#each_todo_list_in_tab(tab) ⇒ Object
Iter through modules in a tab (See each_modules_in_tab)
176 177 178 |
# File 'lib/netvibes/api.rb', line 176 def each_todo_list_in_tab(tab) each_modules_in_tab(tab, :todolist) { |todolist| yield todolist } end |
#load(key) ⇒ Object
Load session data from the filesystem in the DATA directory
-
key : String used to uncrypt the data
205 206 207 208 209 210 211 212 213 |
# File 'lib/netvibes/api.rb', line 205 def load(key) file = File.join(DATA, @email) if File.exists?(file) @data = eval(crypt(File.read(file), key)) true else false end end |
#load_credentials ⇒ Object
Load credentials from a previous saved session, and then refresh the data.
225 226 227 228 229 |
# File 'lib/netvibes/api.rb', line 225 def load_credentials return false unless File.exists?(CREDS) @email, @active_user_id, @active_session_id = Marshal.load(File.read(CREDS)) refresh end |
#login(email, password) ⇒ Object
Login to netvibes website
-
email : email adress used to login
-
password : password :)
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/netvibes/api.rb', line 63 def login(email, password) email.strip! password.strip! res = Net::HTTP.post_form(URL[:login], {'email'=> email, 'password' => password}) if res.body.strip == 'success' @email = email = CGI::Cookie::parse(res['set-cookie']) @active_user_id = ['activeUserID'].value @active_session_id = ['activeSessionID'].value refresh true else false end end |
#refresh ⇒ Object
Refresh all the session data
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/netvibes/api.rb', line 80 def refresh res = Net::HTTP.start(URL[:data].host, URL[:data].port) do |http| headers = {'Cookie' => "activeUserID=#{@active_user_id}; activeSessionID=#{@active_session_id}"} http.request(Net::HTTP::Get.new(URL[:data].path, headers)) end return false if res.code != "200" @data = json2ruby(unicode(res.body)) @tabs = {} @modules = {} @data['tabs'].each { |t| @tabs[t['id'].to_i] = t } @data['modules'].each { |m| @modules[m['id'].to_i] = m } true end |
#save(id, options) ⇒ Object
Save module
-
id : the Id of the module
-
options : Hash ot data
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/netvibes/api.rb', line 98 def save(id, ) mod = @modules[id.to_i] case mod['name'] when /PostIt/i mod['title'] = [:title] mod['data']['text'] = [:text] mod['mode'] = 'module' else raise 'Not implemented' end Net::HTTP.start(URL[:data].host, URL[:data].port) do |http| headers = {'Cookie' => "activeUserID=#{@active_user_id}; activeSessionID=#{@active_session_id}"} post = Net::HTTP::Post.new(URL[:save].path, headers) post.set_form_data(module2post(mod)) http.request(post) end dump end |
#save_credentials ⇒ Object
Save credentials (email adress, session id, user id) to the filesystem. Used to implement a “remember me” feature.
217 218 219 220 221 222 |
# File 'lib/netvibes/api.rb', line 217 def save_credentials creds = [ @email, @active_user_id, @active_session_id ] File.open(CREDS, 'w') do |fd| fd.write(Marshal.dump(creds)) end end |
#tab_has?(tab, kind) ⇒ Boolean
Ask if a tab has some module
-
tab : Id of the tab
-
kind : symbol of the module type
152 153 154 155 156 157 158 |
# File 'lib/netvibes/api.rb', line 152 def tab_has?(tab, kind) raise "Unknown module : #{kind}" if not MODULES.keys.include?(kind) @data['modules'].select{|m| m['tab'].to_i == tab.to_i}.each do |m| return true if MODULES[kind] == m['name'] end false end |