-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconsumer.rb
147 lines (115 loc) · 3.16 KB
/
consumer.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
139
140
141
142
143
144
145
146
147
require 'rubygems'
require 'sinatra'
require 'oauth'
require 'oauth/consumer'
require 'dm-core'
require 'dm-validations'
require 'dm-serializer'
require File.dirname(__FILE__) + '/lib/oauth_test_wrapper'
DataMapper.setup(:default, "sqlite3:///#{Dir.pwd}/consumer.sqlite3")
class Oauth
include DataMapper::Resource
property :id, Integer, :serial => true # primary serial key
property :consumer_key, String, :nullable => false # cannot be null
property :consumer_secret, String, :nullable => false # cannot be null
property :request_token, String
property :request_secret, String
property :access_token, String
property :access_secret, String
end
DataMapper.auto_upgrade!
set :views, File.dirname(__FILE__) + '/views_client'
before do
@client ||= get_client
if !@client.nil?
if @client.access_token.nil?
@client = get_access
end
end
end
error do
exception = request.env['sinatra.error']
warn "%s: %s" % [exception.class, exception.message]
warn exception.backtrace.join("\n")
@error = "Oh my! Something went awry. (" + exception.message + ")"
erb :error
end
# index!
get '/' do
erb :index
end
get '/messages' do
if @client.nil?
erb :consumerkey
else
if @client.access_token.nil?
redirect '/'
else
@messages = @client.messages
erb :list
end
end
end
post '/messages' do
if @client.nil?
erb :consumerkey
else
if @client.access_token.nil?
redirect '/'
else
@message = @client.create_message(params[:message_name], params[:message_details])
redirect "/messages/#{@message.message_id}"
end
end
end
get '/messages/:message_id' do
if @client.nil?
erb :consumerkey
else
if @client.access_token.nil?
redirect '/'
else
@message = @client.show_message(params[:message_id])
erb :show
end
end
end
post '/addconsumerkey' do
oauth = Oauth.new
oauth.consumer_key = params[:consumer_key]
oauth.consumer_secret = params[:consumer_secret]
oauth.save
redirect '/messages'
end
private
def get_client
oauth = Oauth.first
if !oauth.nil?
clientDetails = {:consumer_key => oauth.consumer_key, :consumer_secret => oauth.consumer_secret}
if oauth.request_token and oauth.request_secret
clientDetails.merge!({:request_token => oauth.request_token, :request_token_secret => oauth.request_secret})
end
if oauth.access_token and oauth.access_secret
clientDetails.merge!({:access_token => oauth.access_token, :access_token_secret => oauth.access_secret})
end
client = OAuthTestWrapper::Client.new(clientDetails)
end
end
def get_access
oauth = Oauth.first
if !oauth.access_token or !oauth.access_secret
if !oauth.request_token or !oauth.request_secret
request_token = @client.get_request_token
oauth.request_token = request_token.token
oauth.request_secret = request_token.secret
oauth.save
redirect @client.authorize_url
else
access_token = @client.get_access_token
oauth.access_token = access_token.token
oauth.access_secret = access_token.secret
oauth.save
end
end
@client
end