/
app.rb
74 lines (59 loc) · 1.51 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# By Henrik Nyh <henrik@nyh.se> 2010-12-18 under the MIT license.
require "open-uri"
require "rubygems"
require "sinatra"
require "sinatra/sequel"
require "haml"
require "sass"
require "json"
set :database, (ENV['DATABASE_URL'] || 'sqlite:///tmp/delpin3.db')
migration "create mappings" do
database.create_table :mappings do
primary_key :id
text :delicious
text :pinboard
timestamp :created_at, :null => false
timestamp :updated_at, :null => false
index :delicious, :unique => true
end
end
class Mapping < Sequel::Model
end
set :haml, :format => :html5, :attr_wrapper => %{"}
set :views, lambda { root }
get '/' do
if @name = params[:name]
@friends = friends_of(@name)
end
render_list
end
post '/' do
delicious = params[:delicious].to_s.downcase
pinboard = params[:pinboard].to_s.downcase
if delicious.empty? || pinboard.empty?
return "Please provide both names."
end
old_mapping = Mapping[:delicious => delicious]
if old_mapping
old_mapping.update(:pinboard => pinboard, :updated_at => Time.now)
else
Mapping.create(
:delicious => delicious,
:pinboard => pinboard,
:created_at => Time.now, :updated_at => Time.now
)
end
render_list
end
def render_list
@mappings = Mapping.limit(10)
haml :index
end
def friends_of(name)
data = open("http://feeds.delicious.com/v2/json/networkmembers/#{name}").read
usernames = JSON.parse(data).map { |values| values["user"] }
usernames
end
def h(text)
Rack::Utils.escape(text)
end