Skip to content

Instantly share code, notes, and snippets.


Adam Watson adamlwatson

View GitHub Profile
adamlwatson /
Last active Jan 27, 2017
Problem with parsing of request with complex types
import pretend
from zeep import Client
from zeep.transports import Transport
client = Client('')
response = pretend.stub(
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="" xmlns:ns1=""><SOAP-ENV:Body><ns1:getTransactionListResponse><ns1:getTransactionListReturn><ns1:Transaction><ns1:iId>00000000</ns1:iId><ns1:sStatus>confirmed</ns1:sStatus><ns1:sType>normal</ns1:sType><ns1:sIp>x.x.x.x</ns1:sIp><ns1:bPaid>false</ns1:bPaid><ns1:iPaymentId>0</ns1:iPaymentId><ns1:iMerchantId>1111</ns1:iMerchantId><ns1:mSaleAmount><ns1:dAmount>0.00</ns1:dAmount><ns1:sCurrency>GBP</ns1:sCurrency></ns1:mSaleAmount><ns1:mCommissionAmount><ns1:dAmount>0.00</ns1:dAmount><ns1:sCurrency>GBP</ns1:sCurrency></ns1:mCommissionAmount><ns1:dClickDate>2017-01-01T00:00:00+00:00</ns1:dClickDate><ns1:dTransactionDate>2017-01-01T00:00:00+00:00</ns1
adamlwatson / for_james.rb
Last active May 7, 2016
Calculate percentage of change over time to linearly ramp a value to 0.
View for_james.rb
def linearRampToZeroOverTime( value, seconds )
value = value.to_f
update_ticks = 60
step_delta = value / seconds.to_f
tick_delta = step_delta / update_ticks.to_f
( 1..seconds ).each do | sec |
( 1..update_ticks ).each do | tick |
last_value = value
value = value - tick_delta
percentage_of_change = value / last_value
View strip-emoji.rb
# this scrubs emoji sequences from a string - i think it covers all of them
def strip_emoji ( str )
str = str.force_encoding('utf-8').encode
clean_text = ""
# emoticons 1F601 - 1F64F
regex = /[\u{1f600}-\u{1f64f}]/
clean_text = str.gsub regex, ''
adamlwatson / gist:1384414
Created Nov 22, 2011
em-http callbacks not being given time to fire when inside an async callback that is repeatedly triggered?
View gist:1384414
require 'amqp'
require 'em-http-request' do
connection = AMQP.connect(:host => '')
channel =
queue = channel.queue("em.load.test", :auto_delete => true)
exchange = channel.default_exchange
def handle_message(header, payload, *args)
adamlwatson / gist:1371577
Created Nov 16, 2011
Mongoid connection pooling in Goliath
View gist:1371577
require 'em-synchrony/em-mongo'
require 'mongoid'
mongoid_conn = 'localhost', 27017, :pool_size => 10
Mongoid.configure do |config|
config.master = mongoid_conn.db('dbname')
rescue Exception=>err
abort "An error occurred while creating the mongoid connection pool: #{err}"
adamlwatson / gist:1371170
Created Nov 16, 2011
Creating a connection pool in Goliath
View gist:1371170
config['mongo'] = do
conn ='localhost', 27017, 1, {:reconnect_in => 1})
rescue Exception=>err
abort "An error occurred while creating the mongodb connection pool: #{err}"
adamlwatson / gist:1371133
Created Nov 16, 2011
Workaround for multiple regex patterns against a variable parameter segment in a url mapping in Goliath
View gist:1371133
class RackRoutes < Goliath::API
get '/item/:value/description' do
if params[:value].match /^[a-zA-Z][\w\-]+$/ awesome stuff with a parameter that is alpha-based...
elsif params[:value].match /\d+/ awesome stuff with a parameter that is numeric-based...
run { |env| [404, {}, "" ] }
adamlwatson / Pre-requisites
Created Nov 15, 2011
Testing throughput bottlenecks in Goliath from Mongoid gem
View Pre-requisites
0. gem install goliath (should be v0.9.4 as of writing this)
1. gem install mongoid (should be v2.3.3 as of writing this)
2. create and save test.rb file (see below)
3. % ruby test.rb -sv
4. % ab -n500 -c10 http://localhost:9000/status
5. note the req/s results (somewhere around 80-90 req/s on my machine)
6. edit the test.rb file, remove the 'require mongoid' line
7. repeat steps 3 and 4.
8. note the req/s results (somewhere aaround 550 req/s on my machine)