Permalink
Browse files

I hate trailing whitespaces.

  • Loading branch information...
1 parent 72eee72 commit 81920292f46b7933d07fe99b1e0e1b20d4d76d40 Adolfo Builes committed Oct 3, 2011
Showing with 52 additions and 52 deletions.
  1. +16 −16 lib/shoutbox.rb
  2. +3 −3 lib/shoutbox/authentication.rb
  3. +10 −10 lib/shoutbox/data.rb
  4. +7 −7 lib/shoutbox/document.rb
  5. +2 −2 server.rb
  6. +14 −14 spec/shoutbox_spec.rb
View
@@ -6,68 +6,68 @@
class Shoutbox
include Authentication
include Util
-
+
def self.initialize
initialize_mongodb
end
-
+
def self.full_host
configuration['full_host']
end
-
+
def self.twitter_consumer_key
configuration['twitter']['key']
end
-
+
def self.twitter_consumer_secret
configuration['twitter']['secret']
end
-
+
def self.configuration
@configuration ||= configuration_hash
end
-
+
def self.get_current_status( account_name )
throw(:halt, [400, "empty account_name\n"]) if account_name.nil?
ShoutboxDocument.find_or_create_for_account( account_name ).current_status.to_json
end
-
+
def self.get_account_name_from_auth_token( auth_token )
ShoutboxDocument.find_for_auth_token( auth_token ).account_name
end
-
+
def self.update_status( account_name, update_data )
document = ShoutboxDocument.find_or_create_for_account( account_name )
document.update_status( update_data )
Shoutbox::Bayeux::Broadcast.message( account_name, document.auth_token, update_data.to_hash )
end
-
+
def self.delete_status( account_name, update_data )
document = ShoutboxDocument.find_or_create_for_account( account_name )
document.delete_status( update_data )
Shoutbox::Bayeux::Broadcast.message( account_name, document.auth_token, update_data.to_hash )
end
-
+
def self.auth_token_for( account_name )
ShoutboxDocument.find_or_create_for_account( account_name ).auth_token
end
-
- private
-
+
+ private
+
def self.initialize_mongodb
Mongoid.configure do |config|
config.from_hash(mongodb_config_hash)
end
end
-
+
def self.configuration_hash
file_name = File.join(File.dirname(__FILE__), "..", "config", "shoutbox.yml")
hash = YAML.load_file( file_name )
end
-
+
def self.mongodb_config_hash
file_name = File.join(File.dirname(__FILE__), "..", "config", "mongodb.yml")
hash = YAML.load_file( file_name )
hash['production']
end
-end
+end
@@ -3,15 +3,15 @@
class Shoutbox
module Authentication
DEFAULT_SALT = 'this is the default shoutbox salt'
-
+
def self.authenticate?( account_name, auth_token )
auth_token == ShoutboxDocument.find_document_for( account_name ).auth_token
end
-
+
def self.create_auth_token_for( account_name )
salt = Digest::SHA2.hexdigest("#{Time.now.utc}#{account_name}#{DEFAULT_SALT}")
token = Digest::SHA2.hexdigest("#{salt}--#{account_name}")
return [salt, token]
end
end
-end
+end
View
@@ -1,8 +1,8 @@
class Shoutbox
class ShoutboxData
-
+
attr_accessor :name, :status, :message, :group, :slug, :updated_at, :expires_at
-
+
def initialize( hash )
self.name = hash['display_name'] || hash['name']
self.status = hash['status']
@@ -13,16 +13,16 @@ def initialize( hash )
self.updated_at = Time.now.to_i
self
end
-
+
def valid?
valid_shoutbox_status? and valid_shoutbox_message?
end
-
+
def to_hash
{ :name => self.name,
- :status => self.status,
+ :status => self.status,
:message => self.message,
- :slug => self.slug,
+ :slug => self.slug,
:updated_at => self.updated_at,
:expires_at => self.expires_at,
:group => self.group }
@@ -37,19 +37,19 @@ def self.from_json_string( json_string )
end
- private
+ private
def expiration_time( expires_in )
Time.now.to_i + (expires_in ? expires_in : (60*60*24 + 15))
end
-
+
def valid_shoutbox_message?
((status == 'red' or status == 'yellow') and (message.nil? or message == '')) ? false : true
end
-
+
def valid_shoutbox_status?
['red','green','yellow','remove'].include?(self.status)
end
-
+
end
end
View
@@ -8,37 +8,37 @@ class ShoutboxDocument
include Mongoid::Document
include Mongoid::Timestamps
include Status
-
+
field :account_name
field :status_data_json
field :auth_token
field :auth_salt
-
+
def self.find_or_create_for_account( account_name )
find_document_for( account_name ) || create_shoutbox_document_for( account_name )
end
-
+
def self.find_for_auth_token( auth_token )
self.where( :auth_token => auth_token ).first
end
-
+
def self.find_document_for( account_name )
self.where( :account_name => account_name ).first
end
-
+
private
def self.create_shoutbox_document_for( account_name )
salt, token = Shoutbox::Authentication.create_auth_token_for( account_name )
self.create!( :account_name => account_name, :status_data => default_status, :auth_token => token, :auth_salt => salt )
end
-
+
def self.default_status
{ "Home" => { "home-use-shoutbox" => { "slug" => "home-use-shoutbox",
"updated_at" => Time.now.to_i,
"expires_at" => Time.now.to_i + 15*60,
"name" => "Use Shoutbox",
- "status" => "red",
+ "status" => "red",
"message" => "You can remove me by using your shoutbox client." } } }
end
end
View
@@ -70,9 +70,9 @@
"OK"
end
-private
+private
-def current_user
+def current_user
@current_user ||= session[:user_name] || account_name_from_auth_token || account_name_from_omniauth
throw(:halt, [401, "Unable to identify you\n"]) unless @current_user
response.headers['X-Shoutbox-Auth-Token'] = Shoutbox.auth_token_for( @current_user )
View
@@ -6,20 +6,20 @@
def app
@app ||= Sinatra::Application
end
-
+
# context 'authentication' do
# it 'should respond with a 401 status code if remote user is not set' do
# get '/'
# last_response.status.should == 401
# end
- #
+ #
# it 'should allow acces for authorized users' do
# authorize 'admin', 'admin'
# get '/'
# last_response.should be_redirect
# end
# end
-
+
context 'user management' do
it 'should creating the default shoutbox document shoutbox document with default data' do
expect {
@@ -28,17 +28,17 @@ def app
}.to change(Shoutbox::ShoutboxDocument, :count).by(1)
end
end
-
+
context 'updating status - error handling' do
before(:each) do
@auth_token = Shoutbox.auth_token_for( 'my_shoutbox' )
end
-
+
it 'should respond with a 400 status code if bad data transmitted' do
put '/status', nil, tokenauth_env(@auth_token).update(:input => "this is not json")
last_response.status.should == 400
end
-
+
it 'should respond with a 400 status code if invalid json data transmitted' do
put '/status', nil, tokenauth_env(@auth_token).update(:input => invalid_shoutbox_data.to_json)
last_response.status.should == 400
@@ -54,47 +54,47 @@ def app
last_response.status.should == 400
end
end
-
+
context 'update status - valid data' do
before(:each) do
@auth_token = Shoutbox.auth_token_for( 'my_shoutbox' )
end
-
+
it "should respond with a 200 status code if valid json data transmitted" do
put '/status', nil, tokenauth_env(@auth_token).update(:input => valid_shoutbox_data.to_json)
last_response.status.should == 200
end
-
+
it 'should include the auth_key in the data hash' do
get '/data', nil, omniauth_env
last_response.headers['X-Shoutbox-Auth-Token'].should == Shoutbox::ShoutboxDocument.find_document_for( 'my_shoutbox' ).auth_token
end
-
+
it 'should set the expiration time correctly' do
time = Time.now.to_i + 60
put '/status', nil, tokenauth_env(@auth_token).update(:input => valid_shoutbox_data.update('expires_in' => 60).to_json)
last_response.status.should == 200
get '/data', nil, omniauth_env
JSON.parse(last_response.body)[valid_shoutbox_data['group']][valid_shoutbox_data['name']]['expires_at'].should == time
end
-
+
end
-
+
def tokenauth_env( auth_token )
{ 'HTTP_X_SHOUTBOX_AUTH_TOKEN' => auth_token, 'CONTENT_TYPE' => 'application/json' }
end
def omniauth_env
{ 'omniauth.auth' => { 'user_info' => { 'screen_name' => 'my_shoutbox' } }, 'CONTENT_TYPE' => 'application/json' }
end
-
+
def valid_shoutbox_data
{ 'name' => 'Status Name',
'group' => 'Default Group',
'message' => 'This is the message',
'status' => 'green' }
end
-
+
def invalid_shoutbox_data
data = valid_shoutbox_data.clone
data.delete('status')

0 comments on commit 8192029

Please sign in to comment.