Skip to content
This repository has been archived by the owner on Mar 20, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Initial commit
  • Loading branch information
M7 committed Apr 24, 2012
1 parent 8aef903 commit 2fee234
Show file tree
Hide file tree
Showing 97 changed files with 2,394 additions and 66 deletions.
24 changes: 24 additions & 0 deletions .gitignore
@@ -0,0 +1,24 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile ~/.gitignore_global

# Ignore bundler config
/.bundle

# Ignore the default SQLite database.
/db/*.sqlite3

# Ignore all logfiles and tempfiles.
/log/*.log
/tmp

# This code will be Open Source, so don't include files with sensitive data
/config/database.yml

# Ignore .watchr file
/.watchr

# Random files to ignore
/lib/whenbot/old_trigger.rb
55 changes: 55 additions & 0 deletions .rvmrc
@@ -0,0 +1,55 @@
#!/usr/bin/env bash

# This is an RVM Project .rvmrc file, used to automatically load the ruby
# development environment upon cd'ing into the directory

# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
environment_id="ruby-1.9.3-p125@whenbot"

#
# Uncomment following line if you want options to be set only for given project.
#
# PROJECT_JRUBY_OPTS=( --1.9 )

#
# First we attempt to load the desired environment directly from the environment
# file. This is very fast and efficient compared to running through the entire
# CLI and selector. If you want feedback on which environment was used then
# insert the word 'use' after --create as this triggers verbose mode.
#
if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
then
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"

if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
then
. "${rvm_path:-$HOME/.rvm}/hooks/after_use"
fi
else
# If the environment file has not yet been created, use the RVM CLI to select.
if ! rvm --create "$environment_id"
then
echo "Failed to create RVM environment '${environment_id}'."
exit 1
fi
fi

#
# If you use an RVM gemset file to install a list of gems (*.gems), you can have
# it be automatically loaded. Uncomment the following and adjust the filename if
# necessary.
#
# filename=".gems"
# if [[ -s "$filename" ]]
# then
# rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
# fi

# If you use bundler, this might be useful to you:
# if command -v bundle && [[ -s Gemfile ]]
# then
# bundle install
# fi


58 changes: 58 additions & 0 deletions Gemfile
@@ -0,0 +1,58 @@
source 'https://rubygems.org'

gem 'rails', '3.2.3'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'pg'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', :platform => :ruby

gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

gem 'heroku'

group :test, :development do
gem 'awesome_print'
gem 'factory_girl_rails'
end

group :development do
#gem 'ruby-debug19', :require => 'ruby-debug' # To use debugger
gem 'rails-footnotes', '>= 3.7'
end

group :test do
gem 'test-unit' # not bundled with ruby 1.9
gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git' # For #has_text?
gem 'database_cleaner' # For Capybara. See test_helper.rb for details
gem 'mocha', :require => false
gem 'shoulda'
end

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
7 changes: 7 additions & 0 deletions Rakefile
@@ -0,0 +1,7 @@
#!/usr/bin/env rake
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require File.expand_path('../config/application', __FILE__)

Whenbot::Application.load_tasks
Binary file added app/assets/images/rails.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions app/assets/javascripts/application.js
@@ -0,0 +1,15 @@
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .
3 changes: 3 additions & 0 deletions app/assets/javascripts/tasks.js.coffee
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
3 changes: 3 additions & 0 deletions app/assets/javascripts/whenbot.js.coffee
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
13 changes: 13 additions & 0 deletions app/assets/stylesheets/application.css
@@ -0,0 +1,13 @@
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the top of the
* compiled file, but it's generally better to create a new file per style scope.
*
*= require_self
*= require_tree .
*/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/tasks.css.scss
@@ -0,0 +1,3 @@
// Place all the styles related to the tasks controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/whenbot.css.scss
@@ -0,0 +1,3 @@
// Place all the styles related to the Whenbot controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/controllers/application_controller.rb
@@ -0,0 +1,3 @@
class ApplicationController < ActionController::Base
protect_from_forgery
end
8 changes: 8 additions & 0 deletions app/controllers/tasks_controller.rb
@@ -0,0 +1,8 @@
class TasksController < ApplicationController
def index
end

def new
# ==== One-liner 12 ====
end
end
31 changes: 31 additions & 0 deletions app/controllers/whenbot_controller.rb
@@ -0,0 +1,31 @@
class WhenbotController < ApplicationController

# /whenbot/:channel/:trigger/callback
def callback

# ==== One-liner 13 ====
# ==== One-liner 14 ====

if response[:head_only]
# ==== One-liner 15 ====
else
render response[:type] => response[:body],
:status => response[:status],
:layout => false
end
end

private

def validate_response(response)
# ==== One-liner 16 ====
response[:head_only] ||= response[:body] ? false : true
response[:status] ||= :ok
response[:type] ||= :json
response[:headers] ||= ''
response[:body] ||= 'Success'
# ==== One-liner 17 ====
end


