Skip to content

Instantly share code, notes, and snippets.

class Post < ActiveRecord::Base
belongs_to :user
end
@natereed
natereed / encode_post_params
Created January 17, 2013 23:10
Convert a postParams hash from an HAR file into a URL-encoded string.
def encode_post_params(params)
params.map do |param|
name = param['name']
value = param['value']
"#{CGI.escape(name)}=#{CGI.escape(value)}"
end.join('&')
end
@natereed
natereed / CRMClientTest.java
Last active December 30, 2016 00:35
Stubbing/mocking Jersey Client. Inspired by WebMock.
import org.junit.Before;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
public class CRMClientTest {
private CRMClient crmClient;
private StubbedClientHandler clientHandler;
@natereed
natereed / read_fieldnames.py
Created November 5, 2015 21:12
Read the headers of a csv file into fieldnames for reading with csv.DictReader
def read_fieldnames(path):
with open(path, "r") as path_csv:
reader = csv.reader(path_csv)
row = reader.next()
return row
@natereed
natereed / set_encoding_utf8.py
Created November 5, 2015 21:14
Set system default encoding to UTF-8
reload(sys)
sys.setdefaultencoding('utf8')
@natereed
natereed / set_encoding_utf8.py
Created November 5, 2015 21:15
Set system default encoding to UTF-8
reload(sys)
sys.setdefaultencoding('utf8')
@natereed
natereed / inspect-scrapy-response.py
Created November 13, 2015 16:53
Inspect a Scrapy response
from scrapy.shell import inspect_response
inspect_response(response, self)
@natereed
natereed / make_regex.py
Created December 9, 2015 17:02
Make a regex from an input string
import re
text = '''Information to be included in statements filed
pursuant to Rule 13d-1 (b) (c) and (d) and Amendments thereto
filed pursuant to Rule 13d-2 (b).'''
text = text.replace('(', '\(')
text = text.replace(')', '\)')
text = re.sub(r'\s+', '\s+', text)
text = re.sub(r'\.', '\.', text)
@natereed
natereed / split_filename.py
Created December 17, 2015 01:14
Split a filename into name and extension
import os
basename = os.path.basename(path)
(root, extension) = os.path.splitext(basename)
@natereed
natereed / CIKgetter.R
Created January 21, 2016 19:46 — forked from ddd1600/CIKgetter.R
get SEC CIK number from ticker symbol
getCIK = function(ticker) {
stopifnot(is.character(ticker))
uri = "http://www.sec.gov/cgi-bin/browse-edgar"
response = getForm(uri,CIK=ticker,action="getcompany")
html = htmlParse(response)
CIKNode = getNodeSet(html, "//acronym[@title=\"Central Index Key\"][text() = \"CIK\"]")
CIKNodeText = sapply(CIKNode, function(x) xmlValue(getSibling(getSibling(x))))
CIK = sub(" .*","",CIKNodeText)
CIK = sub("^0*","",CIK)