Skip to content

Instantly share code, notes, and snippets.

@illoyd
illoyd / auth_spec_helpers.rb
Last active November 25, 2015 16:04
Digest auth helpers for Rails 4 and your test framework of choice (rspec shown here)
#
# This should go into spec/support/auth_spec_helpers.rb
module AuthSpecHelpers
##
# Convenience method for setting the Digest Authentication details.
# To use, pass the username and password.
# The method and target are used for the initial request to get the digest auth headers. These will be translated into 'get :index' for example.
# The final 'header' parameter sets the request's authentication headers.
def authenticate_with_http_digest(user, password, method = :get, target = :index, header = 'HTTP_AUTHORIZATION')
@illoyd
illoyd / bootstrap_helpers.rb
Last active August 29, 2015 14:12
Bootstrap and Rails Helpers
##
# Prefix a string with an icon
def iconify(label, icon, options = {})
"#{ icon(icon, options) } #{ label }".strip.html_safe
end
##
# Create a label
def llabel(text, kind = :default)
content_tag(:span, text, class: [ 'label', "label-#{ kind }" ].compact)
@illoyd
illoyd / any_one_order.py
Last active October 2, 2016 06:31
AnyOneOrder for Quantopian
"""
This is an example of a one-or-the-other order, generalised as an any-one order. Useful when setting orders around a price point, such that if one order is triggered (begins to fill) the other orders are cancelled.
"""
class AnyOneOrder(object):
""" A group of orders where when one order is triggered, all other open orders are cancelled. """
def __init__(self, *orders_or_ids):
self.order_ids = set(
map(self._get_id, orders_or_ids)
)
@illoyd
illoyd / order_manager.py
Created October 2, 2016 07:44
An Order Manager for Quantopian. Slightly smarter management of open orders.
class OrderManager(object):
""" A helper for placing and tracking orders in Quantopian. """
def __init__(self):
self.orders = []
self.context = None
self.data = None
def update(self, context, data):
self.context = context