Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
init
- Loading branch information
Pat Patterson
committed
Feb 9, 2012
0 parents
commit 996423c
Showing
12 changed files
with
333 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
vars.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
source :gemcutter | ||
gem 'sinatra' | ||
gem 'oauth2' | ||
gem 'json' | ||
gem 'dalli' | ||
gem 'rack' | ||
gem 'logger' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
GEM | ||
remote: http://rubygems.org/ | ||
specs: | ||
addressable (2.2.6) | ||
dalli (1.1.4) | ||
faraday (0.7.6) | ||
addressable (~> 2.2) | ||
multipart-post (~> 1.1) | ||
rack (~> 1.1) | ||
json (1.6.5) | ||
logger (1.2.8) | ||
multi_json (1.0.4) | ||
multipart-post (1.1.4) | ||
oauth2 (0.5.2) | ||
faraday (~> 0.7) | ||
multi_json (~> 1.0) | ||
rack (1.4.1) | ||
rack-protection (1.2.0) | ||
rack | ||
sinatra (1.3.2) | ||
rack (~> 1.3, >= 1.3.6) | ||
rack-protection (~> 1.2) | ||
tilt (~> 1.3, >= 1.3.3) | ||
tilt (1.3.3) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
dalli | ||
json | ||
logger | ||
oauth2 | ||
rack | ||
sinatra |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require 'demo' | ||
run Sinatra::Application |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
require 'rubygems' | ||
require 'sinatra' | ||
require 'oauth2' | ||
require 'json' | ||
require 'cgi' | ||
require 'dalli' | ||
require 'rack/session/dalli' # For Rack sessions in Dalli | ||
|
||
# Dalli is a Ruby client for memcache | ||
def dalli_client | ||
Dalli::Client.new(nil, :compression => true, :namespace => 'rack.session', :expires_in => 3600) | ||
end | ||
|
||
# Use the Dalli Rack session implementation | ||
use Rack::Session::Dalli, :cache => dalli_client | ||
|
||
# Set up the OAuth2 client | ||
def oauth2_client | ||
OAuth2::Client.new( | ||
ENV['CLIENT_ID'], | ||
ENV['CLIENT_SECRET'], | ||
:site => ENV['LOGIN_SERVER'], | ||
:authorize_url =>'/services/oauth2/authorize', | ||
:token_url => '/services/oauth2/token', | ||
:raise_errors => false | ||
) | ||
end | ||
|
||
# Filter for all paths except /oauth* | ||
before do | ||
pass if request.path_info.start_with?("/oauth") | ||
|
||
token = session['access_token'] | ||
@instance_url = session['instance_url'] | ||
|
||
if token | ||
@access_token = OAuth2::AccessToken.from_hash(oauth2_client, { :access_token => token, :header_format => 'OAuth %s' } ) | ||
else | ||
halt erb :auth | ||
end | ||
end | ||
|
||
get '/' do | ||
# Field list isn't very volatile - stash it in the session | ||
if !session['field_list'] | ||
session['field_list'] = @access_token.get("#{@instance_url}/services/data/v21.0/sobjects/Account/describe/").parsed | ||
end | ||
|
||
@field_list = session['field_list'] | ||
|
||
if params[:value] | ||
query = "SELECT Name, Id FROM Account WHERE #{params[:field]} LIKE '#{params[:value]}%' ORDER BY Name LIMIT 20" | ||
else | ||
query = "SELECT Name, Id from Account ORDER BY Name LIMIT 20" | ||
end | ||
|
||
@accounts = @access_token.get("#{@instance_url}/services/data/v20.0/query/?q=#{CGI::escape(query)}").parsed | ||
|
||
erb :index | ||
end | ||
|
||
get '/detail' do | ||
@account = @access_token.get("#{@instance_url}/services/data/v20.0/sobjects/Account/#{params[:id]}").parsed | ||
|
||
erb :detail | ||
end | ||
|
||
post '/action' do | ||
if params[:new] | ||
@action_name = 'create' | ||
@action_value = 'Create' | ||
|
||
@account = Hash.new | ||
@account['Id'] = '' | ||
@account['Name'] = '' | ||
@account['Industry'] = '' | ||
@account['TickerSymbol'] = '' | ||
|
||
done = :edit | ||
elsif params[:edit] | ||
@account = @access_token.get("#{@instance_url}/services/data/v20.0/sobjects/Account/#{params[:id]}").parsed | ||
@action_name = 'update' | ||
@action_value = 'Update' | ||
|
||
done = :edit | ||
elsif params[:delete] | ||
@access_token.delete("#{@instance_url}/services/data/v20.0/sobjects/Account/#{params[:id]}") | ||
@action_value = 'Deleted' | ||
|
||
@result = Hash.new | ||
@result['id'] = params[:id] | ||
|
||
done = :done | ||
end | ||
|
||
erb done | ||
end | ||
|
||
post '/account' do | ||
if params[:create] | ||
body = {"Name" => params[:Name], | ||
"Industry" => params[:Industry], | ||
"TickerSymbol" => params[:TickerSymbol]}.to_json | ||
|
||
@result = @access_token.post("#{@instance_url}/services/data/v20.0/sobjects/Account/", | ||
{:body => body, | ||
:headers => {'Content-type' => 'application/json'}}).parsed | ||
@action_value = 'Created' | ||
elsif params[:update] | ||
body = {"Name" => params[:Name], | ||
"Industry" => params[:Industry], | ||
"TickerSymbol" => params[:TickerSymbol]}.to_json | ||
|
||
# No response for an update | ||
@access_token.post("#{@instance_url}/services/data/v20.0/sobjects/Account/#{params[:id]}?_HttpMethod=PATCH", | ||
{:body => body, | ||
:headers => {'Content-type' => 'application/json'}}) | ||
@action_value = 'Updated' | ||
|
||
@result = Hash.new | ||
@result['id'] = params[:id] | ||
end | ||
|
||
erb :done | ||
end | ||
|
||
get '/logout' do | ||
# First kill the access token | ||
# (Strictly speaking, we could just do a plain GET on the revoke URL, but | ||
# then we'd need to pull in Net::HTTP or somesuch) | ||
@access_token.get(ENV['LOGIN_SERVER']+'/services/oauth2/revoke?token='+session['access_token']) | ||
# Now save the logout_url | ||
@logout_url = session['instance_url']+'/secur/logout.jsp' | ||
# Clean up the session | ||
session['access_token'] = nil | ||
session['instance_url'] = nil | ||
session['field_list'] = nil | ||
# Now give the user some feedback, loading the logout page into an iframe... | ||
erb :logout | ||
end | ||
|
||
get '/oauth' do | ||
redirect oauth2_client.auth_code.authorize_url( | ||
:redirect_uri => "https://#{request.host}/oauth/callback" | ||
) | ||
end | ||
|
||
get '/oauth/callback' do | ||
begin | ||
access_token = oauth2_client.auth_code.get_token(params[:code], | ||
:redirect_uri => "https://#{request.host}/oauth/callback") | ||
|
||
session['access_token'] = access_token.token | ||
session['instance_url'] = access_token.params['instance_url'] | ||
|
||
redirect '/' | ||
rescue => exception | ||
output = '<html><body><tt>' | ||
output += "Exception: #{exception.message}<br/>"+exception.backtrace.join('<br/>') | ||
output += '<tt></body></html>' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
body { | ||
font-family:"Arial"; | ||
} | ||
table.main { | ||
border: 1px solid #666; | ||
} | ||
.highlighted { | ||
background-color:#D6EDFC; | ||
} | ||
.odd { | ||
background-color:#fea; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
<title>REST/OAuth Example</title> | ||
</head> | ||
<body> | ||
<script type="text/javascript" language="javascript"> | ||
if (window.location.protocol != "https:") { | ||
document.write("OAuth will not work correctly from plain http. "+ | ||
"Please use an https URL."); | ||
} else { | ||
window.location.href = "/oauth"; | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Account Detail</title> | ||
<link href="/stylesheets/style.css" rel="stylesheet" type="text/css" /> | ||
</head> | ||
<body> | ||
<h1>Account Detail</h1> | ||
<table> | ||
<tr><td>Account Name:</td><td id="accountname"><%= @account['Name'] %></td></tr> | ||
<tr><td>Industry:</td><td><a id="industry" href="/?field=Industry&value=<%= @account['Industry'] %>&search=Search"><%= @account['Industry'] %></a></td></tr> | ||
<tr><td>Ticker Symbol:</td><td id="tickersymbol"><%= @account['TickerSymbol'] %></td></tr> | ||
</table> | ||
<form action="/action" method="post"> | ||
<input type="hidden" name="id" id="id" value="<%= @account['Id'] %>" /> | ||
<input type="submit" id="delete" name="delete" value="Delete" /> | ||
<input type="submit" id="edit" name="edit" value="Edit" /> | ||
</form> | ||
<p><a href="/">Home</a></p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title><%= @action_value %> Account</title> | ||
<link href="/stylesheets/style.css" rel="stylesheet" type="text/css" /> | ||
</head> | ||
<body onLoad="setTimeout(function(){window.location = '/';}, 3000)"> | ||
<p><%= @action_value %> <span id="id"><%= @result['id'] %></span> | ||
<p><a href="/">Home</a></p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Account Detail</title> | ||
<link href="/stylesheets/style.css" rel="stylesheet" type="text/css" /> | ||
</head> | ||
<body> | ||
<h1 id="header">Account Detail</h1> | ||
<form id="editform" action="account" method="post"> | ||
<input type="hidden" name="id" id="id" value="<%= @account['Id'] %>" /> | ||
<table> | ||
<tr><td>Name:</td><td><input name="Name" id="Name" value="<%= @account['Name'] %>"/></td></tr> | ||
<tr><td>Industry:</td><td><input name="Industry" id="Industry" value="<%= @account['Industry'] %>"/></td></tr> | ||
<tr><td>Ticker Symbol:</td><td><input name="TickerSymbol" id="TickerSymbol" value="<%= @account['TickerSymbol'] %>"/></td></tr> | ||
</table> | ||
<br/> | ||
<input type="submit" id="action" name="<%= @action_name %>" value="<%= @action_value %>" /> | ||
</form> | ||
<p><a href="/">Home</a></p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Account List</title> | ||
<link href="/stylesheets/style.css" rel="stylesheet" type="text/css" /> | ||
</head> | ||
<body> | ||
<h2>Accounts for <span id="displayname">User Name</span></h2> | ||
<form action="/" method="get"> | ||
<h3 style="display:inline;">Filter on</h3> | ||
<select id="field" name="field"> | ||
<% @field_list['fields'].each do |field| %> | ||
<% if field['type'] == 'string' %> | ||
<option value="<%= field['name'] %>"><%= field['label'] %></option> | ||
<% end %> | ||
<% end %> | ||
</select> | ||
<input type="text" id="value" name="value" /> | ||
<input type="submit" id="go" name="search" value="Search" /> | ||
</form> | ||
<form action="/action" method="post"> | ||
<input type="submit" id="new" name="new" value="New" /> | ||
</form> | ||
<table class="accountlist"> | ||
<% @accounts['records'].each do |record| %> | ||
<tr><td><a href="detail?id=<%= record['Id'] %>"><%= record['Name'] %></a></td></tr> | ||
<% end %> | ||
</table> | ||
<br/> | ||
<a href="logout" id="logout">Logout</a> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Logged Out</title> | ||
<link href="/stylesheets/style.css" rel="stylesheet" type="text/css" /> | ||
</head> | ||
<body> | ||
<p>Logged out</p> | ||
<p><a href="/">Login Again</a></p> | ||
<iframe style="display:none;" src="<%= @logout_url %>"/> | ||
</body> | ||
</html> |