Skip to content

Commit 2fee234

Browse files
committed
Initial commit
1 parent 8aef903 commit 2fee234

File tree

97 files changed

+2394
-66
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+2394
-66
lines changed

Diff for: .gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# See http://help.github.com/ignore-files/ for more about ignoring files.
2+
#
3+
# If you find yourself ignoring temporary files generated by your text editor
4+
# or operating system, you probably want to add a global ignore instead:
5+
# git config --global core.excludesfile ~/.gitignore_global
6+
7+
# Ignore bundler config
8+
/.bundle
9+
10+
# Ignore the default SQLite database.
11+
/db/*.sqlite3
12+
13+
# Ignore all logfiles and tempfiles.
14+
/log/*.log
15+
/tmp
16+
17+
# This code will be Open Source, so don't include files with sensitive data
18+
/config/database.yml
19+
20+
# Ignore .watchr file
21+
/.watchr
22+
23+
# Random files to ignore
24+
/lib/whenbot/old_trigger.rb

Diff for: .rvmrc

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env bash
2+
3+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
4+
# development environment upon cd'ing into the directory
5+
6+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
7+
environment_id="ruby-1.9.3-p125@whenbot"
8+
9+
#
10+
# Uncomment following line if you want options to be set only for given project.
11+
#
12+
# PROJECT_JRUBY_OPTS=( --1.9 )
13+
14+
#
15+
# First we attempt to load the desired environment directly from the environment
16+
# file. This is very fast and efficient compared to running through the entire
17+
# CLI and selector. If you want feedback on which environment was used then
18+
# insert the word 'use' after --create as this triggers verbose mode.
19+
#
20+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
21+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
22+
then
23+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
24+
25+
if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
26+
then
27+
. "${rvm_path:-$HOME/.rvm}/hooks/after_use"
28+
fi
29+
else
30+
# If the environment file has not yet been created, use the RVM CLI to select.
31+
if ! rvm --create "$environment_id"
32+
then
33+
echo "Failed to create RVM environment '${environment_id}'."
34+
exit 1
35+
fi
36+
fi
37+
38+
#
39+
# If you use an RVM gemset file to install a list of gems (*.gems), you can have
40+
# it be automatically loaded. Uncomment the following and adjust the filename if
41+
# necessary.
42+
#
43+
# filename=".gems"
44+
# if [[ -s "$filename" ]]
45+
# then
46+
# rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
47+
# fi
48+
49+
# If you use bundler, this might be useful to you:
50+
# if command -v bundle && [[ -s Gemfile ]]
51+
# then
52+
# bundle install
53+
# fi
54+
55+

Diff for: Gemfile

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'rails', '3.2.3'
4+
5+
# Bundle edge Rails instead:
6+
# gem 'rails', :git => 'git://github.com/rails/rails.git'
7+
8+
gem 'pg'
9+
10+
11+
# Gems used only for assets and not required
12+
# in production environments by default.
13+
group :assets do
14+
gem 'sass-rails', '~> 3.2.3'
15+
gem 'coffee-rails', '~> 3.2.1'
16+
17+
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
18+
gem 'therubyracer', :platform => :ruby
19+
20+
gem 'uglifier', '>= 1.0.3'
21+
end
22+
23+
gem 'jquery-rails'
24+
25+
gem 'heroku'
26+
27+
group :test, :development do
28+
gem 'awesome_print'
29+
gem 'factory_girl_rails'
30+
end
31+
32+
group :development do
33+
#gem 'ruby-debug19', :require => 'ruby-debug' # To use debugger
34+
gem 'rails-footnotes', '>= 3.7'
35+
end
36+
37+
group :test do
38+
gem 'test-unit' # not bundled with ruby 1.9
39+
gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git' # For #has_text?
40+
gem 'database_cleaner' # For Capybara. See test_helper.rb for details
41+
gem 'mocha', :require => false
42+
gem 'shoulda'
43+
end
44+
45+
# To use ActiveModel has_secure_password
46+
# gem 'bcrypt-ruby', '~> 3.0.0'
47+
48+
# To use Jbuilder templates for JSON
49+
# gem 'jbuilder'
50+
51+
# Use unicorn as the app server
52+
# gem 'unicorn'
53+
54+
# Deploy with Capistrano
55+
# gem 'capistrano'
56+
57+
# To use debugger
58+
# gem 'ruby-debug19', :require => 'ruby-debug'

Diff for: Rakefile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env rake
2+
# Add your own tasks in files placed in lib/tasks ending in .rake,
3+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4+
5+
require File.expand_path('../config/application', __FILE__)
6+
7+
Whenbot::Application.load_tasks

Diff for: app/assets/images/rails.png

6.49 KB
Loading

Diff for: app/assets/javascripts/application.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This is a manifest file that'll be compiled into application.js, which will include all the files
2+
// listed below.
3+
//
4+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6+
//
7+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8+
// the compiled file.
9+
//
10+
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11+
// GO AFTER THE REQUIRES BELOW.
12+
//
13+
//= require jquery
14+
//= require jquery_ujs
15+
//= require_tree .

Diff for: app/assets/javascripts/tasks.js.coffee

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

Diff for: app/assets/javascripts/whenbot.js.coffee

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

Diff for: app/assets/stylesheets/application.css

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* This is a manifest file that'll be compiled into application.css, which will include all the files
3+
* listed below.
4+
*
5+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6+
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7+
*
8+
* You're free to add application-wide styles to this file and they'll appear at the top of the
9+
* compiled file, but it's generally better to create a new file per style scope.
10+
*
11+
*= require_self
12+
*= require_tree .
13+
*/

