Skip to content

Instantly share code, notes, and snippets.

View Achillefs's full-sized avatar

Achilles Charmpilas Achillefs

View GitHub Profile
module ApplicationHelper
# Your own stuff
def ajax_actions opts = {}
container = opts.delete(:container) || "actions_container"
actions_selector = opts.delete(:actions_selector) || "td.actions a"
raise ArgumentError.new("Please specify a container element identifier") if container.empty? or container.nil?
raise ArgumentError.new("The action links selector cannot be empty") if actions_selector.empty? or actions_selector.nil?
html = %[<div id="#{container}"></div>\n]
html << javascript_tag(%[
#!/usr/bin/env bash
echo "Creating and downloading live database image..."
ssh username@server.com 'mysqldump -u username -ppassword dbname | bzip2 > image.sql.bz'
scp username@server.com:./image.sql.bz ~/Desktop/
ssh username@server.com 'rm -rf image.sql.bz'
echo "Done. Importing database..."
bunzip2 ~/Desktop/image.sql.bz
mysql -u username local_dbname < ~/Desktop/image.sql
rm -rf ~/Desktop/image.sql
echo "Done. Database update complete."
# sudo gem install mechanize ratom --no-ri --no-rdoc
%W{rubygems mechanize atom/pub net/http}.each { |r| require r }
module Wordpress
# A proof of concept class, displaying how to manage a WP blog through ruby
class Blog
attr_accessor :agent, :blog_uri, :username, :password, :logged_in
def initialize blog_uri, username, password
@username = username
@password = password
@blog_uri = blog_uri.gsub(/\/$/,"") # remove last slash if given
<?php
class Phrase {
public static function to_link($content){
$phrases = DbFinder::from("Phrase")->find(); # This was for a symfony site
foreach ($phrases as $p) { # Interestingly, foreach is much faster than a for loop
$text = preg_quote($p->getText()); # Sanitize the text
$content = preg_replace("/(?!=(?:<a [^>]*>))({$text})(?!(?:<\/a>))/si","<a href=\"{$p->getUrl()}\">\\1</a>",$content,1);
}
return $content;
}
@Achillefs
Achillefs / add_newlines_to_xml.rb
Created October 18, 2010 11:29
Add newlines to long ass XML files
Dir[File.join(%w[test fixtures files *.xml])].each do |file|
xml = File.read(file)
matches = xml.scan("\n")
if !matches or matches.size < 3
puts "#{file}: Not enough line brakes, adding them now..."
File.open(file,"w") do |f|
f.puts(xml.gsub(/(<[^\/]*>)/,"\n\\1"))
end
end
end
@Achillefs
Achillefs / autometal_net_http.rb
Created November 29, 2010 14:51
Net::HTTP with multiple bind address functionality
require "net/http"
module Net
class HTTP
alias :old_connect :connect
def connect
D "opening connection to #{conn_address()}..."
begin # HACK START
s = timeout(@open_timeout) do
TCPSocket.open(conn_address(), conn_port(), App.available_ip_addresses.rand)
end
@Achillefs
Achillefs / google_product_api.rb
Created April 13, 2011 16:03
Perform product searches using the new Google Products API
require "uri"
require "active_support"
# Get auth token from Googles
auth_uri = "https://www.google.com/accounts/ClientLogin"
params = {
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => "yer_gmail",
"Passwd" => "yer_gpass",
"source" => "organization-appname-version",
@Achillefs
Achillefs / transliterations.yml
Created April 18, 2011 12:52
Greek transliteration table for rails applications
el:
i18n:
transliterate:
rule:
α: a
ά: a
Α: a
Ά: a
β: v
Β: v
@Achillefs
Achillefs / permalink_fu.rb
Created April 18, 2011 12:56
Patched PermalinkFu to use I18n.transliterate on string
@Achillefs
Achillefs / gist:1004409
Created June 2, 2011 13:21
crawl_website.rb
%W[rubygems anemone].each {|r| require r}
site_root = "http://www.whatever.org/"
# Create the root folder
folder = URI.parse(site_root).host
FileUtils.mkdir_p(File.join(".",folder))
Anemone.crawl(site_root) do |anemone|
anemone.on_every_page do |page|