Skip to content

Instantly share code, notes, and snippets.

@gylaz
gylaz / eslintrc
Last active August 28, 2018 19:59
Hound default configs
{
"rules": {},
"env": {
"es6": true,
"browser": true
},
"extends": "eslint:recommended"
}
@gylaz
gylaz / hound.rb
Created March 30, 2015 16:47
Hound: Helpful commands
# Run violations on a repo/pr/sha:
def violations_in_pr(repo, pr_number, sha)
api = GithubApi.new(ENV["HOUND_GITHUB_TOKEN"])
commit = Commit.new(repo, sha, api)
pr_files = api.pull_request_files(repo, pr_number).map { |file| CommitFile.new(file, commit) }
repo_config = RepoConfig.new(commit)
style_guide = StyleGuide::Ruby.new(repo_config, repo.split("/").first)
pr_files.flat_map { |file| style_guide.violations_in_file(file) }
end
@gylaz
gylaz / asset_data_uri.rb
Created March 26, 2015 18:04
Asset Data URI -- Convert image to binary data
def asset_data_uri(path)
asset = Rails.application.assets.find_asset(path)
data = Base64.encode64(asset.source)
"data:#{asset.content_type};base64,#{Rack::Utils.escape(data)}"
end
@gylaz
gylaz / tic_tac_toe.ex
Created May 17, 2014 02:11
Tic Tac Toe game in Elixirc
defmodule TicTacToe do
require Integer
defmodule X do end
defmodule Y do end
defmodule Grid do
defstruct turns: 0, cells: Enum.map(1..9, fn(_) -> nil end)
end
@gylaz
gylaz / tag.rb
Last active January 2, 2016 17:49
Polymorphic conditions
class Tag < ActiveRecord::Base
validates :name, presence: true, uniqueness: true
has_many :taggings
has_many :case_studies, through: :taggings, source: :taggable, source_type: 'CaseStudy'
has_many :posts, through: :taggings, source: :taggable, source_type: 'Post'
has_many :events, through: :taggings, source: :taggable, source_type: 'Event'
end
@gylaz
gylaz / rate_limit_post.md
Last active July 19, 2023 09:45
Handling API Rate Limits with Delayed::Job

Has your app ever encountered a 429 (Too Many Requests) status code when making requests to a third-party API? Getting rate limited can be a nussance and if not handled properly can cause a bad experience for you and your users. While one solution is to catch the exception and ignore it, a better solution is to retry the request.

Let's take a look at how we can alleviate rate-limiting woes by utilizing a background job system. In this example we'll use delayed_job, since it provides the ability to retry failed jobs.

Let's pretend that we are going to be accessing the API of a popular website. Let's setup a background job that makes a request to that API.

class MyCustomJob < Struct.new(:param1, :param2)
  def perform
 PopularSiteApi.get('/posts')
@gylaz
gylaz / fast_spec_helper.rb
Created May 13, 2013 17:25
Fast spec helper example (no rails)
$: << File.expand_path('../..', __FILE__)
require 'webmock/rspec'
require 'bourne'
Dir['spec/support/**/*.rb'].each {|f| require f}
RSpec.configure do |config|
config.order = 'random'
config.mock_with :mocha
@gylaz
gylaz / fibonacci.go
Last active December 15, 2015 08:59
Fibonacci in Go
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
current_fib_number := 0
prev_fib_number := 0
@gylaz
gylaz / scraper.rb
Last active October 10, 2015 14:27
Nokogiri scraping (Rails school class)
require 'nokogiri'
require 'open-uri'
def open_page(url)
Nokogiri::HTML(open(url))
end
# get a count of 'obama' vs 'romney' keywords
def extract_keywords(elements)
elements.each do |node|