Skip to content

Instantly share code, notes, and snippets.

View dblock's full-sized 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 April 17, 2014 20:19
Keep track of global counters in MongoDB.
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 April 23, 2014 20:59
drawGradientImageFromColor
#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 August 29, 2015 14:01
Find objects in the same sort order as the IDs provided
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)
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 August 29, 2015 14:02
for anatomy of a ruby gem, work in progress
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 August 29, 2015 14:05
Render markdown with syntax highlighting.
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 August 29, 2015 14:06
Convert HTML to markdown.
# 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 September 22, 2014 01:25
An email describing the Artsy bug bounty program to the team.

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 October 20, 2014 13:26
Pagination in a Grape API with Roar.
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 August 29, 2015 14:08
Implement a Hypermedia API with Grape + Roar

Create Gemfile

source 'http://rubygems.org'

gem 'grape'
gem 'grape-roar'

Run bundle install.