<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Notes on ScopePort &#187; api</title>
	<atom:link href="http://blog.scopeport.org/tag/api/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.scopeport.org</link>
	<description>The ScopePort Blog</description>
	<lastBuildDate>Sun, 07 Feb 2010 01:08:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to use the GitHub post-receive JSON API with Rails</title>
		<link>http://blog.scopeport.org/ruby-on-rails/how-to-use-the-github-post-receive-json-api-rails/</link>
		<comments>http://blog.scopeport.org/ruby-on-rails/how-to-use-the-github-post-receive-json-api-rails/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 15:08:02 +0000</pubDate>
		<dc:creator>Lennart</dc:creator>
				<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[basic]]></category>
		<category><![CDATA[commit]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[information]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[post-receive]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://blog.scopeport.org/?p=131</guid>
		<description><![CDATA[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 &#8211; 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 < [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.github.com">GitHub</a> 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 &#8211; Another option is to call a URL with <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> data. This <a href="http://www.rubyonrails.com">Rails</a> snippet receives the <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> data and stores relevant information in your database:</p>
<pre>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 &#038;&#038; password == PASSWORD
		end
	end

end
</pre>
<p>Set the <a href="http://www.github.com">GitHub</a> post-receive URL to something like http://github:secret@example.org/tuner &#8211; Where &#8220;github&#8221; is the user and &#8220;secret&#8221; the password. You can alternatively just enter the URL and remove the authentication methods from the <a href="http://www.rubyonrails.com">Rails</a> controller.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.scopeport.org/ruby-on-rails/how-to-use-the-github-post-receive-json-api-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
