Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add the 'Tag' class

Add a class to represent Google Reader generic tags/folders. It already
supports the write methods provided by API.
  • Loading branch information...
commit 66fbb6adc75298a440a247871c7e8fae597baf4c 1 parent 9b1ba5c
Gustavo Machado Campagnani Gama authored
View
26 lib/google-reader/item.rb
@@ -10,44 +10,36 @@ module Google; module Reader;
class Item < UnderscoreMash
attr_accessor :client
- module State
- Read = 'state/com.google/read'
- Starred = 'state/com.google/starred'
- Shared = 'state/com.google/broadcast'
- Broadcast = 'state/com.google/broadcast'
- KeptUnread = 'state/com.google/kept-unread'
- end
-
def mark_as_read
- add_tag("user/#{client.user.user_id}/#{State::Read}")
+ add_tag("user/#{client.user.user_id}/#{Tag::Read}")
end
def mark_as_unread
- remove_tag("user/#{client.user.user_id}/#{State::Read}")
+ remove_tag("user/#{client.user.user_id}/#{Tag::Read}")
end
def add_star
- add_tag("user/#{client.user.user_id}/#{State::Starred}")
+ add_tag("user/#{client.user.user_id}/#{Tag::Starred}")
end
def remove_star
- remove_tag("user/#{client.user.user_id}/#{State::Starred}")
+ remove_tag("user/#{client.user.user_id}/#{Tag::Starred}")
end
def share
- add_tag("user/#{client.user.user_id}/#{State::Shared}")
+ add_tag("user/#{client.user.user_id}/#{Tag::Shared}")
end
def unshare
- remove_tag("user/#{client.user.user_id}/#{State::Shared}")
+ remove_tag("user/#{client.user.user_id}/#{Tag::Shared}")
end
def keep_unread
- add_tag("user/#{client.user.user_id}/#{State::KeptUnread}")
+ add_tag("user/#{client.user.user_id}/#{Tag::KeptUnread}")
end
def keep_read
- remove_tag("user/#{client.user.user_id}/#{State::KeptUnread}")
+ remove_tag("user/#{client.user.user_id}/#{Tag::KeptUnread}")
end
alias :unkeep_unread :keep_read
@@ -66,7 +58,7 @@ def remove_tag(tag)
'i' => id, # entry
'r' => tag, # label/state to remove
'ac' => 'edit', # action (only known value: edit)
- 'T' => client.token # token
+ 'T' => client.token # the write-access token
}
client.access_token.post('/reader/api/0/edit-tag', params)
end
View
8 lib/google-reader/state.rb
@@ -1,8 +0,0 @@
-############################################################
-# Copyright (c) 2010, iGenesis Ltda. #
-# Author: Gustavo Machado C. Gama <gustavo.gama@gmail.com> #
-############################################################
-
-module Google; module Reader;
-
-end; end # module Google::Reader
View
53 lib/google-reader/tag.rb
@@ -0,0 +1,53 @@
+############################################################
+# Copyright (c) 2010, iGenesis Ltda. #
+# Author: Gustavo Machado C. Gama <gustavo.gama@gmail.com> #
+############################################################
+
+require 'google-reader/underscore-mash'
+
+module Google; module Reader;
+
+class Tag < UnderscoreMash
+ Read = self.new(:id => 'state/com.google/read')
+ Starred = self.new(:id => 'state/com.google/starred')
+ Shared = self.new(:id => 'state/com.google/broadcast')
+ Broadcast = self.new(:id => 'state/com.google/broadcast')
+ KeptUnread = self.new(:id => 'state/com.google/kept-unread')
+
+ def self.build_id(label, user)
+ "user/#{user.is_a?(Google::Reader::User) ? user.user_id : user}/label/#{label}"
+ end
+
+ def label
+ (self.id.nil? || (idx = self.id.rindex('/label/')).nil?) ? nil : self.id[(idx + 7) .. -1]
+ end
+
+ def share(val = true)
+ params = {
+ 's' => self.id, # the tag id
+ 'pub' => val, # true => public/share, false => not public/unshare
+ 'T' => client.token # the write-access token
+ }
+ client.access_token.post('/reader/api/0/tag/edit', params)
+ end
+
+ def unshare
+ share(false)
+ end
+
+ def disable
+ params = {
+ 's' => self.id, # the tag id
+ 'ac' => 'disable-tags', # action (only known value: disable-tags'
+ 'T' => client.token # the write-access token
+ }
+ client.access_token.post('/reader/api/0/disable-tag', params)
+ end
+ alias :delete :disable
+
+ def to_s
+ self.id.to_s
+ end
+end
+
+end; end # module Google::Reader
View
11 lib/google-reader/user.rb
@@ -122,6 +122,17 @@ def subscriptions!
subscriptions
end
+ # retrieve the list of tags/folders
+ def tags
+ @tags ||= begin
+ resp = @client.access_token.get('/reader/api/0/tag/list?output=json')
+ raise "unable to retrieve the list of tags for user #{user_id}" unless resp.code_type == Net::HTTPOK
+ JSON.parse(resp.body)['tags'].collect do |hash|
+ Google::Reader::Tag.new(hash.merge({:client => @client}))
+ end
+ end
+ end
+
# define 'list items' methods filtered by state; also, add an bang (!)
# version of each *_items method, that bypasses the cache
# and forces a new request

0 comments on commit 66fbb6a

Please sign in to comment.
Something went wrong with that request. Please try again.