Diff for: app/assets/stylesheets/tasks.css.scss

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the tasks controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/

Diff for: app/assets/stylesheets/whenbot.css.scss

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the Whenbot controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/

Diff for: app/controllers/application_controller.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ApplicationController < ActionController::Base
2+
protect_from_forgery
3+
end

Diff for: app/controllers/tasks_controller.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class TasksController < ApplicationController
2+
def index
3+
end
4+
5+
def new
6+
# ==== One-liner 12 ====
7+
end
8+
end

Diff for: app/controllers/whenbot_controller.rb

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class WhenbotController < ApplicationController
2+
3+
# /whenbot/:channel/:trigger/callback
4+
def callback
5+
6+
# ==== One-liner 13 ====
7+
# ==== One-liner 14 ====
8+
9+
if response[:head_only]
10+
# ==== One-liner 15 ====
11+
else
12+
render response[:type] => response[:body],
13+
:status => response[:status],
14+
:layout => false
15+
end
16+
end
17+
18+
private
19+
20+
def validate_response(response)
21+
# ==== One-liner 16 ====
22+
response[:head_only] ||= response[:body] ? false : true
23+
response[:status] ||= :ok
24+
response[:type] ||= :json
25+
response[:headers] ||= ''
26+
response[:body] ||= 'Success'
27+
# ==== One-liner 17 ====
28+
end
29+
30+
31+
end

Diff for: app/helpers/application_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module ApplicationHelper
2+
end

Diff for: app/helpers/tasks_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module TasksHelper
2+
end

Diff for: app/helpers/whenbot_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module WhenbotHelper
2+
end

Diff for: app/mailers/.gitkeep

Whitespace-only changes.

Diff for: app/models/.gitkeep

Whitespace-only changes.

Diff for: app/models/action.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Action < ActiveRecord::Base
2+
belongs_to :task
3+
4+
serialize :parameters, Hash
5+
serialize :extra_data, Hash
6+
attr_accessible :channel, :action, :parameters, :active
7+
end

Diff for: app/models/authentication.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Authentication < ActiveRecord::Base
2+
serialize :parameters, Hash
3+
attr_accessible :channel, :parameters
4+
end

Diff for: app/models/task.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Task < ActiveRecord::Base
2+
# ==== One-liner 18 ====
3+
# ==== One-liner 19 ====
4+
5+
accepts_nested_attributes_for :triggers, :actions
6+
7+
# ==== One-liner 20 ====
8+
9+
#
10+
# Handles calling the appropriate Trigger's #callback method,
11+
# saves the match data to the table, and runs the Action(s) if
12+
# its conditions are met.
13+
#
14+
def self.handle_callback(channel, trigger, params, headers, body)
15+
triggers, trigger_ids = Trigger.triggers_for(channel, trigger)
16+
returned_triggers, response = Whenbot.relay_callback(channel, trigger, triggers, params, headers, body)
17+
# ==== One-liner 21 ====
18+
# ==== One-liner 22 ====
19+
end
20+
21+
end

