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 / persistent_http_client.rb
Created November 25, 2020 12:49
PersistentHttpClient
class PersistentHttpClient
def self.get(url)
uri = url.is_a?(URI) ? url : URI(url)
connection_manager.get_connection(uri)
end
def self.connection_manager
Thread.current[:persistent_http_connection_manager] ||= new_manager
end
@TheRusskiy
TheRusskiy / list_helper_methods.rb
Created December 7, 2016 17:17
List all Rails helper methods
def get_helper_modules(subtree = [])
helper_directory = Rails.root.join('app').join('helpers')
subtree.each do |folder|
helper_directory = helper_directory.join(folder)
end
entries = Dir.entries(helper_directory) - %w(. ..)
files = entries.select { |e| e.include?('.rb') }
modules = files.map do |file|
module_name = subtree.map(&:camelcase).join('::')
if module_name.present?
return {
type: actionType,
channel: channelName,
event: eventName,
data: data
};
@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
@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 / 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 / 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 / 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 / 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 / static_ngdocs_links.rb
Last active January 4, 2016 05:59
Save all ngdocs links to a file