Permalink
Browse files

Implemented the 'edit-tag' method

Initial support for the 'write' operations: add wrappers to the Item
class that encapsulate the 'edit-tag' operations provided by the Google
Reader API (add/remove star, mark/unmark unread, share/unshare, add
arbitrary tags, etc).
  • Loading branch information...
1 parent a67ad74 commit 9b1ba5cdb4cfdaa38be7375fe01d035b68f00521 Gustavo Machado Campagnani Gama committed Dec 21, 2010
Showing with 72 additions and 8 deletions.
  1. +1 −1 lib/google-reader/client.rb
  2. +7 −5 lib/google-reader/item-list.rb
  3. +62 −0 lib/google-reader/item.rb
  4. +2 −2 lib/google-reader/user.rb
@@ -40,7 +40,7 @@ def friends
resp = request_proxy.get('/reader/api/0/friend/list?output=json')
raise "unable to retrieve the list of friends" unless resp.code_type == Net::HTTPOK
JSON.parse(resp.body)['friends'].collect do |friend|
- ((friend['flags'] & (1 << User::IS_ME)) > 0) ? CurrentUser.new(request_proxy, friend) : User.new(request_proxy, friend)
+ ((friend['flags'] & (1 << User::IS_ME)) > 0) ? CurrentUser.new(self, friend) : User.new(self, friend)
end
end
end
@@ -11,15 +11,17 @@ module Google; module Reader;
class ItemList
attr_reader :items
- def initialize(req_proxy, json_str = nil)
- @request_proxy = req_proxy
+ def initialize(cli, json_str = nil)
+ @client = cli
if json_str
json = JSON.parse(json_str)
%w(author title updated direction continuation).each do |key|
instance_variable_set(('@'+key).to_sym, json[key])
end
@url = json['self'].first['href']
- @items = json['items'].collect{|i| Google::Reader::Item.new(i)}
+ @items = json['items'].collect do |item_mash|
+ Google::Reader::Item.new(item_mash).tap{|i| i.client = @client}
+ end
else
@items = Array.new
end
@@ -40,8 +42,8 @@ def next?
# fetch next batch of items, if available
def next(params = {})
next? or return nil
- resp = @request_proxy.get(merge_query_string(@url, params.merge({:continuation => @continuation})))
- self.class.new(@request_proxy, resp.body)
+ resp = @client.get(merge_query_string(@url, params.merge({:continuation => @continuation})))
+ self.class.new(@client, resp.body)
end
alias :more :next
View
@@ -8,6 +8,68 @@
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}")
+ end
+
+ def mark_as_unread
+ remove_tag("user/#{client.user.user_id}/#{State::Read}")
+ end
+
+ def add_star
+ add_tag("user/#{client.user.user_id}/#{State::Starred}")
+ end
+
+ def remove_star
+ remove_tag("user/#{client.user.user_id}/#{State::Starred}")
+ end
+
+ def share
+ add_tag("user/#{client.user.user_id}/#{State::Shared}")
+ end
+
+ def unshare
+ remove_tag("user/#{client.user.user_id}/#{State::Shared}")
+ end
+
+ def keep_unread
+ add_tag("user/#{client.user.user_id}/#{State::KeptUnread}")
+ end
+
+ def keep_read
+ remove_tag("user/#{client.user.user_id}/#{State::KeptUnread}")
+ end
+ alias :unkeep_unread :keep_read
+
+ def add_tag(tag)
+ params = {
+ 'i' => id, # entry
+ 'a' => tag, # label/state to add
+ 'ac' => 'edit', # action (only known value: edit)
+ 'T' => client.token # token
+ }
+ client.access_token.post('/reader/api/0/edit-tag', params)
+ end
+
+ def remove_tag(tag)
+ params = {
+ 'i' => id, # entry
+ 'r' => tag, # label/state to remove
+ 'ac' => 'edit', # action (only known value: edit)
+ 'T' => client.token # token
+ }
+ client.access_token.post('/reader/api/0/edit-tag', params)
+ end
end
end; end # module Google::Reader
@@ -43,8 +43,8 @@ class User < UnderscoreMash
const_set(flag, index)
end
- def initialize(req_proxy, hash = nil)
- @request_proxy = req_proxy
+ def initialize(cli, hash = nil)
+ @client = cli
super(hash, default, &blk)
friends = Array.new
end

0 comments on commit 9b1ba5c

Please sign in to comment.