Permalink
Browse files

Updates galore. Added a wrapper for the api. Added a Sinatra consumer…

… using the wrapper.
  • Loading branch information...
1 parent 03530d6 commit 06e3640f5fc4c786cb3b557889d4e65d9e097e72 @eddanger committed Jan 28, 2009
Showing with 195 additions and 17 deletions.
  1. +2 −1 .gitignore
  2. +1 −1 README
  3. +30 −0 lib/attendease.rb
  4. +97 −0 lib/attendease/client.rb
  5. +16 −0 lib/attendease/events.rb
  6. +32 −0 lib/attendease/response.rb
  7. +1 −0 lib/oauth_provider
  8. +1 −0 lib/restfulx
  9. BIN test.sqlite3
  10. +7 −7 views/applications.erb
  11. +8 −8 views/list.erb
View
3 .gitignore
@@ -1,2 +1,3 @@
-lib/*
.DS_Store
+provider.sqlite3
+consumer.sqlite3
View
2 README
@@ -21,4 +21,4 @@ In /lib/oauth_provider/lib/provider.rb add:
attr_reader :backend
-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!
+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!
View
30 lib/attendease.rb
@@ -0,0 +1,30 @@
+require 'time'
+require 'net/https'
+require 'rubygems'
+require 'oauth/helper'
+require 'oauth/client/helper'
+require 'oauth/request_proxy/net_http'
+require 'hpricot'
+
+class AttendEase
+ API_SERVER = "http://localhost:3000"
+ AUTH_SERVER = "http://localhost:3000"
+ REQUEST_TOKEN_PATH = "/oauth/request_token"
+ ACCESS_TOKEN_PATH = "/oauth/access_token"
+ AUTHORIZATION_URL = "#{AUTH_SERVER}/oauth/authorize"
+ EVENTS_API_PATH = "/events"
+ FORMAT_XML = "xml"
+
+ class Error < RuntimeError #:nodoc:
+ end
+
+ class ArgumentError < Error #:nodoc:
+ end
+
+ class AttendEaseException < Error #:nodoc:
+ end
+end
+
+require File.dirname(__FILE__) + '/attendease/client'
+require File.dirname(__FILE__) + '/attendease/response'
+require File.dirname(__FILE__) + '/attendease/events'
View
97 lib/attendease/client.rb
@@ -0,0 +1,97 @@
+class AttendEase
+ class Client
+ # TODO add access_token=() and request_token=() methods that check whether the tokens are usable
+
+ attr_reader :access_token, :request_token, :consumer, :format
+
+ def initialize(options = {})
+ options = {
+ :debug => false,
+ :format => AttendEase::FORMAT_XML
+ }.merge(options)
+
+ # symbolize keys
+ options.map do |k,v|
+ options[k.to_sym] = v
+ end
+ raise AttendEase::ArgumentError, "OAuth Consumer Key and Secret required" if options[:consumer_key].nil? || options[:consumer_secret].nil?
+ @consumer = OAuth::Consumer.new(options[:consumer_key], options[:consumer_secret], :site => AttendEase::API_SERVER, :authorize_url => AttendEase::AUTHORIZATION_URL)
+ @debug = options[:debug]
+ @format = options[:format]
+ @app_id = options[:app_id]
+ if options[:access_token] && options[:access_token_secret]
+ @access_token = OAuth::AccessToken.new(@consumer, options[:access_token], options[:access_token_secret])
+ else
+ @access_token = nil
+ end
+ if options[:request_token] && options[:request_token_secret]
+ @request_token = OAuth::RequestToken.new(@consumer, options[:request_token], options[:request_token_secret])
+ else
+ @request_token = nil
+ end
+ end
+
+ # Obtain an <strong>new</strong> unauthorized OAuth Request token
+ def get_request_token(force_token_regeneration = false)
+ if force_token_regeneration || @request_token.nil?
+ @request_token = consumer.get_request_token
+ end
+ @request_token
+ end
+
+ # Return the Fire Eagle authorization URL for your mobile application. At this URL, the User will be prompted for their request_token.
+ def mobile_authorization_url
+ raise AttendEase::ArgumentError, ":app_id required" if @app_id.nil?
+ "#{AttendEase::MOBILE_AUTH_URL}#{@app_id}"
+ end
+
+ # 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.
+ def authorization_url
+ raise AttendEase::ArgumentError, "call #get_request_token first" if @request_token.nil?
+ request_token.authorize_url
+ end
+
+ #Exchange an authorized OAuth Request token for an access token. For use by desktop-based and mobile applications.
+ def convert_to_access_token
+ raise AttendEase::ArgumentError, "call #get_request_token and have user authorize the token first" if @request_token.nil?
+ @access_token = request_token.get_access_token
+ end
+
+
+
+ def events(params)
+ raise AttendEase::ArgumentError, "OAuth Access Token Required" unless @access_token
+ response = get(AttendEase::EVENTS_API_PATH + ".#{format}", :params => params)
+ AttendEase::Response.new(response.body)
+ end
+
+
+ protected
+
+ def get(url, options = {}) #:nodoc:
+ request(:get, url, options)
+ end
+
+ def post(url, options = {}) #:nodoc:
+ request(:post, url, options)
+ end
+
+ def request(method, url, options) #:nodoc:
+ response = case method
+ when :post
+ access_token.request(:post, url, options[:params])
+ when :get
+ qs = options[:params].collect { |k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&") if options[:params]
+ access_token.request(:get, "#{url}?#{qs}")
+ else
+ raise ArgumentError, "method #{method} not supported"
+ end
+
+ case response.code
+ when '500'; then raise AttendEase::AttendEaseException, "Internal Server Error"
+ when '400'; then raise AttendEase::AttendEaseException, "Method Not Implemented Yet"
+ else response
+ end
+ end
+ end
+end
View
16 lib/attendease/events.rb
@@ -0,0 +1,16 @@
+#Describes a location
+class AttendEase
+ class Events
+
+ #Initialize a Location from an XML response
+ def initialize(doc)
+ doc = Hpricot(doc) unless doc.is_a?(Hpricot::Doc || Hpricot::Elem)
+ @doc = doc
+ end
+
+ def title
+ @title ||= @doc.at("/event/title").innerText rescue nil
+ end
+
+ end
+end
View
32 lib/attendease/response.rb
@@ -0,0 +1,32 @@
+class AttendEase
+ class Response
+
+ #Parses the XML response from AttendEase
+ def initialize(doc)
+ doc = Hpricot(doc) unless doc.is_a?(Hpricot::Doc || Hpricot::Elem)
+ @doc = doc
+ #raise AttendEase::AttendEaseException, @doc.at("/rsp/err").attributes["msg"] if !success?
+ end
+
+ #does the response indicate success?
+ def success?
+ #@doc.at("/rsp").attributes["stat"] == "ok" rescue false
+ end
+
+ #An array of of User-specific tokens and their Location at all levels the Client can see and larger.
+ def users
+ @users ||= @doc.search("/rsp//user").map do |user|
+ AttendEase::User.new(user.to_s)
+ end
+ end
+
+ #A Location array.
+ def events
+ #@locations ||= @doc.search("/rsp/locations/location").map do |location|
+ # AttendEase::Location.new(location.to_s)
+ #end
+ @doc
+ end
+
+ end
+end
1 lib/oauth_provider
@@ -0,0 +1 @@
+Subproject commit 1aa2f6dab4370c982da6816c46700b0ff89f208e
1 lib/restfulx
@@ -0,0 +1 @@
+Subproject commit 84da42c44308d0b210c6f7e3be6a168a373c0149
View
BIN test.sqlite3
Binary file not shown.
View
14 views/applications.erb
@@ -6,18 +6,18 @@
</div>
<% end %>
-<% if @shared_key and @secret_key %>
+<% if @consumer_key and @consumer_secret %>
<div style="background-color: #efe; padding: 20px; margin-bottom: 10px;">
<h2>Application created!</h2>
<h3>Save this information!</h3>
- <b>Shared Key:</b>
- <%= @shared_key %>
+ <b>Consumer Key:</b>
+ <%= @consumer_key %>
<br/>
- <b>Secret Key:</b>
- <%= @secret_key %>
+ <b>Consumer Secret:</b>
+ <%= @consumer_secret %>
</div>
<% end %>
@@ -29,9 +29,9 @@
<div style="background-color: #eef; padding: 20px; margin-bottom: 10px;">
<b><%= consumer.callback %></b>
<p>
- <b>Shared Key:</b> <%= consumer.token.shared_key %>
+ <b>Consumer Key:</b> <%= consumer.token.shared_key %>
<br/>
- <b>Secret Key:</b> <%= consumer.token.secret_key %>
+ <b>Consumer Secret:</b> <%= consumer.token.secret_key %>
</p>
</div>
<% end %>
View
16 views/list.erb
@@ -1,13 +1,13 @@
<h2>My Messages</h2>
-<% if !@messages.empty? %>
- <ul>
- <% @messages.each do |message| %>
- <li><a href="/messages/<%= message.id %>"><%= message.name %></a></li>
- <% end %>
- </ul>
-<% else %>
- You don't have any messages.
+<% if @messages.empty? %>
+ You don't have any messages.
+ <% else %>
+ <ul>
+ <% @messages.each do |message| %>
+ <li><a href="/messages/<%= message.id %>"><%= message.name %></a></li>
+ <% end %>
+ </ul>
<% end %>
<h3>Create New Message</h3>

0 comments on commit 06e3640

Please sign in to comment.