Skip to content

Instantly share code, notes, and snippets.

Avatar
🐝
Alexa, ask the future of America to be great again! (try it)

Daniel (dB.) Doubrovkine dblock

🐝
Alexa, ask the future of America to be great again! (try it)
View GitHub Profile
@dblock
dblock / gist:11009220
Created Apr 17, 2014
Keep track of global counters in MongoDB.
View gist:11009220
module Counters
def self.next_sequence(name, increment = 1)
# find and increment (or initialize) counter
counter = collection.find(_id: name).modify({ '$inc' => { seq: increment } }, upsert: true, new: true)
# return new counter value
counter['seq']
end
private
@dblock
dblock / drawGradientImageFromColor.m
Created Apr 23, 2014
drawGradientImageFromColor
View drawGradientImageFromColor.m
#import <Foundation/Foundation.h>
...
- (UIImage *)drawGradientImageFromColor:(UIColor *)beginColor toColor:(UIColor *)endColor imageSize:(CGSize)imageSize
{
// set sideline width
CGFloat lineWidth = 3.0f;
// set a canvas, and use the imageSize
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
@dblock
dblock / mongoid_document.rb
Last active Aug 29, 2015
Find objects in the same sort order as the IDs provided
View mongoid_document.rb
require 'garner'
module Mongoid
module Document
module ClassMethods
# Find instances by ID or crtieria in the same order as the IDs provided.
# TODO: this can be done with an $or until MongoDB 2.6, see https://jira.mongodb.org/browse/SERVER-14083
def find_ordered(ids, criteria = nil)
return [] if ids.empty?
instances = criteria || self.find(ids)
View 3676_test.rb
require 'mongoid'
require 'mongoid/support/query_counter'
require 'minitest/autorun'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
Mongoid.configure.connect_to("mongoid_test")
class Address
@dblock
dblock / grape.rb
Last active Aug 29, 2015
for anatomy of a ruby gem, work in progress
View grape.rb
module Grape
class Endpoint
attr_accessor :block, :settings
attr_reader :env, :request, :headers, :params
def initialize(settings, block)
@settings = settings
method = instance_method(...) # with some unique name
@block = proc { |endpoint_instance| method.bind(endpoint_instance).call }
@dblock
dblock / markup_helper.rb
Last active Aug 29, 2015
Render markdown with syntax highlighting.
View markup_helper.rb
require 'redcarpet'
require 'coderay'
module MarkdownHelper
def render_markdown(text)
renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML, fenced_code_blocks: true)
doc = Nokogiri::HTML::DocumentFragment.parse(renderer.render(text))
doc.css('code[@class]').each do |code|
div = CodeRay.scan(code.text.rstrip, code[:class].to_sym).div
code = code.replace(div)
@dblock
dblock / downmark_it.rb
Last active Aug 29, 2015
Convert HTML to markdown.
View downmark_it.rb
# https://github.com/cousine/downmark_it
#
# =Overview
# DownmarkIt is a library to convert HTML to markdown, based on Hpricot[http://github.com/hpricot/hpricot/].
#
# =Motivation
# While working on our company's new CMS, I needed to parse HTML back to markdown and surprisngly there wasn't any solution that could fit our enviroment, so I decided to make my own and share it :)
#
# =Usage
# Make sure you install Hpricot[http://github.com/hpricot/hpricot/] first, then require the library in your application, if you are using the library in a rails application, just place it in your lib folder, then use this method to convert HTML into markdown.
@dblock
dblock / bug_bounty.md
Created Sep 22, 2014
An email describing the Artsy bug bounty program to the team.
View bug_bounty.md

Team,

I wanted to give you a bit of a background and some stats for a program that we have been running in Engineering for the past few weeks to improve the security of our websites, services and users. It's called a Security Bug Bounty and is publicly advertised under https://artsy.net/security.

What is it?

It's an opportunity to be a true, publicly recognized hacker without the FBI ever showing up at your door.

A security bug bounty is a formal program for independent security researchers to find bugs in our systems that may cause data loss, account takeovers, spam or other kinds of abuse. If you play by our rules, we pay a small bounty (often 50$) for every new bug found. We might also send you Artsy swag and we will list your name on artsy.net/security if you wish.

@dblock
dblock / cursor_helper.rb
Created Oct 20, 2014
Pagination in a Grape API with Roar.
View cursor_helper.rb
module Api
module V2
module Util
module CursorHelpers
extend ActiveSupport::Concern
# apply cursor-based pagination to a collection
# returns a hash:
# results: (paginated collection subset)
# next: (cursor to the next page)
@dblock
dblock / live-coding.md
Last active Aug 29, 2015
Implement a Hypermedia API with Grape + Roar
View live-coding.md

Create Gemfile

source 'http://rubygems.org'

gem 'grape'
gem 'grape-roar'

Run bundle install.