Permalink
Browse files

Restructure classes, and add sinatra-flash messaging

  • Loading branch information...
1 parent 4c2db40 commit f98e5730833d44c3dce8864a20e4ceafd8f54812 @punkstar committed Mar 18, 2012
Showing with 146 additions and 115 deletions.
  1. +4 −1 Gemfile
  2. +4 −0 Gemfile.lock
  3. +1 −1 bin/scan_network
  4. +3 −1 lib/autocheckin.rb
  5. +0 −91 lib/autocheckin/base.rb
  6. +71 −0 lib/autocheckin/models.rb
  7. +29 −0 lib/autocheckin/network.rb
  8. +26 −15 lib/autocheckin/server.rb
  9. +8 −6 var/server/views/layout.erb
View
@@ -12,4 +12,7 @@ gem 'foursquare2'
gem 'oauth2'
# nmap result parsing
-gem 'nmap-parser'
+gem 'nmap-parser'
+
+# Require an older version of rack due to a bug in rack-flash (https://github.com/nakajima/rack-flash/issues/8)
+gem "sinatra-flash"
View
@@ -1,4 +1,5 @@
GEM
+ remote: http://rubygems.org/
specs:
addressable (2.2.7)
bcrypt-ruby (3.0.1)
@@ -77,6 +78,8 @@ GEM
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
+ sinatra-flash (0.3.0)
+ sinatra (>= 1.0.0)
stringex (1.3.2)
tilt (1.3.3)
uuidtools (2.1.2)
@@ -91,3 +94,4 @@ DEPENDENCIES
nmap-parser
oauth2
sinatra
+ sinatra-flash
View
@@ -4,7 +4,7 @@ lib_dir = File.join(File.dirname(__FILE__), '..', 'lib')
$LOAD_PATH.unshift lib_dir if File.directory?(lib_dir)
require 'autocheckin'
-require 'pp'
+require 'autocheckin/network'
DataMapper.auto_upgrade!
View
@@ -1,2 +1,4 @@
require 'autocheckin/config'
-require 'autocheckin/base'
+require 'autocheckin/base'
+require 'autocheckin/models'
+require 'autocheckin/network'
@@ -1,94 +1,3 @@
module Autocheckin
- require 'data_mapper'
- require 'nmap/parser'
- require 'digest/sha1'
- require 'foursquare2'
-
- # @FIXME: This is not advised, but fixes the "OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed" error
- require 'openssl'
- OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
-
- # Connect to the database
- DataMapper.setup(:default, Autocheckin::Config.instance.settings['database']['dsn'])
-
- class Network
-
- def find_hosts()
- parser = Nmap::Parser.parsescan("sudo nmap", "-sP " + Autocheckin::Config.instance.settings['network']['scan_range'])
-
- puts "Nmap args: #{parser.session.scan_args}"
-
- parser.hosts('up') do |host|
- ip = host.addr
- mac = host.mac_addr
- puts "Found #{ip}"
-
- if not ip.nil? and not mac.nil?
- yield mac.chomp.upcase, ip.chomp
- end
- end
- end
- end
-
- class User
- include DataMapper::Resource
-
- has n, :devices
-
- property :id, Serial
- property :email, String, :required => true, :length => 256, :unique => true
- property :hashed_password, String
- property :salt, String
- property :token, String
-
- attr_accessor :password
-
- def generate_hashed_password(password, salt)
- Digest::SHA1.hexdigest(salt + password)
- end
-
- def perform_checkin
- client = Foursquare2::Client.new(:oauth_token => self.token)
- client.add_checkin(:venueId => Autocheckin::Config.instance.settings['foursquare']['venue_id'], :broadcast => 'public', :shout => "Automatic check-in.")
- end
-
- def authenticate(password)
- self.hashed_password == self.generate_hashed_password(password, self.salt)
- end
-
-
- before :create do
- throw :halt if self.password.nil?
-
- self.salt = (0...16).map{ ('a'..'z').to_a[rand(26)] }.join
- self.hashed_password = self.generate_hashed_password(self.password, self.salt)
- end
- end
-
- class Device
- include DataMapper::Resource
-
- belongs_to :user
-
- has n, :entries
-
- property :id, Serial
- property :name, String, :length => 50, :required => true
- property :mac_addr, String, :required => true, :unique => true
-
- before :save do
- self.mac_addr = self.mac_addr.upcase
- end
- end
-
- class Entry
- include DataMapper::Resource
-
- belongs_to :device
-
- property :id, Serial
- property :ip_addr, IPAddress
- property :created_at, DateTime
- end
end
@@ -0,0 +1,71 @@
+#!/usr/bin/env ruby
+
+module Autocheckin
+ require 'foursquare2'
+ require 'data_mapper'
+ require 'digest/sha1'
+
+ # Connect to the database
+ DataMapper.setup(:default, Autocheckin::Config.instance.settings['database']['dsn'])
+
+ class User
+ include DataMapper::Resource
+
+ has n, :devices
+
+ property :id, Serial
+ property :email, String, :required => true, :length => 256, :unique => true
+ property :hashed_password, String
+ property :salt, String
+ property :token, String
+
+ attr_accessor :password
+
+ def generate_hashed_password(password, salt)
+ Digest::SHA1.hexdigest(salt + password)
+ end
+
+ def perform_checkin
+ client = Foursquare2::Client.new(:oauth_token => self.token)
+ client.add_checkin(:venueId => Autocheckin::Config.instance.settings['foursquare']['venue_id'], :broadcast => 'public', :shout => "Automatic check-in.")
+ end
+
+ def authenticate(password)
+ self.hashed_password == self.generate_hashed_password(password, self.salt)
+ end
+
+
+ before :create do
+ throw :halt if self.password.nil?
+
+ self.salt = (0...16).map{ ('a'..'z').to_a[rand(26)] }.join
+ self.hashed_password = self.generate_hashed_password(self.password, self.salt)
+ end
+ end
+
+ class Device
+ include DataMapper::Resource
+
+ belongs_to :user
+
+ has n, :entries
+
+ property :id, Serial
+ property :name, String, :length => 50, :required => true
+ property :mac_addr, String, :required => true, :unique => true
+
+ before :save do
+ self.mac_addr = self.mac_addr.upcase
+ end
+ end
+
+ class Entry
+ include DataMapper::Resource
+
+ belongs_to :device
+
+ property :id, Serial
+ property :ip_addr, IPAddress
+ property :created_at, DateTime
+ end
+end
@@ -0,0 +1,29 @@
+#!/usr/bin/env ruby
+
+module Autocheckin
+ require 'nmap/parser'
+
+ # @FIXME: This is not advised, but fixes the "OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed" error
+ require 'openssl'
+ OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
+
+ class Network
+
+ def find_hosts()
+ parser = Nmap::Parser.parsescan("sudo nmap", "-sP " + Autocheckin::Config.instance.settings['network']['scan_range'])
+
+ puts "Nmap args: #{parser.session.scan_args}"
+
+ parser.hosts('up') do |host|
+ ip = host.addr
+ mac = host.mac_addr
+
+ puts "Found #{ip}"
+
+ if not ip.nil? and not mac.nil?
+ yield mac.chomp.upcase, ip.chomp
+ end
+ end
+ end
+ end
+end
@@ -4,12 +4,13 @@ module Autocheckin
require 'sinatra/base'
require 'foursquare2'
require 'oauth2'
- require 'pp'
+ require 'sinatra/flash'
class Server < Sinatra::Base
-
enable :sessions
+ register Sinatra::Flash
+
set :root, File.join(File.dirname(__FILE__), '..', '..', 'var', 'server')
set :views, settings.root + '/views'
@@ -54,11 +55,12 @@ def initialize
if user.save
session[:user] = user.id
- redirect to('/?success=true')
- else
- pp user.errors
- redirect to('/login?fail=true&email=' + email)
+ flash[:success] = "You've been registered"
+ redirect to('/')
+ else
+ flash[:error] = "There was a problem with your registration"
+ redirect to('/login?email=' + email)
end
end
@@ -77,9 +79,12 @@ def initialize
if not user.nil? and user.authenticate(password)
session[:user] = user.id
- redirect to('/?success=true')
+
+ flash[:success] = "You've been logged in"
+ redirect to('/')
else
- redirect to('/login?fail=true&email=' + email)
+ flash[:error] = "Invalid username or password"
+ redirect to('/login?email=' + email)
end
end
@@ -88,7 +93,8 @@ def initialize
########################################################
get '/logout' do
session[:user] = nil
- redirect to('/?success=true')
+ flash[:success] = "You have been logged out"
+ redirect to('/')
end
########################################################
@@ -103,9 +109,11 @@ def initialize
device = Device.new( :user => current_user(), :mac_addr => mac_addr, :name => name )
if device.save
- redirect to('/?success=true')
+ flash[:success] = "Your device has been added"
+ redirect to('/')
else
- redirect to('/?fail=true&mac_addr=' + mac_addr + '&name=' + name)
+ flash[:fail] = "You've been logged in!"
+ redirect to('/?mac_addr=' + mac_addr + '&name=' + name)
end
end
@@ -118,9 +126,11 @@ def initialize
if not device.nil?
device.destroy
- redirect to('/?success=true')
+ flash[:success] = "Device has been deleted"
+ redirect to('/')
else
- redirect to('/?fail=true')
+ flash[:error] = "There was a problem deleting the device"
+ redirect to('/')
end
end
@@ -139,8 +149,9 @@ def initialize
token = @foursquare.auth_code.get_token(code, :redirect_uri => @settings['foursquare']['callback_url'])
current_user.update(:token => token.token)
-
- redirect to('/?success=true')
+
+ flash[:success] = "We're all hooked up for Foursquare!"
+ redirect to('/')
end
end
end
@@ -31,14 +31,16 @@
</div>
<% end %>
- <% if params[:fail] %>
+ <% if flash[:error] %>
<div class="alert alert-error">
- There was an error with something you just did.. probably. I'll change then when I can be arsed to implement flash messaging. Sorry.
+ <strong>There was a problem:</strong> <%= flash[:error] %>
+ </div>
+ <% end %>
+
+ <% if flash[:success] %>
+ <div class="alert alert-success">
+ <strong>Sweet!</strong> <%= flash[:success] %>
</div>
- <% elsif params[:success] %>
- <div class="alert alert-success">
- Whatever you just did was successful. I'll change then when I can be arsed to implement flash messaging. Sorry.
- </div>
<% end %>
<% if current_user and current_user.token.nil? %>

0 comments on commit f98e573

Please sign in to comment.