Skip to content

Instantly share code, notes, and snippets.

View TheRusskiy's full-sized avatar
⌨️
Living the life, one keystroke at a time

Dmitry Ishkov TheRusskiy

⌨️
Living the life, one keystroke at a time
View GitHub Profile
@TheRusskiy
TheRusskiy / RailsCastsMultiStream.rb
Last active December 24, 2015 11:39
RailsCastsMultiStream
# Copyright © 2013 Ishkov Dmitry <therusskiy@gmail.com>
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
require 'open-uri'
require 'nokogiri'
require 'typhoeus'
def make_name link
@TheRusskiy
TheRusskiy / execute_with_delay.py
Created November 9, 2013 11:39
Execute shell command after a specified time period. E.g. shutdown computer.
import os
import datetime
import time
execute_after_minutes = 17
command = 'shutdown /h'
# 'shutdown /s' #shutdown
# 'shutdown /h' #hibernate
def now():
return datetime.datetime.now()
@TheRusskiy
TheRusskiy / static_ngdocs_links.rb
Last active January 4, 2016 05:59
Save all ngdocs links to a file
@TheRusskiy
TheRusskiy / auth.coffee
Last active August 29, 2015 13:55
NodeJS authorization
auth = (role_middleware_hash)->
return (req, res, next)->
# transform to [{role: function}, {another_role: true}] format:
role_middleware_array = for key, value of role_middleware_hash then obj={}; obj[key]=value; obj
if not req.user? then res.send(403); return; # no user == no access
nextFn = (allowed)->
if allowed is true # previous call allowed access
next()
else if role_middleware_array.length is 0 or allowed is false # all middleware behind, still no explicit access
res.send(403)
@TheRusskiy
TheRusskiy / chatControllerExample.coffee
Last active August 29, 2015 13:56
Socket.io with AngularJS
'use strict';
angular.module('chatApp')
.controller 'ChatCtrl', ($scope, Socket)->
socket = Socket($scope)
socket.emit 'subscribe', {}
$scope.messages = [ ]
socket.on 'message_history', (messages)->
@TheRusskiy
TheRusskiy / embed_subs.rb
Created January 6, 2015 14:09
Embed subtitles for all videos in a folder
require 'awesome_print'
folder = "/media/media_nas_drive/Train/Video/Biology/Consumer Neuroscience/"
folder_for_cd = folder.gsub(' ', '\ ')
files = Dir[folder+'*.mp4'].map{|f| f.gsub(folder, '').gsub('.mp4', '')}
files.each_with_index do |f, i|
ap "!!!!!!!!!"
ap "#{i+1}/#{files.length}"
ap "!!!!!!!!!"
system "cd #{folder_for_cd} && ffmpeg -i \"#{f}.mp4\" -f srt -i \"#{f}.srt\" -c:v copy -c:a copy \
-c:s mov_text \"new_video/#{f}.mp4\""
@TheRusskiy
TheRusskiy / angular_parse_html.js
Created January 18, 2015 23:51
Parse HTML generated outside of Angular
var $injector = angular.element(document).injector(); // first we get the injector our app uses
$injector.invoke(function($rootScope, $compile) { // you inject anything here in params
var element = angular.element('#selector'); // get the element we want to parse
// following hack isn't always neccessary, but I use to prevent double parsing if the same HTML is generated statically and dynamically
if (element.attr('class') && element.attr('class').indexOf("ng-scope")!=-1){
return;
}
var compiled = $compile(element)($rootScope); // finally, we compile the element against root scope
compiled.scope().$apply(); // ... and we tell Angular that we made changes outside of its 'world'
});
@TheRusskiy
TheRusskiy / ajax.js
Created January 7, 2016 18:10
for blogpost about error handling
$.ajax({
dataType: "json",
url: "/stuff/" + id + "/do_something",
data: {x: x, y: y}
}).done(function(response){
window.alert("Yay!");
}).fail(function(error){
// even if no message is provided we should tell a user that something went wrong
errorMessage = (error.responseJSON && error.responseJSON.message) || "Some error occurred!"
window.alert(errorMessage);
@TheRusskiy
TheRusskiy / application_controller.rb
Created October 1, 2016 20:01
Smart page titles in Rails
class ApplicationController < ActionController::Base
before_filter :set_page_title
CONTROLLER_TITLES = Hash.new
# for setting page titles for the whole controller
def self.page_title title
CONTROLLER_TITLES[self.name] = title
end
return {
type: actionType,
channel: channelName,
event: eventName,
data: data
};