end
2 changes: 2 additions & 0 deletions app/helpers/application_helper.rb
@@ -0,0 +1,2 @@
module ApplicationHelper
end
2 changes: 2 additions & 0 deletions app/helpers/tasks_helper.rb
@@ -0,0 +1,2 @@
module TasksHelper
end
2 changes: 2 additions & 0 deletions app/helpers/whenbot_helper.rb
@@ -0,0 +1,2 @@
module WhenbotHelper
end
Empty file added app/mailers/.gitkeep
Empty file.
Empty file added app/models/.gitkeep
Empty file.
7 changes: 7 additions & 0 deletions app/models/action.rb
@@ -0,0 +1,7 @@
class Action < ActiveRecord::Base
belongs_to :task

serialize :parameters, Hash
serialize :extra_data, Hash
attr_accessible :channel, :action, :parameters, :active
end
4 changes: 4 additions & 0 deletions app/models/authentication.rb
@@ -0,0 +1,4 @@
class Authentication < ActiveRecord::Base
serialize :parameters, Hash
attr_accessible :channel, :parameters
end
21 changes: 21 additions & 0 deletions app/models/task.rb
@@ -0,0 +1,21 @@
class Task < ActiveRecord::Base
# ==== One-liner 18 ====
# ==== One-liner 19 ====

accepts_nested_attributes_for :triggers, :actions

# ==== One-liner 20 ====

#
# Handles calling the appropriate Trigger's #callback method,
# saves the match data to the table, and runs the Action(s) if
# its conditions are met.
#
def self.handle_callback(channel, trigger, params, headers, body)
triggers, trigger_ids = Trigger.triggers_for(channel, trigger)
returned_triggers, response = Whenbot.relay_callback(channel, trigger, triggers, params, headers, body)
# ==== One-liner 21 ====
# ==== One-liner 22 ====
end

end
96 changes: 96 additions & 0 deletions app/models/trigger.rb
@@ -0,0 +1,96 @@
class Trigger < ActiveRecord::Base
# ==== One-liner 23 ====

# ==== One-liner 24 ====
# ==== One-liner 25 ====
# ==== One-liner 26 ====
# ==== One-liner 27 ====
# ==== One-liner 28 ====
# ==== One-liner 29 ====


#
# Returns all active triggers for the requested Channel and Trigger
#
def self.triggers_for(channel, trigger)
records = where("channel = ? AND trigger = ?", channel.to_s.camelize, trigger.to_s.camelize).
select([:id, :channel, :trigger, :parameters, :match_data, :extra_data, :last_matched]).
active
convert_for_matching(records)
end

#
# Saves triggers that either have their :save or
# :matched flag set
def self.save_updated_triggers(triggers, trigger_ids)
save_updated_hash(triggers, trigger_ids)
end

private

#
# Creates an array of hashes to be passed to the Trigger
# via its #callback method. The Trigger will fill in
# the array[n][:match_data] if there's a match, and
# can use array[n][:extra_data] to store data that
# it may need later.
#
# Another array of ids is returned to keep track of
# which records should be updated.
#
# We do this to avoid passing around actual records.
# A Trigger only needs to concern itself with certain
# things, and shouldn't be playing with table data.
#
# Hash Values:
#
# :save => Signals that the hash was updated and
# should be saved
# :matched => New match was found, and stored in
# :match_data. Data will be saved
# even if :save isn't set to true
# :parameters => Parameters saved from the user
# :match_data => Match data that's saved to be
# used by the Action (i.e. the
# data to be used by Liquid)
# :extra_data => Use this to store any data
# you want to receive next time
# :last_matched => Time that this Trigger last
# had a match.
def self.convert_for_matching(records)
array = Array.new
ids_array = Array.new

triggers = Array(records)
triggers.each do |trigger|
hash = Hash.new
hash[:save] = false
hash[:match_found] = false
hash[:parameters] = trigger.parameters || {}
hash[:match_data] = trigger.match_data || {}
hash[:extra_data] = trigger.extra_data || {}
hash[:last_matched] = trigger.last_matched
array << hash
ids_array << trigger.id
end
return array, ids_array
end

def self.save_updated_hash(triggers, trigger_ids)
triggers.each_with_index do |trigger, index|
if trigger[:save] || trigger[:match]
record = Trigger.find(trigger_ids[index])

if trigger[:save]
trigger.each_pair do |key, value|
record.send("#{key}=", value) if record.respond_to? "#{key}=".to_sym
end
elsif trigger[:match_found]
record.match_data = trigger[:match_data]
end

record.save! if record.changed?
end
end
end
end
14 changes: 14 additions & 0 deletions app/views/layouts/application.html.erb
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>Whenbot</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

0 comments on commit 2fee234

Please sign in to comment.