This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Entry | |
include SAXMachine | |
element :title | |
element :name, :as => :author | |
element :body | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HTTPMachine.service_access | |
@pages = YahooBOSS.web_search("paul dix").get_pages | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| |
OlderNewer