Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ejdraper committed Dec 22, 2010
0 parents commit 1060820
Show file tree
Hide file tree
Showing 52 changed files with 10,146 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
@@ -0,0 +1,10 @@
.bundle/
log/*.log
pkg/
test/dummy/db/*.sqlite3
test/dummy/log/*.log
test/dummy/tmp/
*.swp*
*.swo*
*.swn*
*.un*
9 changes: 9 additions & 0 deletions Gemfile
@@ -0,0 +1,9 @@
source "http://rubygems.org"

gem "rails", "3.0.3"
gem "capybara", ">= 0.4.0"
gem "sqlite3-ruby", :require => "sqlite3"

# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
# gem 'ruby-debug'
# gem 'ruby-debug19'
99 changes: 99 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,99 @@
GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
actionmailer (3.0.3)
actionpack (= 3.0.3)
mail (~> 2.2.9)
actionpack (3.0.3)
activemodel (= 3.0.3)
activesupport (= 3.0.3)
builder (~> 2.1.2)
erubis (~> 2.6.6)
i18n (~> 0.4)
rack (~> 1.2.1)
rack-mount (~> 0.6.13)
rack-test (~> 0.5.6)
tzinfo (~> 0.3.23)
activemodel (3.0.3)
activesupport (= 3.0.3)
builder (~> 2.1.2)
i18n (~> 0.4)
activerecord (3.0.3)
activemodel (= 3.0.3)
activesupport (= 3.0.3)
arel (~> 2.0.2)
tzinfo (~> 0.3.23)
activeresource (3.0.3)
activemodel (= 3.0.3)
activesupport (= 3.0.3)
activesupport (3.0.3)
arel (2.0.6)
builder (2.1.2)
capybara (0.4.0)
celerity (>= 0.7.9)
culerity (>= 0.2.4)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
selenium-webdriver (>= 0.0.27)
xpath (~> 0.1.2)
celerity (0.8.6)
childprocess (0.1.6)
ffi (~> 0.6.3)
culerity (0.2.13)
erubis (2.6.6)
abstract (>= 1.0.0)
ffi (0.6.3)
rake (>= 0.8.7)
i18n (0.5.0)
json_pure (1.4.6)
mail (2.2.12)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
nokogiri (1.4.4)
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
rack (>= 1.0.0)
rack-test (0.5.6)
rack (>= 1.0)
rails (3.0.3)
actionmailer (= 3.0.3)
actionpack (= 3.0.3)
activerecord (= 3.0.3)
activeresource (= 3.0.3)
activesupport (= 3.0.3)
bundler (~> 1.0)
railties (= 3.0.3)
railties (3.0.3)
actionpack (= 3.0.3)
activesupport (= 3.0.3)
rake (>= 0.8.7)
thor (~> 0.14.4)
rake (0.8.7)
rubyzip (0.9.4)
selenium-webdriver (0.1.2)
childprocess (~> 0.1.5)
ffi (~> 0.6.3)
json_pure
rubyzip
sqlite3-ruby (1.3.2)
thor (0.14.6)
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.23)
xpath (0.1.2)
nokogiri (~> 1.3)

PLATFORMS
ruby

DEPENDENCIES
capybara (>= 0.4.0)
rails (= 3.0.3)
sqlite3-ruby
20 changes: 20 additions & 0 deletions MIT-LICENSE
@@ -0,0 +1,20 @@
Copyright 2010 Elliott Draper <el@ejdraper.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
108 changes: 108 additions & 0 deletions README.rdoc
@@ -0,0 +1,108 @@
= Chameleon

* http://github.com/ejdraper/chameleon

== DESCRIPTION:

This is a Rails engine designed to make it super easy to expose data from your application as widgets for use with Geckoboard (http://geckoboard.com).

This only works for Rails 3 apps.

== FEATURES:

This provides a generator to build widgets using a basic DSL syntax to describe the data to be exposed, and the settings for it to be exposed under, and also includes the controller/routing necessary to provide an endpoint for Geckoboard to retrieve the data to display on your dashboard.

Currently it supports just the "number and optional secondary stat" widget type, with more widget types to follow shortly.

== SYNOPSIS:

Simply add to your Gemfile as follows:

gem 'chameleon'

Then run "bundle install" and once installed, you can run:

rails g chameleon:widget NAME TYPE

to generate a widget within "app/widgets". For example, if you want to generate a widget that exposes the amount of users you have in your app currently:

rails g chameleon:widget users number_and_secondary

This will generate something similar to the following in "app/widgets/users_widget.rb":

widget :users do
key "3638c90ec12d5a59061ad7b78afcbd050e50b621"
type "number_and_secondary"
data do
end
end

To implement your widget, you simply need to alter it to return the required data - for a "number_and_secondary" type of widget, it requires an array containing two values, the first being the current value, and the last being the value to compare it to (such as user count as of yesterday, for example):

widget :users do
key "3618c90ec02d5a57061ad7b78afcbb050e50b608"
type "number_and_secondary"
data do
[User.count, User.count(:conditions => "created_at < '#{1.day.ago.to_s(:db)}'")]
end
end

You can then point Geckoboard at your widget, http://myapp/widgets/users?key=3618c90ec02d5a57061ad7b78afcbb050e50b608, and Geckoboard will then show the current user count, as well as the percentage difference compared to a day ago. You can optionally just return a single value too, if you don't want the secondary stat.

By default the widgets are secured by the key which is autogenerated within the widget - this has to be passed in via the querystring for the widget to function, as seen above. You can change this value to anything you consider secure, and if you require a parameter other than :key to be used for this security check, you can define the parameter as such:

widget :users do
key "my_updated_key"
key_parameter :token
type "number_and_secondary"
data do
[User.count, User.count(:conditions => "created_at < '#{1.day.ago.to_s(:db)}'")]
end
end

This will now expect the key to be passed in as the :token parameter, for example http://myapp/widgets/users?token=my_updated_key. For data that is acceptable to be publically exposed, you can exclude the key and set the widget as public as follows:

widget :users do
public true
type "number_and_secondary"
data do
[User.count, User.count(:conditions => "created_at < '#{1.day.ago.to_s(:db)}'")]
end
end

This will now be accessible simply at http://myapp/widgets/users.

Full documentation on the different widget types and data will follow once the other widget types are implemented, but this basic syntax should provide an easy way to expose data from your Rails 3 application to Geckoboard quickly and easily.

== REQUIREMENTS:

A Rails 3 application.

== INSTALL:

It's best to add it to the Gemfile for your Rails 3 app, but if needed you can install the gem manually:

gem install chameleon

== LICENSE:

Copyright 2010 Elliott Draper <el@ejdraper.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 changes: 30 additions & 0 deletions Rakefile
@@ -0,0 +1,30 @@
# encoding: UTF-8
require 'rubygems'
begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end

require 'rake'
require 'rake/rdoctask'

require 'rake/testtask'

Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
end

task :default => :test

Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'Chameleon'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README.rdoc')
rdoc.rdoc_files.include('app/**/*.rb')
rdoc.rdoc_files.include('lib/**/*.rb')
end
20 changes: 20 additions & 0 deletions app/controllers/widgets_controller.rb
@@ -0,0 +1,20 @@
class WidgetsController < ApplicationController
before_filter :find_widget
before_filter :validate_key
skip_before_filter :verify_authenticity_token

