Skip to content

Instantly share code, notes, and snippets.

View NaN1488's full-sized avatar

Nahuel Sciaratta NaN1488

View GitHub Profile
@NaN1488
NaN1488 / can_call.rb
Created December 6, 2018 14:20
Rate Limiter with redis
def can_call?(name, seconds, limit)
calls = redis.llen(name)
return false if calls > limit
if redis.exists(name)
redis.rpushx(name, 1)
else
redis.multi do
redis.rpush(name, 1)
redis.expire(name, seconds)
end
@NaN1488
NaN1488 / gist:7454292
Last active December 28, 2015 05:59 — forked from Alpa84/gist:7454193
class ApplicationController < ActionController::API
def status
begin
DatabaseHelper::Mysql.smrt.exec_query('SELECT id FROM customers LIMIT 1')
DatabaseHelper::Mysql.scr.exec_query('SELECT id FROM filter_prefixes LIMIT 1')
DatabaseHelper::Mysql.backend.exec_query('SELECT id FROM accounts LIMIT 1')
DatabaseHelper::Mysql.delayed_job.exec_query('SELECT id FROM delayed_jobs LIMIT 1')
status = Class.new.extend DatabaseHelper
Everything up-to-date
* 2013-09-26 17:39:07 executing `staging'
triggering start callbacks for `deploy:migrations'
* 2013-09-26 17:39:07 executing `multistage:ensure'
* 2013-09-26 17:39:07 executing `deploy:migrations'
* 2013-09-26 17:39:07 executing `deploy:update_code'
triggering before callbacks for `deploy:update_code'
* 2013-09-26 17:39:07 executing `smrt:compress_assets'
executing locally: "rm -rf public/assets/*"
command finished in 1071ms
@NaN1488
NaN1488 / gist:6180632
Last active December 20, 2015 19:08
OpenStruct points to get the convex points list
module ConvexHull
def self.calculate(points)
lop = points.sort_by { |p| p.x }
left = lop.shift
right = lop.pop
lower, upper = [left], [left]
lower_hull, upper_hull = [], []
det_func = determinant_function(left, right)
until lop.empty?
@NaN1488
NaN1488 / application_controller.rb
Created June 14, 2013 12:43 — forked from sergio-fry/application_controller.rb
authorized_inherited_resources with cancan
class ApplicationController < ActionController::Base
# Add this to your application_controller
def self.authorized_inherited_resources
inherit_resources
before_filter :authorize_resource_with_cancan
define_method(:authorize_resource_with_cancan) do
case action_name.to_sym
when :new, :create
@NaN1488
NaN1488 / capybara_hover.rb
Created April 10, 2013 21:22
this emulate hover action without using execute_script
# Example:
#
# find("span.assign").hover
#
module Capybara
module Node
class Element
def hover
@session.driver.browser.action.move_to(self.native).perform
end
@NaN1488
NaN1488 / hooks.rb
Last active December 16, 2015 01:19
Tell Cucumber to quit after a scenario is done - if it failed.
# Tell Cucumber to quit after a scenario is done - if it failed.
# $ cucumber EXIT_WHEN_FAIL=1
# Note: Please take in account the advantage of tags when working with a specific scenario before implemebt this!
After do |s|
if s.failed? && ENV.include?('EXIT_WHEN_FAIL') && ENV['EXIT_WHEN_FAIL'] == '1'
Cucumber.wants_to_quit = true
end
end
@NaN1488
NaN1488 / cached_grouped_options_for_select.rb
Created March 27, 2013 17:47
Add grouped_options_for_select_with_cache method to ActionView::Helpers::FormOptionsHelper in order to avoid recreate a html string options for the same collection
module ActionView
module Helpers
module FormOptionsHelper
#
# NOTE: This approach does not work with the :disabled flag and other HTML options that
# grouped_options_for_select_with_cache supports, it was done
# this way in order to improve the performance of large select tags that are rebuilt
# more than one time per request.
def grouped_options_for_select_with_cache(grouped_options, selected_key = nil, prompt = nil)
@grouped_options_cache ||= {}
@NaN1488
NaN1488 / jquery_chosen_helper.rb
Last active December 15, 2015 09:39 — forked from erkattak/jquery_chosen_helper.rb
Update to work with chosen jquery plugin. If don't know the id of the select and find option by text rather than id. Whitout using execute_script
# Example:
# chosen_select 'select[name="projects"]', "ProjectA"
#
def chosen_select (css_select, option_text)
element_id = page.find(css_select)['id']
page.driver.browser.mouse.down find("##{element_id}_chzn").native
within("##{element_id}_chzn") do
find(:xpath,"//li[text()=\"#{option_text}\"]").click
end
@NaN1488
NaN1488 / gist:4263108
Created December 11, 2012 22:56
secure and hhtponly cookies
//bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )
//@see http://php.net/manual/en/function.setcookie.php
setcookie("A", 'A', time()+3600, '/','a.com', 1, 1);