How to use the GitHub post-receive JSON API with Rails
Posted by Lennart | Filed under Ruby On Rails
GitHub offers you a lot of post-receive notifications. This means that some actions are made whenever you commit something. You can e.g. send notifications via Jabber or IRC - Another option is to call a URL with JSON data. This Rails snippet receives the JSON data and stores relevant information in your database:
class TunerController < ApplicationController
# Basic HTTP authentication.
USER, PASSWORD = "github", "secret"
before_filter :authenticate
# Disable need of authenticity token.
skip_before_filter :verify_authenticity_token
def index
# Include JSON. (gem install json)
require 'json'
# Check if the JSON request is in correct format.
if params[:payload].blank?
# Wrong format. Exit.
render :text => "no payload"
return
end
# Parse the JSON request and store resulting hash.
push = JSON.parse(params[:payload])
# Get the "commits" part.
commits = push["commits"]
# Check if there were commits. Yes, there should be some...
if commits.blank?
# No commits found. Strange - Exit!
render :text => "no commits found"
return
end
# Store the interesting information of the last commit-
last_commit_message = commits.last["message"]
last_commit_timestamp = commits.last["timestamp"]
last_commit_author = commits.last["author"]["name"]
# Create a new object to save in databse.
data = Git_Message.new do |d|
d.last_commit_message = last_commit_message
d.last_commit_timestamp = last_commit_timestamp
d.last_commit_author = last_commit_author
end
# Save our commit data in the database!
if !data.save
# Could not save.
render :text => "could not store in database"
return
end
# Everything went well.
render :text => "done"
end
private
def authenticate
authenticate_or_request_with_http_basic "nothing to see here"
do |id, password|
id == USER && password == PASSWORD
end
end
end
Set the GitHub post-receive URL to something like http://github:secret@example.org/tuner - Where “github” is the user and “secret” the password. You can alternatively just enter the URL and remove the authentication methods from the Rails controller.
Tags: api, authentication, basic, commit, github, http, information, json, post-receive, rails, url
How to add a value to a select field in Ruby On Rails
Posted by Lennart | Filed under Hacking, Ruby On Rails
I’ll just leave this here: When you fill a select box directly from a array returned by .find(:all) you can add more values tothe select box this way:
Controller:
@hosts = Host.find(:all).collect {|p| [p.name, p.id] }
@hosts << ["None", nil]
View:
<dd>
<%= f.select :linkedhost, @hosts %>
<%= error_message_on(:service, :linkedhost) %>
</dd>
Tags: add, array, find, model, standard, value
Rails Migrations: How to define an own column as primary key
Posted by Lennart | Filed under Ruby On Rails
Usually the Ruby On Rails convention to use ID as the primary key of tables is fine. But sometimes you want to use another column as primary key. In my case I wanted to use the column “pid” of the ScopePort table “health” as the primary key. This is the way to do it:
def self.up
create_table :health, :primary_key => :pid do |t|
t.integer :threads, :timestamp
t.boolean :clienthandler
t.string :vmem, :packetsOK, :packetsERR
end
end
The first notable point is the create_table option “:primary_key => :pid”. It instructs rake not to create the ID column but to use the column “pid” as primary key. You don’t need to define :pid later - It will be created automatically as an integer.
Tags: column, database, migrations, primary key, primary_key