Skip to content

Commit

Permalink
revamp a lot of stuff, preparing for new home page
Browse files Browse the repository at this point in the history
  • Loading branch information
alexch committed Nov 28, 2011
1 parent 8421751 commit f99866c
Show file tree
Hide file tree
Showing 11 changed files with 350 additions and 91 deletions.
74 changes: 22 additions & 52 deletions app.rb
Expand Up @@ -162,13 +162,25 @@ def google_data
end

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.")
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
Lipsumar.new(google_data).lipsumar_feeds
end


get '/about' do
app_page(About).to_html
end


get '/home2' do
params = {current_account: signed_in? ? current_account : nil}
app_page(Home2.new(params)).to_html
end

get '/sharebros' do
app_page(Sharebros.new(:google_data => google_data, :lipsumar_feeds => lipsumar_feeds)).to_html
end
Expand Down Expand Up @@ -287,7 +299,7 @@ def authorize back = nil
end

post "/subscribe" do
user_ids = params[:user_ids].split(',')
user_ids = params[:user_ids] && params[:user_ids].split(',')
Ant.request(:object, :class => "Subscribe", :account_id => current_account["_id"], :user_ids => user_ids)
app_page(Subscribed).to_html
end
Expand All @@ -305,14 +317,6 @@ def admin?
app_page(Admin).to_html
end

def prefs_to_hash prefs
h = {}
prefs.each do |pref|
h[pref['id']] = pref['value']
end
h
end

get '/send_to' do

item = params.pluck("title", "url", "source")
Expand All @@ -334,55 +338,21 @@ def prefs_to_hash prefs
else
return app_page(Raw.new(
:title => "unknown result #{result}",
:data => {:params => params}.merge(cmd.info))).to_html
:data => {:params => params}.merge(cmd.info)
)).to_html
end
end

# see http://www.google.com/reader/settings?display=edit-extras , click "Send To"

post '/add_send_to_sharebro' do

# todo: make response more pretty, probably a redirect too

data = nil

prefs = google_api.preference_list["prefs"]
prefs = prefs_to_hash(prefs)
if prefs["custom-item-links"]
value = prefs["custom-item-links"]
value_hash = JSON.parse(value)
post '/add_send_to_link' do
cmd = AddSendToLink.new(google_api, current_account)
result = cmd.perform
if result == :ok
message_page("added Send To Sharebro link", "added 'Send To' link -- go look for it in Google Reader")
else
value_hash = {
"builtinLinksEnabledState" => [],
"customLinks" => []
}
app_page(Raw.new(:title => "error adding Send To link", :data => {:params => params}.merge(cmd.info))).to_html
end

