-
-
Save dfischer/1937412 to your computer and use it in GitHub Desktop.
Ruby Sample Application for AT&T Alpha API Foundry
This file contains hidden or 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
require File.expand_path("helloapp", File.dirname(__FILE__)) | |
run Sinatra::Application |
This file contains hidden or 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
source :rubygems | |
gem 'rack', "~> 1.3.6" | |
gem 'sinatra' | |
gem 'json' | |
gem 'oauth2' |
This file contains hidden or 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
require 'rubygems' | |
require 'sinatra' | |
require 'oauth2' | |
require 'json' | |
def client | |
OAuth2::Client.new((ENV['FOUNDRY_CLIENT_ID']||'testing'), | |
(ENV['FOUNDRY_CLIENT_SECRET']||'testing'), | |
:site => 'http://apifoundry-dev.pao1.tfoundry.com', | |
:authorize_url => 'http://apifoundry-dev.pao1.tfoundry.com/oauth/authorize', | |
:token_url => 'http://apifoundry-dev.pao1.tfoundry.com/oauth/access_token') | |
end | |
get "/" do | |
erb :index | |
end | |
get '/auth' do | |
url = client.auth_code.authorize_url(:redirect_uri => redirect_uri, :response_type => "client_credentials", :scope => "TL") | |
puts "Redirecting to URL: #{url.inspect}" | |
redirect url | |
end | |
get '/auth/callback' do | |
begin | |
access_token = client.auth_code.get_token(params[:code], :redirect_uri => redirect_uri) | |
location = JSON.parse(access_token.get("/1/devices/tel:3105971013/location?access_token=#{access_token.token}&requestedAccuracy=1000").body) | |
"<p>Your location:\n#{location.inspect}</p>" | |
rescue OAuth2::Error => e | |
%(<p>#{$!}</p><p><a href="/auth">Retry</a></p>) | |
end | |
end | |
get '/auth/failure' do | |
erb "<h1>Authentication Failed:</h1><h3>message:<h3> <pre>#{params}</pre>" | |
end | |
def redirect_uri(path = '/auth/callback', query = nil) | |
uri = URI.parse(request.url) | |
uri.path = path | |
uri.query = query | |
uri.to_s | |
end |
This file contains hidden or 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
<p><a href="/auth">Try to authorize</a>.</p> |
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Bootstrap, from Twitter</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta name="description" content=""> | |
<meta name="author" content=""> | |
<!-- Le styles --> | |
<link href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" rel="stylesheet"> | |
<style> | |
body { | |
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */ | |
} | |
</style> | |
<link href="/css/bootstrap-responsive.css" rel="stylesheet"> | |
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements --> | |
<!--[if lt IE 9]> | |
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script> | |
<![endif]--> | |
</head> | |
<body> | |
<div class="navbar navbar-fixed-top"> | |
<div class="navbar-inner"> | |
<div class="container"> | |
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
</a> | |
<a class="brand" href="#">Project name</a> | |
<div class="nav-collapse"> | |
<ul class="nav"> | |
<li class="active"><a href="#">Home</a></li> | |
<li><a href="#about">About</a></li> | |
<li><a href="#contact">Contact</a></li> | |
</ul> | |
</div><!--/.nav-collapse --> | |
</div> | |
</div> | |
</div> | |
<div class="container"> | |
<%= yield %> | |
<h1>Bootstrap starter template</h1> | |
<p>Use this document as a way to quick start any new project.<br> All you get is this message and a barebones HTML document.</p> | |
</div> <!-- /container --> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment