Skip to content

Instantly share code, notes, and snippets.

💙
Coding Go

Peter Hellberg peterhellberg

💙
Coding Go
Block or report user

Report or block peterhellberg

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@peterhellberg
peterhellberg / textarea.rb
Created Feb 27, 2012
Simple redirect in Sinatra
View textarea.rb
require 'sinatra'
require 'uri'
helpers do
include Rack::Utils
end
get '/' do
<<HTML
<form action="/" method="post">
View 01_echo_data.rb
require 'sinatra'
require 'json'
post '/' do
JSON.parse(params[:data]).inspect
end
@peterhellberg
peterhellberg / test_blog_posts.rb
Created Mar 8, 2012
Check if all the old blog posts has been transferred to athega.se
View test_blog_posts.rb
require "net/http"
remote_cmd = 'cat /tmp/2012-03-05-blogg.athega.se.sql | grep -E "^20../"'
slugs = `ssh athega.se #{remote_cmd}`.split("\n")
req = Net::HTTP.new('athega.se', 80)
slugs.each do |slug|
response = req.request_head("/blogg/#{slug}")
puts (response.code != "200") ? "Missing: #{slug}" : "OK: #{slug}"
@peterhellberg
peterhellberg / yajl_weirdness_spec.rb
Created Mar 14, 2012
Some (maybe) unexpected return values from yajl-ruby (0.8.3)
View yajl_weirdness_spec.rb
require 'yajl'
require 'minitest/spec'
require 'minitest/autorun'
describe Yajl do
def parse(*args)
Yajl::Parser.parse(*args)
end
@peterhellberg
peterhellberg / gfm.rb
Created Mar 18, 2012
GitHub Flavored Markdown parser for use with Jekyll and Marked.app
View gfm.rb
#!/usr/bin/env ruby
require 'rubygems'
require 'redcarpet'
require 'pygments.rb'
class HTMLwithPygments < Redcarpet::Render::HTML
def block_code(code, language)
Pygments.highlight(code, :lexer => language.to_sym, :options => {
:encoding => 'utf-8'
})
@peterhellberg
peterhellberg / multi_value.rb
Created Mar 18, 2012
Support for multiple return values in Ruby 1.9, the way Common Lisp does it… sort of
View multi_value.rb
class MultiValue < BasicObject
attr_reader :secondary
def initialize(obj, *secondary)
@obj, @secondary = obj, secondary
end
def method_missing(sym, *args, &block)
@obj.__send__(sym, *args, &block)
end
@peterhellberg
peterhellberg / jquery-querystring.js
Created Apr 5, 2012
Using jQuery to get query string values
View jquery-querystring.js
(function($) {
$.QueryString = (function(a) {
if (a == "") return {};
var b = {};
for (var i = 0; i < a.length; ++i)
{
var p=a[i].split('=');
if (p.length != 2) continue;
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
@peterhellberg
peterhellberg / Gemfile
Created Apr 10, 2012
Sinatra acceptance testing, using minitest/spec and capybara-webkit
View Gemfile
source :rubygems
gem "sinatra", "~> 1.3.2"
group :test do
gem "minitest", "~> 2.10"
gem "rack-test", "~> 0.6.1"
gem "capybara", "~> 1.1"
gem "capybara-webkit", "~> 0.11"
gem "capybara_minitest_spec", "~> 0.2"
@peterhellberg
peterhellberg / week_number.rb
Created Apr 13, 2012
Testing the WeekNumber class
View week_number.rb
# encoding: utf-8
class WeekNumber
attr_reader :cweek, :year
def initialize(cweek = Date.today.cweek, year = Date.today.year)
@year = year.to_i
@cweek = cweek.to_i
if @cweek > WeekNumber.weeks_in(@year)
@peterhellberg
peterhellberg / image_dimensions.rb
Created May 3, 2012
Check image dimensions using the Dimensions gem.
View image_dimensions.rb
require 'json'
class ImageDimensions < Sinatra::Base
get "/" do
'API: GET /[ur_product_id].json'
end
get /(\d{6})\.json/ do |ur_product_id|
content_type :json
data = { exists: false }
You can’t perform that action at this time.