# customLinks == "Send To"
customLinks = value_hash['customLinks']
customLinks.delete_if{|entry|
entry['url'] =~ %r{^http://sharebro.org}
}
customLinks << {
"url" => "http://sharebro.org/send_to?sharebro_id=#{current_account["_id"]}&title=${title}&url=${url}&source=${source}",
"iconUrl" => "http://sharebro.org/favicon.ico",
"enabled" => true,
"name" => "Sharebro"
}

set_params = {"k" => "custom-item-links",
"v" => JSON.dump(value_hash),
}

response = google_api.post_json "/reader/api/0/preference/set", set_params

data = {
:customLinks => customLinks,
:set_params => set_params,
:response => response
}

app_page(Raw.new(:data => data)).to_html
end

delete '/send_to' do
Expand Down
68 changes: 68 additions & 0 deletions lib/add_send_to_link.rb
@@ -0,0 +1,68 @@
require 'nokogiri'
require 'net/http'

# sorta command-patterny
class AddSendToLink
def initialize google_api, current_account
@google_api, @current_account = google_api, current_account
@info = {}
end

attr_reader :google_api, :current_account
attr_reader :info # for debugging and error output

def prefs_to_hash prefs
h = {}
prefs.each do |pref|
h[pref['id']] = pref['value']
end
h
end


def perform
# todo: make response more pretty, probably a redirect too

data = nil

prefs = google_api.preference_list["prefs"]
prefs = prefs_to_hash(prefs)
if prefs["custom-item-links"]
value = prefs["custom-item-links"]
value_hash = JSON.parse(value)
else
value_hash = {
"builtinLinksEnabledState" => [],
"customLinks" => []
}
end

# customLinks == "Send To"
customLinks = value_hash['customLinks']
customLinks.delete_if{|entry|
entry['url'] =~ %r{^http://sharebro.org}
}

customLinks << {
"url" => "http://sharebro.org/send_to?sharebro_id=#{current_account["_id"]}&title=${title}&url=${url}&source=${source}",
"iconUrl" => "http://sharebro.org/favicon.ico",
"enabled" => true,
"name" => "Sharebro"
}
@info[:custom_links] = customLinks

set_params = {"k" => "custom-item-links",
"v" => JSON.dump(value_hash),
}
@info[:set_params] = set_params

response = google_api.post_json "/reader/api/0/preference/set", set_params
@info[:response] = response
if response == {:response => "OK"}
return :ok
else
return :error
end
end

end
16 changes: 8 additions & 8 deletions lib/google_api.rb
Expand Up @@ -165,14 +165,14 @@ def remove_label feed_url, user_label

def share feed_url, item_id
# http://blog.martindoms.com/2010/01/20/using-the-google-reader-api-part-3/#item-editing
#
post_json "/reader/api/0/edit-tag",
{
i: item_id,
a: "user/-/state/com.google/broadcast",
s: "feed/#{feed_url}",
ac: "edit",
}
say "sharing #{feed_url} #{item_id}"
params = {
i: item_id,
a: "user/-/state/com.google/broadcast",
s: "feed/#{feed_url}",
ac: "edit",
}
post_json "/reader/api/0/edit-tag", params
end

end
47 changes: 27 additions & 20 deletions lib/send_to.rb
Expand Up @@ -40,7 +40,6 @@ def perform
entry = entries.detect do |entry|
# todo: make an object out of this?
entry_id = entry.xpath('./xmlns:id').text
@info[:entry_id] = entry_id
item_links = entry.xpath('./xmlns:link')

# item_link = entry.xpath('./xmlns:link[rel=alternate]').first # not working because xpath sucks
Expand All @@ -59,31 +58,21 @@ def perform
next if item_link.nil?

href = item_link['href']

# finally, dereference proxy to get *real* original item href
if href =~ %r{http://feedproxy.google.com}
# http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html
response = Net::HTTP.get_response(URI(href))
if response.code.to_i != 301
say_error "expected redirect from #{href} but got #{response.code}"
else
href = response['Location']
if href
href = href.split('?').first # there's probably a cleaner way to do this (strip the query params)
end
end
end
href == item_url
d { href }
item_url == href or item_url == dereference(href)
end

if entry
@info[:entry_id] = entry.xpath('./xmlns:id').text
@info[:entry_xml] = entry.to_xml
entry_id = entry.xpath('./xmlns:id').text

# finally flag that sucker
x = google_api.share feed_url, entry_id
if x != {:response=>"OK"}
@info[:share_result] = x
say_error x
api_response = google_api.share feed_url, entry_id
d { api_response }
if api_response != {:response=>"OK"}
@info[:share_result] = api_response
say_error api_response
return :error
else
return :ok
Expand All @@ -93,4 +82,22 @@ def perform
return :not_found
end
end

def dereference href
# finally, dereference proxy to get *real* original item href
if href =~ %r{http://feedproxy.google.com}
# http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html
response = Net::HTTP.get_response(URI(href))
if response.code.to_i != 301
say_error "expected redirect from #{href} but got #{response.code}"
else
href = response['Location']
if href
href = href.split('?').first # there's probably a cleaner way to do this (strip the query params)
end
end
end
href
end

end
9 changes: 7 additions & 2 deletions lib/subscribe.rb
Expand Up @@ -4,6 +4,7 @@ class Subscribe # < Job
include Say

attr_reader :job

def initialize(job)
@job = job
end
Expand All @@ -24,7 +25,11 @@ def google_data
end

def bro_ids
job['user_ids']
if !job['user_ids'] or job['user_ids'].empty?
([google_data.you] + google_data.following).map{|bro| bro.user_id}
else
job['user_ids']
end
end

def bros
Expand Down Expand Up @@ -83,7 +88,7 @@ def subscribe_to_broadcast
end

def subscribe_to_lipsumar
lipsumar_feeds = Lipsumar.new(google_data).lipsumar_feeds
lipsumar_feeds = Lipsumar.new(google_data).lipsumar_feeds
bros.each do |bro|
response = if lipsumar_feeds[bro.user_id]
feed_name = "#{bro.full_name}'s Lipsumarium Shares"
Expand Down

0 comments on commit f99866c

Please sign in to comment.