George Opritescu

Developer from somewhere

connection management with node.js

Strange thing. The wireless network I’m currently on seems to randomly disconnect. To prevent having to reconnect from the NetworkManager applet, I wrote the following simple script:

child_process = require("child_process")
network_name = "your-wireless-network-name"

reenqueue_time = 15000
network_check = ->
  console.log("started network check")
  child_process.exec "ping -c 1 google.ro", (err, stdout, stderr) ->
    if(err)
      console.log("pinging google failed")
      console.log(err)
      console.log("stderr:")
      console.log(stderr)
      child_process.exec "nmcli con down id #{network_name}", (err, stdout, stderr) ->
        if !err
          child_process.exec "nmcli con up id #{network_name}", (err, stdout, stderr) ->
            if err
              console.log("nmcli con up failed")
            else
              console.log("nmcli up succesful")
            setTimeout(network_check, reenqueue_time)
        else
          setTimeout(network_check, reenqueue_time)
    else
      console.log("pinging succesful")
      setTimeout(network_check, reenqueue_time)

network_check()
Read More

My First Normal Post on Jekyll

Steve Austin: Astronaut. A man barely alive. Gentlemen… we can rebuild him. We have the technology. We have the capability to make the world’s first bionic man. Steve Austin will be that man. Better than he was before. Better. Stronger. Faster.

My name is Rhoda Morgenstern. I was born in the Bronx, New York in December of 1941. I’ve always felt responsible for World War II. The first thing I remember liking that liked me back was food. I had a bad puberty; it lasted seventeen years. I’m a high school graduate. I went to art school. My entrance exam was on a book of matches. I decided to move out of the house when I was twenty-four. My mother still refers to this as the time I ran away from home. Eventually, I ran to Minneapolis, where it’s cold and I figured I’d keep better. Now I’m back in Manhattan. New York, this is your last chance!

Dr. David Banner: physician; scientist. Searching for a way to tap into the hidden strengths that all humans have… then an accidental overdose of gamma radiation alters his body chemistry. And now when David Banner grows angry or outraged, a startling metamorphosis occurs. The creature is driven by rage and pursued by an investigative reporter. The creature is wanted for a murder he didn’t commit. David Banner is believed to be dead, and he must let the world think that he is dead, until he can find a way to control the raging spirit that dwells within him.

Read More

run rspec test matching regex

I wanted to run just a test matching a specific pattern, from an integration suite. The SPEC_OPTS variable allowed me to do just that:

env SPEC_OPTS='-e translations' SELENIUM=1 bundle exec rspec spec/features/*
Read More

automating the commandline using ruby

Lately, I’ve been thinking about how to automate some commandline stuff. I knew about expect, so I started to dig into similar solutions for ruby. Turns out, there’s expect, but … it looks kind of complicated. And that’s how the search for gems with a nicer API started. I fire up github, and I write ruby expect in the searchbox. As expected, a ton of results, but some of them look promising. Until I see that there’ve been no commits in quite some time.

So, I order them by Recently Updated. And I stumble upon greenletters.

Seeing that it’s from Avdi Grimm, it kind of instantly gets a lot of credibility from me.

I read the introductory blog post, and I’m hooked.

So, I’ve decided to write a simple script, to automate running heroku run bash, because why not, right?

The first try failed really fast

home/geo/.rvm/gems/ruby-2.2.1@russh/gems/greenletters-0.3.0/lib/greenletters.rb:648:in `process_interruption': Interrupted (timeout) while waiting for output matching /~ $/. (Greenletters
::SystemError)

Turns out, greenletters seems to have a pretty small timeout by default. After a bit of browsing around in the issues section, I stumble upon the :timeout option, which looks to be exactly what I need. After that, things just started to fall into place, so without further ado, I present to you the most useful script, which runs ls -l and cats the Gemfile:

require "bundler"

Bundler.require(:default)

adv = Greenletters::Process.new("heroku run bash -a myherokuapp", :timeout => 100, :transcript => $stdout)
adv.start!

adv.wait_for(:output, /run.\d+.*?\$/m)
adv << "ls -l\n"
adv.wait_for(:output, /\$/)
adv << "cat Gemfile\n"
adv.wait_for(:output, /\$/)

Low and behold, it works. The run pattern is in that way, because when heroku runs a command, it’s output has the following format:

Running `bash` attached to terminal... up, run.5710

Also, the heroku shell command prompt looks like this ~ $. Hopefully, it makes sense why I used those regexes now.

I can see plenty of uses for such a gem, and it’s API is pretty nice to work with. Hopefully you can put it to great use. I’m interested to see what you’ll come up with :)

Read More

generate corresponding sql from a hash of attributes

User.send(:sanitize_sql_for_conditions, User.first.attributes)
Read More

capybara login using warden helpers

Include Warden::Test::Helpers in your test, to be able to access the login_as method. Example:

describe "Something", :js => true do
  include Warden::Test::Helpers

  it "does smth" do
    user = FactoryGirl.create(:user)
    login_as(user) # method provided by warden
  end
end
Read More

jquery prevent enter from submitting

When trying to catch enter in a submit, the following did the trick:

    $(document).on 'keypress keydown keyup', '#elem', (e) ->
     if e.which == 13
       # whatever logic
       e.preventDefault()
Read More

postscript delegate failed

Solution for error messages such as:

Postscript delegate failed `/home/ubuntu/workspace/project/spec/testfiles/example.pdf': No such file or directory @ error/pdf.c/ReadPDFImage/677

Is fixed by:

sudo apt-get install ghostscript
Read More

sinatra routes inside a class

Working example of sinatra routes with routes inside a class:

module MyMod
  class Application < ::Sinatra::Base
    get "/hello" do
      "potato"
    end

    get "/movies/:movie_name/actors" do
      {
        actors: [
          {
            name: "Actor 1",
            character_played: "Character 1"
          },
          {
            name: "Actor 2",
            character_played: "Character 2"
          }
        ]
      }.to_json
    end
  end
end

MyMod::Application.run!
Read More