Skip to content

Commit 06e3640

Browse files
author
Michael Wood
committed
Updates galore. Added a wrapper for the api. Added a Sinatra consumer using the wrapper.
1 parent 03530d6 commit 06e3640

11 files changed

+195
-17
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
lib/*
21
.DS_Store
2+
provider.sqlite3
3+
consumer.sqlite3

README

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ In /lib/oauth_provider/lib/provider.rb add:
2121

2222
attr_reader :backend
2323

24-
Thanks to pelle, halorgium (for auth_provider), and singpolyma (for the simple Sinatra example)! And to the Vancouver "Ruby in the Rain" event for opening my eyes to Sinatra. You guys rock!
24+
Thanks to pelle, halorgium (for auth_provider), and singpolyma (for the simple Sinatra example)! And to the Vancouver "Ruby in the Rain" event for opening my eyes to Sinatra. You guys rock!

lib/attendease.rb

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'time'
2+
require 'net/https'
3+
require 'rubygems'
4+
require 'oauth/helper'
5+
require 'oauth/client/helper'
6+
require 'oauth/request_proxy/net_http'
7+
require 'hpricot'
8+
9+
class AttendEase
10+
API_SERVER = "http://localhost:3000"
11+
AUTH_SERVER = "http://localhost:3000"
12+
REQUEST_TOKEN_PATH = "/oauth/request_token"
13+
ACCESS_TOKEN_PATH = "/oauth/access_token"
14+
AUTHORIZATION_URL = "#{AUTH_SERVER}/oauth/authorize"
15+
EVENTS_API_PATH = "/events"
16+
FORMAT_XML = "xml"
17+
18+
class Error < RuntimeError #:nodoc:
19+
end
20+
21+
class ArgumentError < Error #:nodoc:
22+
end
23+
24+
class AttendEaseException < Error #:nodoc:
25+
end
26+
end
27+
28+
require File.dirname(__FILE__) + '/attendease/client'
29+
require File.dirname(__FILE__) + '/attendease/response'
30+
require File.dirname(__FILE__) + '/attendease/events'

lib/attendease/client.rb

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
class AttendEase
2+
class Client
3+
# TODO add access_token=() and request_token=() methods that check whether the tokens are usable
4+
5+
attr_reader :access_token, :request_token, :consumer, :format
6+
7+
def initialize(options = {})
8+
options = {
9+
:debug => false,
10+
:format => AttendEase::FORMAT_XML
11+
}.merge(options)
12+
13+
# symbolize keys
14+
options.map do |k,v|
15+
options[k.to_sym] = v
16+
end
17+
raise AttendEase::ArgumentError, "OAuth Consumer Key and Secret required" if options[:consumer_key].nil? || options[:consumer_secret].nil?
18+
@consumer = OAuth::Consumer.new(options[:consumer_key], options[:consumer_secret], :site => AttendEase::API_SERVER, :authorize_url => AttendEase::AUTHORIZATION_URL)
19+
@debug = options[:debug]
20+
@format = options[:format]
21+
@app_id = options[:app_id]
22+
if options[:access_token] && options[:access_token_secret]
23+
@access_token = OAuth::AccessToken.new(@consumer, options[:access_token], options[:access_token_secret])
24+
else
25+
@access_token = nil
26+
end
27+
if options[:request_token] && options[:request_token_secret]
28+
@request_token = OAuth::RequestToken.new(@consumer, options[:request_token], options[:request_token_secret])
29+
else
30+
@request_token = nil
31+
end
32+
end
33+
34+
# Obtain an <strong>new</strong> unauthorized OAuth Request token
35+
def get_request_token(force_token_regeneration = false)
36+
if force_token_regeneration || @request_token.nil?
37+
@request_token = consumer.get_request_token
38+
end
39+
@request_token
40+
end
41+
42+
# Return the Fire Eagle authorization URL for your mobile application. At this URL, the User will be prompted for their request_token.
43+
def mobile_authorization_url
44+
raise AttendEase::ArgumentError, ":app_id required" if @app_id.nil?
45+
"#{AttendEase::MOBILE_AUTH_URL}#{@app_id}"
46+
end
47+
48+
# The URL the user must access to authorize this token. get_request_token must be called first. For use by web-based and desktop-based applications.
49+
def authorization_url
50+
raise AttendEase::ArgumentError, "call #get_request_token first" if @request_token.nil?
51+
request_token.authorize_url
52+
end
53+
54+
#Exchange an authorized OAuth Request token for an access token. For use by desktop-based and mobile applications.
55+
def convert_to_access_token
56+
raise AttendEase::ArgumentError, "call #get_request_token and have user authorize the token first" if @request_token.nil?
57+
@access_token = request_token.get_access_token
58+
end
59+
60+
61+
62+
def events(params)
63+
raise AttendEase::ArgumentError, "OAuth Access Token Required" unless @access_token
64+
response = get(AttendEase::EVENTS_API_PATH + ".#{format}", :params => params)
65+
AttendEase::Response.new(response.body)
66+
end
67+
68+
69+
protected
70+
71+
def get(url, options = {}) #:nodoc:
72+
request(:get, url, options)
73+
end
74+
75+
def post(url, options = {}) #:nodoc:
76+
request(:post, url, options)
77+
end
78+
79+
def request(method, url, options) #:nodoc:
80+
response = case method
81+
when :post
82+
access_token.request(:post, url, options[:params])
83+
when :get
84+
qs = options[:params].collect { |k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&") if options[:params]
85+
access_token.request(:get, "#{url}?#{qs}")
86+
else
87+
raise ArgumentError, "method #{method} not supported"
88+
end
89+
90+
case response.code
91+
when '500'; then raise AttendEase::AttendEaseException, "Internal Server Error"
92+
when '400'; then raise AttendEase::AttendEaseException, "Method Not Implemented Yet"
93+
else response
94+
end
95+
end
96+
end
97+
end

lib/attendease/events.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#Describes a location
2+
class AttendEase
3+
class Events
4+
5+
#Initialize a Location from an XML response
6+
def initialize(doc)
7+
doc = Hpricot(doc) unless doc.is_a?(Hpricot::Doc || Hpricot::Elem)
8+
@doc = doc
9+
end
10+
11+
def title
12+
@title ||= @doc.at("/event/title").innerText rescue nil
13+
end
14+
15+
end
16+
end

lib/attendease/response.rb

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class AttendEase
2+
class Response
3+
4+
#Parses the XML response from AttendEase
5+
def initialize(doc)
6+
doc = Hpricot(doc) unless doc.is_a?(Hpricot::Doc || Hpricot::Elem)
7+
@doc = doc
8+
#raise AttendEase::AttendEaseException, @doc.at("/rsp/err").attributes["msg"] if !success?
9+
end
10+
11+
#does the response indicate success?
12+
def success?
13+
#@doc.at("/rsp").attributes["stat"] == "ok" rescue false
14+
end
15+
16+
#An array of of User-specific tokens and their Location at all levels the Client can see and larger.
17+
def users
18+
@users ||= @doc.search("/rsp//user").map do |user|
19+
AttendEase::User.new(user.to_s)
20+
end
21+
end
22+
23+
#A Location array.
24+
def events
25+
#@locations ||= @doc.search("/rsp/locations/location").map do |location|
26+
# AttendEase::Location.new(location.to_s)
27+
#end
28+
@doc
29+
end
30+
31+
end
32+
end

lib/oauth_provider

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 1aa2f6dab4370c982da6816c46700b0ff89f208e

lib/restfulx

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 84da42c44308d0b210c6f7e3be6a168a373c0149

test.sqlite3

7 KB
Binary file not shown.

views/applications.erb

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
</div>
77
<% end %>
88

9-
<% if @shared_key and @secret_key %>
9+
<% if @consumer_key and @consumer_secret %>
1010
<div style="background-color: #efe; padding: 20px; margin-bottom: 10px;">
1111
<h2>Application created!</h2>
1212

1313
<h3>Save this information!</h3>
1414

15-
<b>Shared Key:</b>
16-
<%= @shared_key %>
15+
<b>Consumer Key:</b>
16+
<%= @consumer_key %>
1717
<br/>
1818

19-
<b>Secret Key:</b>
20-
<%= @secret_key %>
19+
<b>Consumer Secret:</b>
20+
<%= @consumer_secret %>
2121
</div>
2222
<% end %>
2323

@@ -29,9 +29,9 @@
2929
<div style="background-color: #eef; padding: 20px; margin-bottom: 10px;">
3030
<b><%= consumer.callback %></b>
3131
<p>
32-
<b>Shared Key:</b> <%= consumer.token.shared_key %>
32+
<b>Consumer Key:</b> <%= consumer.token.shared_key %>
3333
<br/>
34-
<b>Secret Key:</b> <%= consumer.token.secret_key %>
34+
<b>Consumer Secret:</b> <%= consumer.token.secret_key %>
3535
</p>
3636
</div>
3737
<% end %>

views/list.erb

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<h2>My Messages</h2>
22

3-
<% if !@messages.empty? %>
4-
<ul>
5-
<% @messages.each do |message| %>
6-
<li><a href="/messages/<%= message.id %>"><%= message.name %></a></li>
7-
<% end %>
8-
</ul>
9-
<% else %>
10-
You don't have any messages.
3+
<% if @messages.empty? %>
4+
You don't have any messages.
5+
<% else %>
6+
<ul>
7+
<% @messages.each do |message| %>
8+
<li><a href="/messages/<%= message.id %>"><%= message.name %></a></li>
9+
<% end %>
10+
</ul>
1111
<% end %>
1212

1313
<h3>Create New Message</h3>

0 commit comments

Comments
 (0)