Skip to content

Instantly share code, notes, and snippets.

View pauldix's full-sized avatar

Paul Dix pauldix

View GitHub Profile
require 'benchmark'
require 'rubygems'
require 'json'
require 'yaml'
include Benchmark
benchmark_iterations = 1
large_single_dimension_array = [42, 123.123] * 5000
large_single_dimension_hash = {}
10000.times do |i|
module Feedzirra
class AtomEntry
include SAXMachine
element :title
# the :as argument makes this available through atom_entry.author instead of .name
element :name, :as => :author
element "feedburner:origLink", :as => :url
element :summary
element :content
element :published
class Entry
include SAXMachine
element :title
element :name, :as => :author
element :body
end
require 'feedzirra'
# fetching a single feed
feed = Feedzirra::Feed.fetch_and_parse("http://feeds.feedburner.com/PaulDixExplainsNothing")
# feed and entries accessors
feed.title # => "Paul Dix Explains Nothing"
feed.url # => "http://www.pauldix.net"
feed.feed_url # => "http://feeds.feedburner.com/PaulDixExplainsNothing"
feed.etag # => "GunxqnEP4NeYhrqq9TyVKTuDnh0"
# Let's say we have a feed entry object
entry = feed.entries.first
# now I could provide access to sanitization a few ways
sanitized_content = Feedzirra::Feed.sanitize(entry.content)
# but that's verbose and ugly, so maybe I could do this
sanitized_content = entry.sanitized_content
# that looks better, but I still didn't like it for some reason.
# I could also do this
santitized_content = entry.sanitized(:title)
# A code snippet that takes a hash and recursively converts it to an object
# that you call methods on instead of hash accessors. So
# h = {:foo => "bar}
# h[:foo] # => "bar"
# h = Hashit.new(h)
# h.foo # => "bar"
# a modified version of the hash mapping stuff here:
# http://pullmonkey.com/2008/1/6/convert-a-ruby-hash-into-a-class-object
class Hashit
class YahooBOSS < HTTPMachine::Remote
API_ID = "..."
remote_server "http://boss.yahooapis.com"
remote_method :web_search, {
:resource => "ysearch/web/v1",
:method => :get,
:params => {:appid => API_ID, :format => "xml", :view => "keyterms", :abstract => "long", :count => 100},
:response_handler => :parse }
result_scope :get_pages, lambda { |result|
HTTPMachine.service_access
@pages = YahooBOSS.web_search("paul dix").get_pages
end
# an idea for the interface to add common parsing elements across all feed
# entry types. Basically have them all inherit from FeedEntry and then
# open up the class.
require ‘feedzirra’
class FeedEntry
element :”wfw:commentRss”, :as => :comment_rss
end
# here's another way:
calls = 10
@klass = Class.new do
include HTTPMachine
end
benchmark do |t|
t.report("httpmachine") do
HTTPMachine.service_access do
calls.times do
s = nil
@klass.get("http://127.0.0.1:3000") do |response_code, response_body|