Skip to content

Commit

Permalink
unsubscribe from your bro's Lipsumarium feed if he doesn't actually h…
Browse files Browse the repository at this point in the history
…ave one
  • Loading branch information
alexch committed Nov 25, 2011
1 parent aad1256 commit 174ddc4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 23 deletions.
17 changes: 3 additions & 14 deletions app.rb
Expand Up @@ -164,22 +164,11 @@ def google_data
def app_page main
AppPage.new(main: main, login_status: login_status, message: "We are currently experimenting with authorization. If things don't work right, please try again soon.")
end

def lipsumar_feeds
google_user_ids = google_data.bros.map(&:user_id)
require 'open-uri'
exists_url = "http://lipsumarium.com/greader/feedexists?_USER_IDS=#{google_user_ids.join(',')}"
x = open(exists_url).read
response = JSON.parse(x)
if response["status"] == "ok"
response["data"]
end
# note that it's OK to return nil here
rescue => e
# don't let a lipsumar error slow us down
say_error e
Lipsumar.new(google_data).lipsumar_feeds
end

get '/sharebros' do
app_page(Sharebros.new(:google_data => google_data, :lipsumar_feeds => lipsumar_feeds)).to_html
end
Expand Down
24 changes: 17 additions & 7 deletions lib/google_api.rb
Expand Up @@ -127,7 +127,15 @@ def edit_token
rescue => e
return error(response, e)
end



# s The feed identifier. This is the URL of the feed preceeded by feed/, for example feed/http://blog.martindoms.com/feed/ for this blog’s RSS feed.
# ac The action to take on this feed. Possible values are subscribe to subscribe, unsubscribe to unsubscribe and edit to edit the feed.
# t The title to give the feed, only relevant in subscribe and edit actions.
# r A label to remove from the feed. This is in the format user/[UserID]/label/[LabelName]. As usual UserID can be replaced with a single dash. For example, to remove the label “MyLabel” you’d use the string user/-/label/MyLabel
# a A label to add to the feed. See the notes for the r argument above.
# T Your token (see Part I if you’re unsure of what this is). This is an argument in every POST call.

def subscribe feed_url, title, user_label
post_json "/reader/api/0/subscription/edit",
{
Expand All @@ -136,12 +144,14 @@ def subscribe feed_url, title, user_label
t: title,
a: "user/-/label/#{user_label}"
}
# s The feed identifier. This is the URL of the feed preceeded by feed/, for example feed/http://blog.martindoms.com/feed/ for this blog’s RSS feed.
# ac The action to take on this feed. Possible values are subscribe to subscribe, unsubscribe to unsubscribe and edit to edit the feed.
# t The title to give the feed, only relevant in subscribe and edit actions.
# r A label to remove from the feed. This is in the format user/[UserID]/label/[LabelName]. As usual UserID can be replaced with a single dash. For example, to remove the label “MyLabel” you’d use the string user/-/label/MyLabel
# a A label to add to the feed. See the notes for the r argument above.
# T Your token (see Part I if you’re unsure of what this is). This is an argument in every POST call.
end

def unsubscribe feed_url
post_json "/reader/api/0/subscription/edit",
{
s: "feed/#{feed_url}",
ac: "unsubscribe",
}
end

def share feed_url, item_id
Expand Down
26 changes: 26 additions & 0 deletions lib/lipsumar.rb
@@ -0,0 +1,26 @@
require 'say'
require 'google_data'

class Lipsumar
include Say

def initialize(google_data)
@google_data = google_data
end

def lipsumar_feeds
google_user_ids = @google_data.bros.map(&:user_id)
require 'open-uri'
exists_url = "http://lipsumarium.com/greader/feedexists?_USER_IDS=#{google_user_ids.join(',')}"
x = open(exists_url).read
response = JSON.parse(x)
if response["status"] == "ok"
response["data"]
end
# note that it's OK to return nil here
rescue => e
# don't let a lipsumar error slow us down
say_error e
end

end
25 changes: 23 additions & 2 deletions lib/subscribe.rb
Expand Up @@ -28,15 +28,28 @@ def perform
@account = Accounts.get account_id
bro_ids = job['user_ids']

subscribed = []
bro_ids.each do |user_id|
bro = google_data.bro(user_id)
feed_name = "#{bro.full_name}'s Shared Items"
say("subscribing #{account_id} to #{user_id} as #{feed_name}")
# subscribe bro.lipsum, "#{bro.full_name}'s Shares"
response = subscribe bro.shared_items_atom_url, feed_name
say response.inspect if response[:response] != "OK"
end

lipsumar_feeds = Lipsumar.new(google_data).lipsumar_feeds

bro_ids.each do |user_id|
bro = google_data.bro(user_id)
feed_name = "#{bro.full_name}'s Lipsumarium Shares"
response = if lipsumar_feeds[user_id]
say("subscribing #{account_id} to #{bro.lipsum}")
subscribe bro.lipsum, "#{bro.full_name}'s Lipsumarium Shares", "Lipsumarium Shares"
else
say("unsubscribing #{account_id} from #{bro.lipsum}")
unsubscribe bro.lipsum
end
say response.inspect if response[:response] != "OK"
end
end

def subscribe feed_url, feed_name, folder_name = "Shares"
Expand All @@ -47,4 +60,12 @@ def subscribe feed_url, feed_name, folder_name = "Shares"
response
end

def unsubscribe feed_url
response = google_api.unsubscribe feed_url
if response == {:response => "OK"}
response[:feed_url] = feed_url
end
response
end

end

0 comments on commit 174ddc4

Please sign in to comment.