forked from andyw8/pocket-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo-server.rb
73 lines (62 loc) · 1.98 KB
/
demo-server.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
require "sinatra"
require "./lib/pocket-ruby.rb"
enable :sessions
CALLBACK_URL = "http://localhost:4567/oauth/callback"
Pocket.configure do |config|
#config.consumer_key = '10188-3565cd04d1464e6d0e64b67f'
config.consumer_key = '55809-cc464f4ad45ea0f4b10fc086'
end
get '/reset' do
puts "GET /reset"
session.clear
end
get "/" do
puts "GET /"
puts "session: #{session}"
if session[:access_token]
'
<a href="/add?url=http://getpocket.com">Add Pocket Homepage</a>
<a href="/retrieve">Retrieve single item</a>
'
else
'<a href="/oauth/connect">Connect with Pocket</a>'
end
end
get "/oauth/connect" do
puts "OAUTH CONNECT"
session[:code] = Pocket.get_code(:redirect_uri => CALLBACK_URL)
new_url = Pocket.authorize_url(:code => session[:code], :redirect_uri => CALLBACK_URL)
puts "new_url: #{new_url}"
puts "session: #{session}"
redirect new_url
end
get "/oauth/callback" do
puts "OAUTH CALLBACK"
puts "request.url: #{request.url}"
puts "request.body: #{request.body.read}"
result = Pocket.get_result(session[:code], :redirect_uri => CALLBACK_URL)
session[:access_token] = result['access_token']
puts result['access_token']
puts result['username']
# Alternative method to get the access token directly
#session[:access_token] = Pocket.get_access_token(session[:code])
puts session[:access_token]
puts "session: #{session}"
redirect "/"
end
get '/add' do
client = Pocket.client(:access_token => session[:access_token])
info = client.add :url => 'http://getpocket.com'
"<pre>#{info}</pre>"
end
get "/retrieve" do
client = Pocket.client(:access_token => session[:access_token])
#info = client.retrieve(:detailType => :complete, :count => 2)
info = client.retrieve(:detailType => :simple, :since => 1467072000)# => :complete, :count => 2)
# html = "<h1>#{user.username}'s recent photos</h1>"
# for media_item in client.user_recent_media
# html << "<img src='#{media_item.images.thumbnail.url}'>"
# end
# html
"<pre>#{info}</pre>"
end