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
# This is a code example for the Ruby HTTP library Typhoeus | |
# here's an example for twitter search | |
# Including Typhoeus adds http methods like get, put, post, and delete. | |
# What's more interesting though is the stuff to build up what I call | |
# remote_methods. | |
class Twitter | |
include Typhoeus | |
remote_defaults :on_success => lambda {|response| JSON.parse(response.body)}, | |
:on_failure => lambda {|response| puts "error code: #{response.code}"}, |
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
# access an http basic feed. You can't do this in a bulk feed get. It has to be when getting only one. | |
Feedzirra::Feed.fetch_and_parse(some_url, :http_authentication => ["username", "password"]) | |
# You can add custom parsing to the feed entry classes. Say you want the wfw:comments fields in an entry | |
Feedzirra::Feed.add_common_feed_entry_element("wfw:commentRss", :as => :comment_rss) | |
# The arguments are the same as the SAXMachine arguments for the element method. For more example usage look at the RSSEntry and | |
# AtomEntry classes. Now you can access those in an atom feed: | |
Feedzirra::Feed.parse(some_atom_xml).entries.first.comment_rss_ # => wfw:commentRss is now parsed! | |
# and the new accessors for feed entries |
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| |
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
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
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
# 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
# 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
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
class Entry | |
include SAXMachine | |
element :title | |
element :name, :as => :author | |
element :body | |
end |