George Opritescu

Developer from somewhere

curl basic auth

curl -u user:password http://your_website
Read More

push local database to heroku

In case you already have data, you have to do the reset. Make sure you know what you’re doing, because the reset is a destructive operation.

$ heroku pg:reset DATABASE_URL -r heroku
$ heroku pg:push your_db DATABASE_URL -r heroku      
Read More

setting headers on the capybara driver

Capybara.current_session.driver.header("X-Ninja", "true")
Read More

environment variables in nginx

Allowing environment variables in nginx config:

env YOUR_BACKEND;

http {
  server {
      set_by_lua $your_backend 'return os.getenv("YOUR_BACKEND")';
      ...
      location ~ ^/to_your_backend/(.*)$ {
          set $url_full         '$1';
          ...
          proxy_pass             http://$your_backend/$url_full;
      }
  }
}
Read More

fixing chrome's open link blank page behaviour

Whenever clicking a link that should open chrome, like for example a link in the terminal, I always got a new chrome window with a blank page. Turns out it was because the file in $HOME/.local/share/applications/google-chrome.desktop had this as Exec:

Exec=/opt/google/chrome/chrome

instead of:

Exec=/opt/google/chrome/chrome %U

Found the answer here

Read More

gnu parallel processing example

Here’s a simple script that does work :)

def do_work(argument)
  puts "#{Time.now} Processing #{argument}"
  sleep 5
  puts "#{Time.now} Done processing #{argument}"
end

do_work(ARGV[0])

When running this, I get the following output on my machine:

2016-01-07 16:14:18 +0100 Processing 1
2016-01-07 16:14:23 +0100 Done processing 1

If we would like to run the script for more than one argument, it would take us some time. Luckily, we can use parallel to speed things up:

$ (echo 1;echo 2) | parallel -j+0 --eta 'ruby initial.rb {}'
When using programs that use GNU Parallel to process data for publication please cite:

  O. Tange (2011): GNU Parallel - The Command-Line Power Tool,
  ;login: The USENIX Magazine, February 2011:42-47.

This helps funding further development; and it won't cost you a cent.
Or you can get GNU Parallel without this requirement by paying 10000 EUR.

To silence this citation notice run 'parallel --bibtex' once or use '--no-notice'.


Computers / CPU cores / Max jobs to run
1:local / 4 / 2

Computer:jobs running/jobs completed/%of started jobs/Average seconds to complete
ETA: 0s Left: 2 AVG: 0.00s  local:2/0/100%/0.0s 2016-01-07 16:12:02 +0100 Processing 1
2016-01-07 16:12:07 +0100 Done processing 1
2016-01-07 16:12:02 +0100 Processing 2
2016-01-07 16:12:07 +0100 Done processing 2
ETA: 0s Left: 0 AVG: 0.00s  local:0/2/100%/2.5s

As we can see from the timestamps, both scripts started at the same time, and were executed in parallel. The -j+0 flag tells parallel to use as many cores as possible to complete the jobs.

Alternatively, this has a simpler syntax:

parallel --eta -j+0 'ruby initial.rb {}' ::: 1 2

or, with a shell glob:

parallel --eta -j+0 'ruby initial.rb {}' ::: *.txt

More examples here

GNU citation:

1
O. Tange (2011): GNU Parallel - The Command-Line Power Tool, The USENIX Magazine, February 2011:42-47.
Read More

git export and apply patch

Generating the patch from a commit:

git format-patch -1 SHA

Applying a patch:

git am < your.patch
Read More
git

refresh_token not sent in google oauth2 response

I kept receiving tokens such as:

{ access_token: 'REDACTED', token_type: 'Bearer', expiry_date: 1452085069587 }

when making calls to google api, even though I was doing the call like this:

var url = cli.generateAuthUrl({
  access_type: 'offline',
  scope: 'https://www.googleapis.com/auth/calendar',
})

As it turns out, you only get the refresh_token on the first authorization, so you should save it then. To address this, go to google apps, and revoke the app’s access to your API ( calendar in my case ), and go through the authorization flow again.

This time you’ll receive a refresh_token as well.

Read More

enumerable#find raise exception

The following will raise an exception, and you won’t have to deal with nil:

[1,2,3].find(-> { raise "Expected to find 4 in the array"}) {|e| e == 4}
Read More

ruby hash self merge

Useful trick, if you have a hash like this:

a = {1 => [2,3,4], 2 => [3,4,5], 3 => [4,5,6,7]}                                                                                

and you would like to get to a hash containing the same keys, but the count of the values:

a = {1 => 3, 2 => 3, 3 => 4}
a.merge(a) {|k,v| v.count }                                                                                                     

hash#merge takes the following params:

merge(other_hash){|key, oldval, newval| block}  new_hash
Read More