Twitter Updates

Saturday, 19 December 2009

Sinatra Second Blog attempt

Example of how to setup a basic Sinatra web application.

This Sinatra app uses erb for the templating and active record for the database ORM. To get the cod eto show up I have had to change "< a" to <-a tags and insert extra space in other tags, unfortunately this means they are not quite cut and paste. To simplify The setup I have created a ./create_db.rb file.

./create_db.rb
require 'rubygems'
require 'sequel'

# Connect to the database
DB = Sequel.sqlite('./db/blog.db')

unless DB.table_exists? :posts
DB.create_table :posts do
primary_key :id
varchar :title
text :body
end

# populate the table
DB[:posts].insert(:title => 'Post Man Pat', :body => "has a black and white cat" )
DB[:posts].insert(:title => 'Sandwiches', :body => "Peanut butter and Marmite, Yum")
DB[:posts].insert(:title => 'Leaves', :body => "Grow on trees")
end

class Posts < Sequel::Model
end


The Main Application. ./app.rb
require 'rubygems'
require 'sinatra'
require 'active_record'

#Load extra functions
require 'url_logic'

ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => './db/blog.db'
)

class Post < ActiveRecord::Base
end

$blogpath = "article"

get '/' do
erb :index
end

#Setup paths for blog
get '/article/?' do
@posts = Post.all()
erb :readallblog
end

#view a post
get '/article/:id/?' do
@posts = Post.find(params[:id])
erb :showoneblog
end

#edit a post
get '/article/:id/edit/?' do |id|
@posts = Post.find(params[:id])
erb :editblog
end

#Update post
put '/article/:id/?' do
@posts = Post.find(params[:id])
@posts.title = params['post']['title']
@posts.body = params['post']['body']
@posts.save
erb :showoneblog
end


#create a Post
post '/article/?' do
Post.create(
:title =>params['post']['title'] ,
:body => params['post']['body']
)
@posts = Post.all()
erb :readallblog
end

#delete an article
delete '/article/?' do
@posts = Post.find(params['post']['id'])
@posts.delete
@posts = Post.all()
erb :readblog
end


./views/readallblog.erb
readBlog All Posts
<%# $blogpath is global set in the main controller%>
<% for post in @posts %>
< h 1><-a href="/<%=$blogpath%>/<%=post.id%>"><%=post.id%>:<%= post.title %>< / a>
<%=post.body%>

<-a href="/<%=$blogpath%>/<%=post.id%>/edit">edit< / a>
<% end %>

< f orm method="post" action="/<%=$blogpath%>/">
< i nput name="_method" type="hidden" value="post" />
< d iv>
Title< i nput type="text" name="post[title]" value="title"/>

Body< i nput type="text" name="post[body]" value="body"/>

< b utton type="submit">Create
< / d iv>
< / f orm>

./views/showoneblog.erb
Show Single Post

< h 1><%=@posts.id%>:<%= @posts.title %>< / h 1>
< p ><%= @posts.body%>

<-a href="/<%=$blogpath%>/<%=@posts.id%>/edit">edit< / a>



./views/blog
Edit Single Post

< h 1><%=@posts.id%>:<%= @posts.title %>< / h 1>
<%=@posts.body%>

< f orm method="post" action="/<%=$blogpath%>/<%=@posts.id%>/">
< i nput name="_method" type="hidden" value="put" />
< d iv>
Title< i nput type="text" name="post[title]" value="<%=@posts.title%>"/>

Body< i nput type="text" name="post[body]" value="<%=@posts.body%>"/>

< b utton type="submit">Update
< / div>
< / f orm>


Hopefully this has covered creating new, editing and retrieving database entries using ActiveRecord. I have probably missed some of the tricks to simplify writing Sinatra apps.

Tuesday, 15 December 2009

Sinatra First Blog attempt

After getting Xcode for Snow leopard correctly installed and restarting. The basic sinatra works correctly.

hi.rb
----
require 'rubygems'
require 'sinatra'
get '/hi' do
"Hello World!"
end

Command line:
$ gem install sinatra
$ ruby hi.rb

Then checkout 127.0.0.1:4567.

Turning it an almost blog, with some erb and an sqlite 3 database.
Assuming you have macports installed you can do (or look up installing sqlite3):
$ sudo port install sqlite3
cd Sinatra_dir
sqlite3 blog
#you are now in the sqlite command prompt
sqlite> create table posts (title text, body text );
sqlite> insert into posts values('title1','Very long article');
sqlite> insert into posts values('Sinatra Blog Post','text text text text text');

hi.rb
----
require 'rubygems'
require 'sinatra'
require 'active_record'

ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:dbfile => 'blog.db'
)
class Post < ActiveRecord::Base
end

get '/news/?' do
@posts = Post.all()
erb :index
end


Now create ./views/index.erb
----
HelloWorld!

<% for post in @posts %>
< h 1><%= post.title %>< / h 1>
< p ><%= post.body%>< / p>
<% end %>


$ ruby hi.rb and goto 127.0.0.1:4567.
If all goes well you should see some thing like this:

Monday, 14 December 2009

whereis alternative to which

whereis is a nice alternative to which for finding the location of unix programs.

$ which ruby
> /usr/local/bin/ruby

$ whereis ruby
> /usr/bin/ruby

Problems with Snow Leopard and Ruby Gems

UPDATE 2 (update 1 at the bottom with some extra ruby version info)
When you install Xcode make sure is is the latest version. I must have at some point installed the old Leopard version. then when things did not work reinstalled that same old version. I found xcode321_10m2003 in my downloads folder not the older one I had put in a software folder. You may still need to update Ruby to a new version.

