Skip to content

Instantly share code, notes, and snippets.

@andrey-kazakov
andrey-kazakov / socksify_faraday.rb
Created February 17, 2012 18:27
HTTP over SOCKS support monkey patch for Mechanize, Faraday and it's based clients (OAuth2 like)
# requires socksify gem
require "socksify"
require 'socksify/http'
# use w/ OAuth2 like OAuth2::Client.new(id, secret, connection_opts: { proxy: 'socks://127.0.0.1:9050' })
class Faraday::Adapter::NetHttp
def net_http_class(env)
if proxy = env[:request][:proxy]
if proxy[:uri].scheme == 'socks'
Net::HTTP::SOCKSProxy(proxy[:uri].host, proxy[:uri].port)
@andrey-kazakov
andrey-kazakov / robokassa_merchant.rb
Created May 2, 2012 20:08
Robokassa Merchant rails rack middleware
require 'digest/md5'
require 'uri'
# this code works fine in some of my projects, but I'm just lazy to document it well...
# so, feel free to use it after full review of logic (is it matching your purposes?)
# at your own risk, of course! :)
class RobokassaMerchant
class << self
def signed_start_params objects_amount, objects_filter, objects_cost, user
@andrey-kazakov
andrey-kazakov / content_repeat.js.coffee
Created January 23, 2013 23:26
This AngularJS directive, when added to element, repeats it's content as many times as length of given array (almost like ng-repeat). The main difference is that DOM, once built, don't being changed on each $digest(). If you want to force the changing of repeated content, you should emit an event 'invalidateView' with (optionally) name of the sc…
angular
.module('ContentRepeat', [])
.directive 'contentrepeat', ($compile) ->
priority: 1000
terminal: true
compile: (element, attr, linker) ->
template = $compile(element.html())
element.empty()
(scope, self, attr) ->
@andrey-kazakov
andrey-kazakov / Gemfile
Created January 29, 2013 20:04
My solution for platform-independent testing with guard gem on Mac/Linux. This code installs both rb-fsevent/rb-inotify gems on all platforms (and this way it won't cause platform conflicts with Gemfile.lock), but really requires only needed one.
group :test do
gem 'guard'
###
gem 'rb-fsevent', '~> 0.9.1', require: (RUBY_PLATFORM.downcase.include?("darwin") && 'rb-fsevent')
gem 'rb-inotify', '~> 0.8.8', require: (RUBY_PLATFORM.downcase.include?("linux") && 'rb-inotify')
end
@andrey-kazakov
andrey-kazakov / FocusMonitorMixin.js
Created May 17, 2015 13:19
React focus monitor mixin (click or focus outside of monitored node calls a handler)
module.exports = {
componentWillUnmount: function() {
this.endFocusMonitor();
},
startFocusMonitor: function() {
document.addEventListener('focusin', this.checkFocusTarget);
document.addEventListener('click', this.checkFocusTarget);
},
@andrey-kazakov
andrey-kazakov / DeferredComponent.jsx
Last active August 29, 2015 14:27
It's a solution to asynchronously load React components used internally in our project. It has a quite simple logic, so just read the script and use it as you want. It depends on jQuery.ajax and jQuery.Deferred, but you can easily avoid it. Loaded script should just do `window.deferredComponents['YourReactComponent'].resolve(YourReactComponent)`
var DeferredComponent;
DeferredComponent = React.createClass({
propTypes: {
componentName: React.PropTypes.string.isRequired
},
getInitialState: function() {
return {
resolvedComponent: null,
@andrey-kazakov
andrey-kazakov / authenticator.rb
Created January 7, 2018 10:07
Simple service for checking user's authenticity (and community role) for VK Iframe-apps.
module Vk::Web
class Authenticator
VK_VIEWER_TYPES = %i[not_participant participant moderator editor administrator].freeze
def initialize(params:)
@params = params
end
def real_viewer?
api_id_valid? && auth_key_valid? && signature_valid?
@andrey-kazakov
andrey-kazakov / interactor.rb
Created January 7, 2018 10:48
Simple service for interaction with Justclick API in Ruby (depends on Faraday)
module Justclick::Api
class Interactor
def invoke_method(method, data, allowed_codes: [0, 601])
check_response_status(
ParamsDecoder.decode(
http_client.post(
[
"http://",
Rails.application.secrets.justclick[:user],
".justclick.ru/api/",