Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Wrapped the "/stream/items/ids" "/stream/items/contents" methods

Add {broadcast,unread,shared,starred,...}_item_ids methods to the User
class. These methods behave much like the *_items methods, except that
only the item ids (or item refs, as the API refers to them) are
returned.

Also added the "items_from_ids" method, which retrieves the full Item
contents given a list of item ids/refs. Together, these two methods
provide a nice mechanism to synchronize a user's item list.
  • Loading branch information...
commit 4bd91d054a2219b1194497a3152fc8a001106905 1 parent ed95120
Gustavo Machado Campagnani Gama authored
View
1  lib/google-reader/item-list.rb
@@ -80,6 +80,7 @@ def self.convert_key_aliases(query_hash)
new_key = case key.to_s
when 'n', 'count', 'n_items': 'n'
when 'r', 'order': 'r'
+ when 's', 'tag': 's'
when 'ot', 'start_time': 'ot'
when 'ck', 'timestamp': 'ck'
when 'xt', 'exclude': 'xt'
View
17 lib/google-reader/item-ref.rb
@@ -0,0 +1,17 @@
+############################################################
+# Copyright (c) 2010, iGenesis Ltda. #
+# Author: Gustavo Machado C. Gama <gustavo.gama@gmail.com> #
+############################################################
+
+require 'google-reader/underscore-mash'
+require 'google-reader/item'
+
+module Google; module Reader;
+
+class ItemRef < UnderscoreMash
+ def item_id
+ Item.str_id(self.id)
+ end
+end
+
+end; end # module Google::Reader
View
17 lib/google-reader/item.rb
@@ -10,6 +10,23 @@ module Google; module Reader;
class Item < UnderscoreMash
attr_accessor :client
+ STR_ID_PREFIX = 'tag:google.com,2005:reader/item/'
+
+ def self.num_id(str)
+ str.start_with?(STR_ID_PREFIX) or raise "invalid string id: #{str}"
+ num = "0x#{str[-16..-1]}".hex
+ num[63] == 1 and num = (num - 0xFFFFFFFFFFFFFFFF - 1)
+ num
+ end
+
+ def self.str_id(num)
+ STR_ID_PREFIX + ('%016x' % num)[-16..-1]
+ end
+
+ def item_ref_id
+ self.class.num_id(self.id)
+ end
+
def mark_as_read
add_tag("user/#{client.user.user_id}/#{Tag::Read}")
end
View
81 lib/google-reader/user.rb
@@ -6,6 +6,7 @@
require 'google-reader/underscore-mash'
require 'google-reader/subscription'
require 'google-reader/item-list'
+require 'google-reader/item-ref'
module Google; module Reader;
@@ -87,6 +88,14 @@ def #{prefix}_items!(params = {})
EOS
end
+ def self.define_item_ids_method(prefix)
+ class_eval <<EOS
+ def #{prefix}_item_ids(params = {})
+ filtered_item_ids('#{prefix.gsub(/_/, '-')}', params)
+ end
+EOS
+ end
+
# retrieve the a list of items from a given user, using a given filter
def filtered_items_list(filter, params = {})
resp = @client.access_token.get(ItemList.merge_query_string("/reader/api/0/stream/contents/user/#{user_id}/state/com.google/#{filter}?output=json", params))
@@ -94,11 +103,40 @@ def filtered_items_list(filter, params = {})
Google::Reader::ItemList.new(@client, resp.body)
end
+ # retrieve the a list of items ids/refs from a given user, using a given filter
+ def filtered_item_ids(filter, params = {})
+ resp = @client.access_token.get(ItemList.merge_query_string("/reader/api/0/stream/items/ids?output=json&s=user/#{user_id}/state/com.google/#{filter}&n=20", params))
+ raise "unable to retrieve the list of #{filter} item ids for user \"#{user_id}\": #{resp.inspect}" unless resp.code_type == Net::HTTPOK
+ JSON.parse(resp.body)['itemRefs'].collect do |hash|
+ Google::Reader::ItemRef.new(hash)
+ end
+ end
+
public
+ # retrieve the contents of each item present in the array parameter
+ def items_from_ids(ids_array)
+ id_strs = ids_array.collect do |id|
+ id = if id.is_a? Google::Reader::ItemRef
+ id.id
+ elsif id.is_a?(Fixnum) || id.is_a?(Bignum)
+ id
+ else
+ raise "invalid item id type: #{id} (class: #{id.class})"
+ end
+ "i=#{id}"
+ end
+
+ resp = @client.access_token.post("/reader/api/0/stream/items/contents", id_strs.join("&"))
+ raise "unable to retrieve the item contents for user \"#{user_id}\": #{resp.inspect}" unless resp.code_type == Net::HTTPOK
+ Google::Reader::ItemList.new(@client, resp.body)
+ end
+
define_item_list_method('broadcast')
- alias :shared_items :broadcast_items
- alias :shared_items! :broadcast_items!
+ define_item_ids_method('broadcast')
+ alias :shared_items :broadcast_items
+ alias :shared_items! :broadcast_items!
+ alias :shared_item_ids :broadcast_item_ids
end
class CurrentUser < User
@@ -154,24 +192,32 @@ def tags!
tracking_item_link_used
tracking_kept_unread).each do |prefix|
define_item_list_method(prefix)
+ define_item_ids_method(prefix)
end
# create a few method aliases to enable friendlier (or more common names
# (for instance, shared vs broadcast)
- alias :reading_list :reading_list_items
- alias :reading_list! :reading_list_items!
- alias :all_items :reading_list_items
- alias :all_items! :reading_list_items!
- alias :shared_friends_items :broadcast_friends_items
- alias :shared_friends_items! :broadcast_friends_items!
- alias :followed_body_items :tracking_body_link_used_items
- alias :followed_body_items! :tracking_body_link_used_items!
- alias :emailed_items :tracking_emailed_items
- alias :emailed_items! :tracking_emailed_items!
- alias :followed_items :tracking_item_link_used_items
- alias :followed_items! :tracking_item_link_used_items!
- alias :kept_unread_ever_items :tracking_kept_unread_items
- alias :kept_unread_ever_items! :tracking_kept_unread_items!
+ alias :reading_list :reading_list_items
+ alias :reading_list! :reading_list_items!
+ alias :reading_list_ids :reading_list_item_ids
+ alias :all_items :reading_list_items
+ alias :all_items! :reading_list_items!
+ alias :all_item_ids :reading_list_item_ids
+ alias :shared_friends_items :broadcast_friends_items
+ alias :shared_friends_items! :broadcast_friends_items!
+ alias :shared_friends_item_ids :broadcast_friends_item_ids
+ alias :followed_body_items :tracking_body_link_used_items
+ alias :followed_body_items! :tracking_body_link_used_items!
+ alias :followed_body_item_ids :tracking_body_link_used_item_ids
+ alias :emailed_items :tracking_emailed_items
+ alias :emailed_items! :tracking_emailed_items!
+ alias :emailed_item_ids :tracking_emailed_item_ids
+ alias :followed_items :tracking_item_link_used_items
+ alias :followed_items! :tracking_item_link_used_items!
+ alias :followed_item_ids :tracking_item_link_used_item_ids
+ alias :kept_unread_ever_items :tracking_kept_unread_items
+ alias :kept_unread_ever_items! :tracking_kept_unread_items!
+ alias :kept_unread_ever_item_ids :tracking_kept_unread_item_ids
# add the special the special case 'unread_items', which doesn't not
# have a dedicated API method; we need to fetch the entire reading list
@@ -183,6 +229,9 @@ def unread_items!(params = {})
@unread_items = nil
unread_items(params)
end
+ def unread_item_ids(params = {})
+ @unread_items ||= filtered_item_ids('reading-list', params.merge(:exclude => 'user/-/state/com.google/read'))
+ end
end
end; end # module Google::Reader
Please sign in to comment.
Something went wrong with that request. Please try again.