Make sure you have the latest Xcode.


I have been trying to get the very simple Sinatra (a light ruby on Rails) setup working following the verysimple instructions on http://www.sinatrarb.com/

Make a file called hi.rb with this inside:
require 'rubygems'
require 'sinatra'
get '/hi' do
"Hello World!"
end

Then run these commands:
$ gems install sinatra
$ ruby hi.rb


They fail with:
hi.rb:2:in `require': no such file to load -- rubygems (LoadError)
from hi.rb:2

After removing the require 'rubygems', is it actually still required ?
hi.rb:3:in `require': no such file to load -- sinatra (LoadError)
from hi.rb:3

To fix this I ran through the instructions at http://hivelogic.com/articles/compiling-ruby-rubygems-and-rails-on-snow-leopard/

Their instructions worked well until the mysql gem which I do not require at the moment but do like to get my set up the same as the tutorial. I found the following line on stackoverflow.
$ sudo env ARCHFLAGS="-arch x86_64" gem install mysqlplus


Finally got the basic sinatra working by calling:
$ /usr/local/bin/ruby hi.rb
Even though which ruby returns /usr/local/bin/ruby, so why does ruby hi.rb not just work!

What is going on just thought I would check to see if calling the other ruby breaks it, but it works fine.
$ /usr/bin/ruby hi.rb


UPDATE
For completeness and to answer Spyro7's Question:
$ /usr/local/bin/ruby -v
> ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin10.2.0]

$ /usr/bin/ruby -v
> ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]

Sunday, 13 December 2009

Rediscovering Airfoil

Apple produces quite a cheap way to stream audio into another room/Hifi all you need is an airport express plug it into an amp or a powered set of speakers then iTunes can transmit over wifi to these external speakers.

There is a nice piece of software called airfoil by rogueamoeba which is available for $25 (25 us dollars). It allows you to stream certain application or all audio to these external speakers. You can also select which speakers to transmit to.

There is a new application which I have not seen before, Airfoil Speakers this allows a Mac or Windows PC to be turned in to a receiving Airport express!

There is also an Airfoil Speakers for the iPod Touch! So you get wireless headphones capability

Thursday, 10 December 2009

Mac OS X NTFS Drivers speed test

I have had to transfer some data from one USB NTFS formatted drive to another USB NTFS formatted drive. These are the results using the different Mac NTFS drivers available.

NTFS-3G (With MAC FUSE) Caching disabled:


NTFS-3G with caching enabled:


Tuxera NTFS for Mac (25 Euro) (based on NTFS-3G) Caching enabled:


Also missing, For comparison HFS+ test:







NTFS-3G (No Caching) 183 MB/Minute
NTFS-3G (Cached) 610 MB/Minute
TUXERA (Cached) to HFS 1000 MB/Minute
TUXERA (Cached) 1223 MB/Minute
NTFS-3G (Cached) to HFS 1391 MB/Minutes
HFS+ SATA to USB 1464 MB/Minures

Wednesday, 9 December 2009

Mac OX Speed test your (USB) Hard Drive

Just tried xbench to speed test my harddrives, very surprised that it still works as it looks like the last time it got any updates was for OS 10.3 a few years ago.


http://www.xbench.com/


The Results from my main Hard Drive Mac OS X:


Aaargh, it refuses to test USB drives the only reason I was trying it out!

Monday, 7 December 2009

201st Post: Finally fixed wrapping text in pre tags

pre tags are used in html to maintain the display of whitespace as it is written in the html. This lends itself nicely to code snippets but unfortunately means that it does not wrap text nicely by default.

By adding this [1] to the embedded or external style sheets you can get text to behave nicely inside pre tags.

[1]
pre {
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}


Original source:
http://labnol.blogspot.com/2006/10/html-css-trick-for-displaying-code.html

Saturday, 5 December 2009

Snow Leopard Get Summary

OS X has a very use shortcut for getting info on files which is cmd-i or the 'apple key' and 'i' will bring up an info window. If you select more than 1 file you get an info window for each! probably not what you wanted.

On OS X 10.6 if you hold down the control 'ctrl' key as well as cmd-i (ctrl-cmd-i) then you get a neat summary of those files. Very useful for getting the disk spaced used by multiple files.

if only one file is selected ctrl-cmd-i and cmd-i perform the same function, why is ctrl-cmd-i not the default!

If you would like to have a multiple file summary by default try this:
Open your system preference is your usual way, I use quick silver so just pres ctrl-space type sys and enter.

Go to the Keyboard item. Key board shortcuts then press then plus in the bottom left of the main panel.

Add a shortcut for application finder. command is 'Get Summary Info' then in the keyboard command type the shortcut you want, in this case cmd-i.


NB: You may also need to uncheck the Show Info option in Keyboard shortcuts->Service->Files and Folders->Show Info.

Google Tasks

Just discovered that google have added a Task list function into gmail.

http://mail.google.com/mail/help/tasks/

This will probably become my main todo list. I do not think that it will replace all the notes I am keeping with Evernote though (Evernote has a very functional free mode).

Windows XP Picture Screen Saver

Windows XP comes with a slideshow screensaver which can be pointed at a folder and displays the picture one at a time. For me it refused to display most of my pictures out of about 2000 only 12 would be shown.

On my search to fix this I was unable to find the spec for the photos allowed through the Windows screen saver, I did however discover that google has a screensaver that comes packaged with picasa. This also has and pan & Zoom effect just like the mac screen saver.

Get from :
http://pack.google.com/screensaver.html

All you actually need is Picasa, so you can uncheck every thing else if you want.
Once installed you do not have set picasa up just choose the new screensaver from the standard place.