Skip to content

Instantly share code, notes, and snippets.

View CoryFoy's full-sized avatar

Cory Foy CoryFoy

View GitHub Profile
;;core-test.clj
(ns gol.core-test
(:require [midje.sweet :refer :all]
[gol.core :refer :all]))
(facts "coin changer"
(fact "returns a penny for a penny"
(make-change-for 1) => [1])
(fact "returns 2 pennies for 2 cents"
(make-change-for 2) => [1,1])
I’m a constituent calling about the recent caucus vote by the GOP to reduce the impact of the Office of Congressional Ethics. This is something even the President Elect has come out against as of this morning, and I would like Congressman Walker to take a stand against this move and side with the president elect, as well as simply doing the right thing.
@CoryFoy
CoryFoy / sample.rb
Created November 28, 2016 21:12
TDD no Expectations
# TODO: Write sample implementation
@CoryFoy
CoryFoy / analytics.rb
Last active July 18, 2023 21:01
An example of calling the Analytics API using machine creds and the V4 API from Ruby
require 'google/apis/analyticsreporting_v4'
require 'googleauth'
include Google::Apis::AnalyticsreportingV4
include Google::Auth
VIEW_ID = "12345678" #your profile ID from your Analytics Profile
SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'
@client = AnalyticsReportingService.new
@CoryFoy
CoryFoy / railsbug.sh
Last active January 12, 2016 04:17
Rails Bug(?) where an object served through the controller as json modifies the URL parameter
This is a bug where a model with a `url` attribute gets it modified when a controller is returning a JSON response
as_json response: [{"id"=>1, "title"=>"Hello 1", "url"=>"http://www.hello.com", "created_at"=>Tue, 12 Jan 2016 04:12:22 UTC +00:00, "updated_at"=>Tue, 12 Jan 2016 04:12:22 UTC +00:00}, {"id"=>2, "title"=>"Hello 2", "url"=>"http://www.hello.com", "created_at"=>Tue, 12 Jan 2016 04:12:29 UTC +00:00, "updated_at"=>Tue, 12 Jan 2016 04:12:29 UTC +00:00}]
JSON response in browser: [{"id":1,"title":"Hello 1","url":"http://localhost:3000/stories/1.json"},{"id":2,"title":"Hello 2","url":"http://localhost:3000/stories/2.json"}]
Corys-MacBook-Pro-2:Workspace foyc$ rails --version
Rails 4.2.5
Corys-MacBook-Pro-2:Workspace foyc$ rails new rails-bug
create
create README.rdoc
@CoryFoy
CoryFoy / gist:3f77bcada9d6b9824ae2
Created February 2, 2015 00:17
Catching auth response
@implementation ViewController
// NSURLConnection Delegates
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] == 0) {
NSLog(@"received authentication challenge");
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"USER"
password:@"PASSWORD"
persistence:NSURLCredentialPersistenceForSession];
NSLog(@"credential created");
class Foo
def initialize(str)
@str = str
end
def *(num)
num.times.map { @str }.join('')
end
end
foo = Foo.new('hello')
@CoryFoy
CoryFoy / gist:6ddfd53af3559151b9b7
Created January 18, 2015 23:42
See lengths of all varchars for tables in the application
#You can use this from a rails console by creating this
#in a directory, starting up rails c, and then doing a
#'load fields.rb'. Then just call Fields.show_report
class Fields
def self.show_report
Rails.application.eager_load!
ActiveRecord::Base.descendants.each do |model|
table_name = model.table_name
puts "Varchar length report for #{table_name}"
puts "-------------------------------------------------"
describe "Mock Each" do
it "mocks it" do
a = []
a.should_receive(:each).and_yield('b')
b = []
a.each { |c| b.push(c) }
expect(b.length).to eq 1
expect(b.first).to eq 'b'
end
end
@CoryFoy
CoryFoy / gist:2db13b5b233e4968721d
Last active August 29, 2015 14:12
Base test for ensuring all controller actions that take an ID will throw a RecordNotFound for invalid IDs
class BaseTest < ActionDispatch::IntegrationTest
test "all controllers should raise exceptions with invalid IDs" do
routes = Rails.application.routes.routes
id_based_routes = routes.find_all { |r| r.required_parts.include?(:id) }
id_based_routes.each do |route|
klass = "#{route.defaults[:controller]}_controller".classify.constantize
@controller = klass.new
num_required_parts = route.required_parts.length
name = route.name
nonexistent_id = -1