Skip to content

Commit

Permalink
experimenting with 'send to'
Browse files Browse the repository at this point in the history
  • Loading branch information
alexch committed Nov 17, 2011
1 parent 9bd3149 commit fcd008d
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 26 deletions.
77 changes: 71 additions & 6 deletions app.rb
Expand Up @@ -32,7 +32,7 @@ class Sharebro < Sinatra::Application
end

enable :show_exceptions # until we get a better exception reporting mechanism

enable :method_override # POST _method=delete => DELETE
enable :sessions
# http://stackoverflow.com/questions/6115136/in-a-sinatra-app-on-heroku-session-is-not-shared-across-dynos
set :session_secret, ENV['SESSION_SECRET'] || 'tetrafluoride'
Expand All @@ -51,10 +51,7 @@ def initialize

attr_reader :here

def say msg
puts "" + Time.now + " - #{msg}"
end

include Say

def app_host
case ENV['RACK_ENV']
Expand All @@ -68,6 +65,11 @@ def app_host
get '/favicon.ico' do
send_file "#{here}/favicon.ico"
end

get '/sendto-icon.ico' do
send_file "#{here}/favicon.ico"
# send_file "#{here}/img/sharebro-logo.png"
end

# google oauth verification file
get '/google66d87a0b5d48cf21.html' do
Expand Down Expand Up @@ -270,10 +272,73 @@ def admin?
end

get "/admin" do
redirect '/' unless
redirect '/' unless admin?
app_page(Admin).to_html
end


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

get '/send_to' do
app_page(Raw.new(:params => data)).to_html
end

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

post '/send_to_your_mom' do
data = nil

prefs = google_api.preference_list["prefs"]
prefs = prefs_to_hash(prefs)
if prefs["custom-item-links"]
value = prefs["custom-item-links"]
d { value }
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"
}

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_your_mom' do
"not implemented"
end

end


Binary file added img/Superman_shield.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/sharebro-logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions lib/google_api.rb
Expand Up @@ -40,6 +40,7 @@ def fetch_json api_path
end

def post_json api_path, post_params = {}
post_params[:T] = edit_token
parts = api_path.split('?') # todo: use URI object
api_path = "#{parts[0]}?#{get_params(api_path).join('&')}"
response = @access_token.post api_path, post_params
Expand Down Expand Up @@ -97,8 +98,13 @@ def unread
fetch_json "/reader/api/0/unread-count?allcomments=true"
end

def preference_list
fetch_json "/reader/api/0/preference/list"
end

###

# Google makes you fetch an edit token for every post
def edit_token
response = access_token.get "/reader/api/0/token?ck=#{ck}&client=sharebro"
unless response.code.to_i == 200
Expand All @@ -119,8 +125,7 @@ def subscribe feed_url, title, user_label
s: "feed/#{feed_url}",
ac: "subscribe",
t: title,
a: "user/-/label/#{user_label}",
T: edit_token
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.
Expand Down
4 changes: 4 additions & 0 deletions notes/google-reader-api.txt
Expand Up @@ -117,6 +117,10 @@ These have an origin stream of user/-/state/com.google/post. They can
also be deleted (in the UI they have a delete action) but I don't
remember off-hand the URL for that.

## send to

http://lifehacker.com/5339214/supercharge-google-reader-with-send-to-links


## misc

Expand Down
21 changes: 20 additions & 1 deletion web/admin.rb
@@ -1,5 +1,24 @@
require 'sections'

class Admin < Widget
include Sections

def action_button name, action, method = 'POST'
form :action => action, :method => 'POST' do
input type: 'hidden', name: "_method", value: method.upcase
input type: 'submit', value: name
end
end

def content
h1 "Admin"

section "Experimental" do


action_button "Add 'Send to your mom'", "/send_to_your_mom"
action_button "Remove 'Send to your mom'", "/send_to_your_mom", 'delete'
end

end
end
end
5 changes: 4 additions & 1 deletion web/googled.rb
Expand Up @@ -68,8 +68,11 @@ def content
hr

item name: "preferences",
url: raw_url("/reader/api/0/preference/stream/list")
url: raw_url("/reader/api/0/preference/list")

item name: "stream preferences",
url: raw_url("/reader/api/0/preference/stream/list")

end
end
end
12 changes: 12 additions & 0 deletions web/raw.rb
@@ -0,0 +1,12 @@
require "sections"

class Raw < Widget
include Sections
needs :data

def content
section "Raw Data" do
pre JSON.pretty_generate(@data)
end
end
end
33 changes: 17 additions & 16 deletions web/sharebros.rb
Expand Up @@ -4,10 +4,13 @@ class Sharebros < Widget

external :style, <<-CSS
div.subscribe {
float: right;
border: 2px solid orange;
margin: .5em;
margin-top: -2em; /* hack to make it appear inside header */
max-width: 20em;
text-align: center;
}
div.subscribe input {
font-size: 24pt;
}
CSS

Expand Down Expand Up @@ -35,28 +38,26 @@ def content
p {
text "After clicking the 'Subscribe in Reader' button below, you will have a "
b { a "Shares", :href => you.shares_in_reader }
text " folder in Google Reader."
}

p {
text " folder in Google Reader. "
b "'People You Follow (1000+)' returns!"
}

div.subscribe do
form :method => :post,
:action => "/subscribe" do
input :type => "hidden",
:name => "user_ids",
:value => ([you] + @google_data.following).map{|bro| bro.user_id}.join(',')
input :type => "submit", :value => "Subscribe in Reader"
end
end


section "You" do
widget you
end

section "People You Follow" do
div.subscribe do
form :method => :post,
:action => "/subscribe" do
input :type => "hidden",
:name => "user_ids",
:value => ([you] + @google_data.following).map{|bro| bro.user_id}.join(',')
input :type => "submit", :value => "Subscribe in Reader"
end
end
div.clear
@google_data.following.each{|bro| widget bro}
end

Expand Down

0 comments on commit fcd008d

Please sign in to comment.