def show
@data = @widget.data.call
render "#{@widget.type}.xml"
end

protected
def find_widget
@widget = Widget.find(params[:id].gsub(".xml", ""))
raise "Invalid widget!" if @widget.nil?
end

def validate_key
raise "Invalid key!" if !@widget.public && (params[@widget.key_parameter].blank? || params[@widget.key_parameter] != @widget.key)
end
end
47 changes: 47 additions & 0 deletions app/models/widget.rb
@@ -0,0 +1,47 @@
class Widget
cattr_accessor :widgets

def self.widget(name, &block)
@@widgets ||= {}
@@widgets[name] = Widget.new(name, &block)
end

def self.find(name)
Dir.glob(File.join("app", "widgets", "*.rb")).each { |f| load File.expand_path(f) } if Rails.env == "development"
(@@widgets || {})[name.to_sym]
end

def initialize(name, &block)
@name = name
instance_eval(&block)
end

def name
@name
end

def key(value = nil)
@key = value unless value.nil?
@key
end

def key_parameter(value = nil)
@key_parameter = value unless value.nil?
@key_parameter || :key
end

def type(value = nil)
@type = value unless value.nil?
@type
end

def public(value = nil)
@public = value unless value.nil?
@public || false
end

def data(&block)
@data = block if block_given?
@data
end
end
11 changes: 11 additions & 0 deletions app/views/widgets/number_and_secondary.xml.erb
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<value><%= @data.is_a?(Array) ? @data.first : @data %></value>
<text></text>
</item>
<item>
<value><%= @data.is_a?(Array) && @data.length == 2 ? @data.last : nil %></value>
<text></text>
</item>
</root>
7 changes: 7 additions & 0 deletions chameleon.gemspec
@@ -0,0 +1,7 @@
Gem::Specification.new do |s|
s.name = "chameleon"
s.summary = "Rails engine to let you easily build Geckoboard widgets"
s.description = "Rails engine to let you easily expose data from your app as Geckoboard widgets"
s.files = Dir["app/**/*"] + Dir["lib/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"]
s.version = "0.1.0"
end
3 changes: 3 additions & 0 deletions config/routes.rb
@@ -0,0 +1,3 @@
Rails::Application.routes.draw do
match "widgets/:id", :to => "widgets#show"
end
14 changes: 14 additions & 0 deletions lib/chameleon.rb
@@ -0,0 +1,14 @@
module Chameleon
class Engine < Rails::Engine
engine_name :chameleon
initializer "chameleon.initialization" do
Dir.glob(File.join("app", "widgets", "*.rb")).each { |f| require File.expand_path(f) }
end
end
end

Kernel.class_eval do
def widget(name, &block)
Widget.widget(name, &block)
end
end

0 comments on commit 1060820

Please sign in to comment.