-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
138 lines (111 loc) · 3.55 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# This code is based on pocket-ruby
# https://github.com/turadg/pocket-ruby
require 'sinatra'
require 'yaml'
require 'pocket.rb'
require './itemLoader.rb'
require 'pocketConsole'
enable :sessions
CALLBACK_URL = 'http://localhost:4567/oauth/callback'
Pocket.configure do |config|
config.consumer_key = 'YOUR-CONSUMER_KEY'
end
get '/tags' do
client = Pocket.client(:access_token => session[:access_token])
info = client.retrieve(:detailType => :complete, :count => 80, :state => :all)
File.open('./.cache-files/info.yml', 'w') {|f| f.write(YAML.dump(info)) }
tags(info)
end
get '/tags-cache' do
info = YAML.load(File.read('./.cache-files/info.yml'))
tags(info)
end
get '/tags-cache-specific-terms' do
client = Pocket.client(:access_token => session[:access_token])
info = client.retrieve(:detailType => :complete, :state => :all)
File.open('./.cache-files/info.yml', 'w') {|f| f.write(YAML.dump(info)) }
tags_specific_terms(info)
end
get '/tags-cache-specific-terms-cache' do
info = YAML.load(File.read('./.cache-files/info.yml'))
tags_specific_terms(info)
end
def tags(info)
pocketConsole = initializePocketConsole(info)
pocketConsole.printStats
'Stats are in the console'
end
def tags_specific_terms(info)
pocketConsole = initializePocketConsole(info)
terms = ['dev', 'job', 'work', 'ruby', 'ux', 'techonomics']
pocketConsole.printSpecificTermsStats(terms)
'Stats are in the console'
end
def initializePocketConsole(info)
list = info['list']
itemLoader = ItemLoader.new(list)
taggedItems = itemLoader.getTaggedItems
untaggedItems = itemLoader.getUntaggedItems
# the two following variables contain integer values
readTaggedItems = itemLoader.getReadTaggedItems
readUntaggedItems = itemLoader.getReadUntaggedItems
pocketConsole = PocketConsole.new(
taggedItems, untaggedItems,
readTaggedItems, readUntaggedItems
)
end
# All the code below belongs to pocket-ruby
# https://github.com/turadg/pocket-ruby
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 => 20)
# 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