Skip to content

Commit

Permalink
Chubbies now uses sqlite to have tables of pods and users
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwell committed Jun 3, 2011
1 parent e720fc0 commit 4b13c83
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public/assets/*
public/source.tar*
tmp/**/*
tmp/*
db/*.sqlite3
*.sqlite3

# Temporary files of every sort
.sass-cache/*
Expand Down
4 changes: 0 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,4 @@ group :test do
gem 'mongrel', :require => false if RUBY_VERSION.include? '1.8'
gem 'rspec-instafail', '>= 0.1.7', :require => false
gem 'fuubar'

#For Chubbies
gem 'sinatra', :require => false
gem 'httparty', :require => false
end
4 changes: 0 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ GEM
hashie (1.0.0)
highline (1.6.2)
http_connection (1.4.1)
httparty (0.7.4)
crack (= 0.1.8)
i18n (0.6.0)
i18n-inflector (2.5.1)
i18n (>= 0.4.1)
Expand Down Expand Up @@ -426,7 +424,6 @@ DEPENDENCIES
fuubar
haml (= 3.0.25)
http_accept_language!
httparty
i18n-inflector-rails (~> 1.0)
jammit (= 0.5.4)
jasmine (= 1.0.2.1)
Expand All @@ -452,7 +449,6 @@ DEPENDENCIES
ruby-debug
selenium-webdriver (= 0.1.3)
settingslogic (= 2.0.6)
sinatra
sod!
thin (= 1.2.11)
twitter (= 1.5.0)
Expand Down
3 changes: 3 additions & 0 deletions app/views/authorizations/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
.description
= @client.description

%p
= "You are currently logged in as #{current_user.name}(#{current_user.diaspora_handle})."
= link_to("Not You?", destroy_user_session_path)
%br

%p
Expand Down
2 changes: 1 addition & 1 deletion features/step_definitions/oauth_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Chubbies

def self.run
@pid = fork do
Process.exec "cd #{Rails.root}/spec/support/chubbies/ && DIASPORA_PORT=9887 bundle exec rackup -p #{PORT} 2> /dev/null"
Process.exec "cd #{Rails.root}/spec/support/chubbies/ && BUNDLE_GEMFILE=Gemfile DIASPORA_PORT=9887 bundle exec rackup -p #{PORT} 2> /dev/null"
end
while(!running?) do
sleep(1)
Expand Down
2 changes: 2 additions & 0 deletions spec/support/chubbies/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ gem 'haml'
gem 'httparty'
gem 'json'
gem 'shotgun'
gem 'sqlite3'
gem 'activerecord'
17 changes: 17 additions & 0 deletions spec/support/chubbies/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
GEM
remote: http://rubygems.org/
specs:
activemodel (3.0.7)
activesupport (= 3.0.7)
builder (~> 2.1.2)
i18n (~> 0.5.0)
activerecord (3.0.7)
activemodel (= 3.0.7)
activesupport (= 3.0.7)
arel (~> 2.0.2)
tzinfo (~> 0.3.23)
activesupport (3.0.7)
arel (2.0.10)
builder (2.1.2)
crack (0.1.8)
haml (3.0.18)
httparty (0.7.4)
crack (= 0.1.8)
i18n (0.5.0)
json (1.4.6)
rack (1.2.2)
shotgun (0.9)
rack (>= 1.0)
sinatra (1.2.6)
rack (~> 1.1)
tilt (>= 1.2.2, < 2.0)
sqlite3 (1.3.3)
tilt (1.3)
tzinfo (0.3.27)

PLATFORMS
ruby

DEPENDENCIES
activerecord
haml
httparty
json
shotgun
sinatra
sqlite3
123 changes: 65 additions & 58 deletions spec/support/chubbies/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,88 +4,99 @@
require 'haml'
require 'httparty'
require 'json'
require 'active_record'

# models ======================================
`rm -f chubbies.sqlite3`
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "chubbies.sqlite3"
)

ActiveRecord::Schema.define do
create_table :users do |table|
table.string :diaspora_handle
table.string :access_token
table.integer :pod_id
end

def resource_host
url = "http://localhost:"
if ENV["DIASPORA_PORT"]
url << ENV["DIASPORA_PORT"]
else
url << "3000"
create_table :pods do |table|
table.string :host
table.string :client_id
table.string :client_secret
end
url
end

@@client_id = nil
@@client_secret = nil
RESOURCE_HOST = resource_host
class User < ActiveRecord::Base
attr_accessible :diaspora_handle, :access_token
belongs_to :pod
end

enable :sessions
class Pod < ActiveRecord::Base
attr_accessible :host, :client_id, :client_secret
has_many :users

helpers do
def redirect_uri
"http://" + request.host_with_port + "/callback" << "?diaspora_handle=#{params['diaspora_handle']}"
def authorize_url(redirect_uri)
"http://" + host + "/oauth/authorize?client_id=#{client_id}&client_secret=#{client_secret}&redirect_uri=#{redirect_uri}"
end

def access_token
session[:access_token]
def token_url
"http://" + host + "/oauth/token"
end

def get_with_access_token(path)
HTTParty.get('http://' + domain_from_handle + path, :query => {:oauth_token => access_token})
def access_token_url
"http://" + host + "/oauth/access_token"
end
end

def authorize_url
"http://" + domain_from_handle + "/oauth/authorize?client_id=#{@@client_id}&client_secret=#{@@client_secret}&redirect_uri=#{redirect_uri}"
end
enable :sessions

def token_url
"http://" + domain_from_handle + "/oauth/token"
helpers do
def redirect_uri
"http://" + request.host_with_port + "/callback" << "?diaspora_handle=#{params['diaspora_handle']}"
end

def access_token_url
"http://" + domain_from_handle + "/oauth/access_token"
def get_with_access_token(user, path)
HTTParty.get('http://' + user.pod.host + path, :query => {:oauth_token => user.access_token})
end
end

get '/' do
@pods = Pod.scoped.includes(:users).all
haml :home
end

get '/callback' do
unless params["error"]

if(params["client_id"] && params["client_secret"])
@@client_id = params["client_id"]
@@client_secret = params["client_secret"]
redirect '/account'

else
response = HTTParty.post(access_token_url, :body => {
:client_id => @@client_id,
:client_secret => @@client_secret,
:redirect_uri => redirect_uri,
:code => params["code"],
:grant_type => 'authorization_code'}
)

session[:access_token] = response["access_token"]
redirect "/account?diaspora_handle=#{params['diaspora_handle']}"
end
pod = Pod.where(:host => domain_from_handle).first

response = HTTParty.post(pod.access_token_url, :body => {
:client_id => pod.client_id,
:client_secret => pod.client_secret,
:redirect_uri => redirect_uri,
:code => params["code"],
:grant_type => 'authorization_code'}
)

user = pod.users.create!(:access_token => response["access_token"], :diaspora_handle => params['diaspora_handle'])
redirect "/account?diaspora_handle=#{user.diaspora_handle}"
else
"What is your major malfunction?"
end
end

get '/account' do
if !@@client_id && !@@client_secret
register_with_pod
# have diaspora handle
host = domain_from_handle
unless pod = Pod.where(:host => host).first
pod = register_with_pod
end

if access_token
@resource_response = get_with_access_token("/api/v0/me")
if user = pod.users.where(:diaspora_handle => params['diaspora_handle']).first
@resource_response = get_with_access_token(user, "/api/v0/me")
haml :response
else
redirect authorize_url
redirect pod.authorize_url(redirect_uri)
end
end

Expand All @@ -98,12 +109,6 @@ def access_token_url
}.to_json
end

get '/reset' do
@@client_id = nil
@@client_secret = nil
end


#=============================
#helpers
#
Expand All @@ -113,15 +118,17 @@ def domain_from_handle
end

def register_with_pod
response = HTTParty.post(token_url, :body => {
pod = Pod.new(:host => domain_from_handle)

response = HTTParty.post(pod.token_url, :body => {
:type => :client_associate,
:manifest_url => "http://" + request.host_with_port + "/manifest"
})

json = JSON.parse(response.body)
pod.update_attributes(json)

@@client_id = json["client_id"]
@@client_secret = json["client_secret"]
pod.save!
pod
end


35 changes: 33 additions & 2 deletions spec/support/chubbies/views/home.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,37 @@
Diaspora Handle
%input{:type=>'text', :id => 'diaspora_handle', :name => 'diaspora_handle'}
%input{:type => 'submit', :value => "Log in with Diaspora" }
%br
%br
%br
%table
%th
Host
%th
Client ID
%th
Client Secret
%th
Users
- @pods.each do |pod|
%tr
%td
= pod.host
%td
= pod.client_id
%td
= pod.client_secret
%td
- pod.users.each do |user|
%table
%th
Diaspora Handle
%th
Access Token
%tr
%td
= user.diaspora_handle
%td
= user.access_token




0 comments on commit 4b13c83

Please sign in to comment.