Skip to content

Instantly share code, notes, and snippets.

View hanshasselberg's full-sized avatar

Hans Hasselberg hanshasselberg

View GitHub Profile
orthos $ bundle exec ruby perf/orthos.rb
user system total real
[ 100000 Creations]
String.new 0.020000 0.000000 0.020000 ( 0.019355)
Easy.new 0.230000 0.010000 0.240000 ( 0.239970)
[ 1000 Requests]
net/http 0.490000 0.150000 0.640000 ( 2.220845)
open-uri 0.810000 0.150000 0.960000 ( 2.520993)
patron 0.140000 0.030000 0.170000 ( 1.496441)
patron reuse 0.170000 0.040000 0.210000 ( 1.525481)
$ git diff
diff --git a/Gemfile b/Gemfile
index c341f77..67c6fa8 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1,4 +1,6 @@
source :rubygems
+
+gem 'typhoeus', path: '../typhoeus'
gemspec
hydra = Typhoeus::Hydra.new
hydra.cache_setter do |request|
@cache.set(request.cache_key, request.response, request.cache_timeout)
end
hydra.cache_getter do |request|
@cache.get(request.cache_key) rescue nil
end
e = Typhoeus::Easy.new
e.auth = {
:username => 'username',
:password => 'password',
:method => Typhoeus::Easy::AUTH_TYPES[:CURLAUTH_NTLM]
}
e.url = "http://example.com/auth_ntlm"
e.method = :get
e.perform
@hanshasselberg
hanshasselberg / add_field.diff
Created July 3, 2012 10:16
Add ean13 field to combinations.
commit df5c445ec9fb5e7b0cfb2a4e67ce366226edc79e
Author: Hans Hasselberg <me@hans.io>
Date: Tue Jul 3 11:31:01 2012 +0200
Add lieferzeit field to combinations.
diff --git a/themes/prestashop/js/product.js b/themes/prestashop/js/product.js
index 01acb56..a9332c4 100644
--- a/themes/prestashop/js/product.js
+++ b/themes/prestashop/js/product.js
@@ -50,7 +50,7 @@ function oosHookJsCode()
}
@hanshasselberg
hanshasselberg / multiple_requests.rb
Created July 29, 2012 21:18
ethon: stream response into file.
require 'ethon'
multi = Ethon::Multi.new
[
"http://cvcl.mit.edu/hybrid/cat2.jpg",
"http://www.petscareblog.com/wp-content/uploads/2011/03/kucing.jpg",
"http://www.great-printable-calendars.com/image-files/cat-calendars-cat2.jpg"
].each_with_index{ |url, index|
easy = Ethon::Easy.new(url: url)

Hedji

Authorization

You have to provide an apikey except for register views.

$ curl http://hedji.com/1.json?apikey=12345

Register view

@hanshasselberg
hanshasselberg / gist:3618489
Created September 4, 2012 08:26
Configuration
# your_lib.rb
module YourLib
def self.configure
yield Config
end
end
# your_lib/config.rb
module YourLib
module Config
Typhoeus.on_complete {|r| p Time.now.to_f }
hydra = Typhoeus::Hydra.new(max_concurrency: 1)
10.times.map{ hydra.queue(Typhoeus::Request.new("www.example.com")) }
hydra.run
# 1347304950.781197
# 1347304950.9252968
# 1347304951.216028
# 1347304951.3606741
# 1347304951.660367
# 1347304951.80959
@hanshasselberg
hanshasselberg / gist:3760076
Created September 21, 2012 06:53
sample mongoid exception
irb(main):001:0> Product.find("123")
Mongoid::Errors::DocumentNotFound:
Problem:
Document(s) not found for class Product with id(s) 123.
Summary:
When calling Product.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): 123 ... (1 total) and the following ids were not found: 123.
Resolution:
Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for multiples.
irb(main):002:0>