Diff for: app/models/trigger.rb

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
class Trigger < ActiveRecord::Base
2+
# ==== One-liner 23 ====
3+
4+
# ==== One-liner 24 ====
5+
# ==== One-liner 25 ====
6+
# ==== One-liner 26 ====
7+
# ==== One-liner 27 ====
8+
# ==== One-liner 28 ====
9+
# ==== One-liner 29 ====
10+
11+
12+
#
13+
# Returns all active triggers for the requested Channel and Trigger
14+
#
15+
def self.triggers_for(channel, trigger)
16+
records = where("channel = ? AND trigger = ?", channel.to_s.camelize, trigger.to_s.camelize).
17+
select([:id, :channel, :trigger, :parameters, :match_data, :extra_data, :last_matched]).
18+
active
19+
convert_for_matching(records)
20+
end
21+
22+
#
23+
# Saves triggers that either have their :save or
24+
# :matched flag set
25+
def self.save_updated_triggers(triggers, trigger_ids)
26+
save_updated_hash(triggers, trigger_ids)
27+
end
28+
29+
private
30+
31+
#
32+
# Creates an array of hashes to be passed to the Trigger
33+
# via its #callback method. The Trigger will fill in
34+
# the array[n][:match_data] if there's a match, and
35+
# can use array[n][:extra_data] to store data that
36+
# it may need later.
37+
#
38+
# Another array of ids is returned to keep track of
39+
# which records should be updated.
40+
#
41+
# We do this to avoid passing around actual records.
42+
# A Trigger only needs to concern itself with certain
43+
# things, and shouldn't be playing with table data.
44+
#
45+
# Hash Values:
46+
#
47+
# :save => Signals that the hash was updated and
48+
# should be saved
49+
# :matched => New match was found, and stored in
50+
# :match_data. Data will be saved
51+
# even if :save isn't set to true
52+
# :parameters => Parameters saved from the user
53+
# :match_data => Match data that's saved to be
54+
# used by the Action (i.e. the
55+
# data to be used by Liquid)
56+
# :extra_data => Use this to store any data
57+
# you want to receive next time
58+
# :last_matched => Time that this Trigger last
59+
# had a match.
60+
def self.convert_for_matching(records)
61+
array = Array.new
62+
ids_array = Array.new
63+
64+
triggers = Array(records)
65+
triggers.each do |trigger|
66+
hash = Hash.new
67+
hash[:save] = false
68+
hash[:match_found] = false
69+
hash[:parameters] = trigger.parameters || {}
70+
hash[:match_data] = trigger.match_data || {}
71+
hash[:extra_data] = trigger.extra_data || {}
72+
hash[:last_matched] = trigger.last_matched
73+
array << hash
74+
ids_array << trigger.id
75+
end
76+
return array, ids_array
77+
end
78+
79+
def self.save_updated_hash(triggers, trigger_ids)
80+
triggers.each_with_index do |trigger, index|
81+
if trigger[:save] || trigger[:match]
82+
record = Trigger.find(trigger_ids[index])
83+
84+
if trigger[:save]
85+
trigger.each_pair do |key, value|
86+
record.send("#{key}=", value) if record.respond_to? "#{key}=".to_sym
87+
end
88+
elsif trigger[:match_found]
89+
record.match_data = trigger[:match_data]
90+
end
91+
92+
record.save! if record.changed?
93+
end
94+
end
95+
end
96+
end

Diff for: app/views/layouts/application.html.erb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Whenbot</title>
5+
<%= stylesheet_link_tag "application", :media => "all" %>
6+
<%= javascript_include_tag "application" %>
7+
<%= csrf_meta_tags %>
8+
</head>
9+
<body>
10+
11+
<%= yield %>
12+
13+
</body>
14+
</html>

0 commit comments

Comments
 (0)