You are currently browsing the category archive for the ‘Ruby’ category.

require ‘resque/tasks’
require ‘resque_scheduler/tasks’
Scheduler needs very little cpu, just start it with a worker.

desc "schedule and work, so we only need 1 dyno"
task :schedule_and_work do
  if Process.fork
    sh "rake environment resque:work"
  else
    sh "rake resque:scheduler"
    Process.wait
  end
end

Make your rspec output look more interesting!
(yes we had too much spare time at our hands… :D )

# spec/spec_helper.rb
# encoding: UTF-8

if defined? RSpec::Core::Formatters::ProgressFormatter
  RSpec::Core::Formatters::ProgressFormatter.class_eval do
    DOTS = ['☘','⚘']
    def example_passed(example)
      output.print green(DOTS[rand(DOTS.size)])
    end
  end
end
# in your controller ...
BLANK_GIF = Base64.decode64("R0lGODlhAQABAPAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")

render text: BLANK_GIF, type: 'image/gif'

A small snippet to interact with commandline applications that ask questions on stdin, particularly useful for testing.

Usage

interact "rails new foo -m template.rb", /RSpec/ => 'y', /MySql/ => 'n'

Code

 # https://gist.github.com/2166521
 # capture commandline output and send responses if nothing was read
  def interact(command, answers)
    puts command
    line = ""
    write = false

    PTY.spawn(command) do |output, input, pid|
      loop do
        rs, ws, = IO.select([output], [input])

        # read into the buffer
        if r = rs[0]
          write = false
          ret = r.read(1)
          print ret

          return unless ret

          line << ret
          line = "" if ret == "\n"
        end

        # time to write ?
        if w = ws[0]
          if write and line != ""
            text = line.gsub(/\e\[\d+m/,'').strip
            answer = answers.detect do |question, answer|
              text =~ question
            end || raise("Question #{text.inspect} has no answer!")
            w.write(answer.last + "\n")
            line = ""
          else
            write = true
          end
        end
      end
    end
    $?.success?
  end

If your environments/*.rb look like a repetitive mess, its time to get a config.yml!

  • overview of your configuration
  • dry code
  • (optional) not check in all the passwords/keys of your app (only check in config.example.yml), great for open-source apps
  • Can be loaded without loading the environment (e.g. small rake tasks)
# lib/cfg.rb
CFG = YAML.load(ERB.new(
  File.read("config/config.yml")
).result)[Rails.env].with_indifferent_access

# config/application.rb
require "cfg"

# config/config.yml
common: &common
  api:
    airbrake_key: xxx
    google_analytics_key: yyy
  admin_email: admin@example.com

test:
  <<: *common
  host: 'localhost'

development:
  <<: *common
  host: 'localhost'

production:
  <<: *common
  host: 'fuu.bar'

# config/application.rb
config.action_mailer.default_url_options = {:host => CFG[:host]}
Follow

Get every new post delivered to your Inbox.

Join 76 other followers