Skip to content

Commit

Permalink
Implemented the 'edit-tag' method
Browse files Browse the repository at this point in the history
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
Gustavo Machado Campagnani Gama committed Dec 21, 2010
1 parent a67ad74 commit 9b1ba5c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/google-reader/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions lib/google-reader/item-list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
62 changes: 62 additions & 0 deletions lib/google-reader/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/google-reader/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9b1ba5c

Please sign in to comment.