Skip to content

Instantly share code, notes, and snippets.

@samsm
Created April 20, 2010 18:30
Show Gist options
  • Save samsm/372870 to your computer and use it in GitHub Desktop.
Save samsm/372870 to your computer and use it in GitHub Desktop.
# I'm disappointed by Rails 3
ActionMailer
ActionPack
ActiveSupport
ActiveRecord
ActiveModel
ActiveResource
class Book < ActiveResource::Base
self.site = "http://bookstore.example.com"
end
books = Book.all # GET http://bookstore.example.com/books.xml
books.length # 25
books.first.title # Slapstick
book = Book.find('Slapstick')
# GET http://bookstore.example.com/books/Slapstick.xml
map.resource :books
class BooksController
def index
@books = Book.all
respond_to do |format|
format.xml { render :xml => @books.to_xml }
end
end
def show
@book = Book.find(params[:id])
respond_to do |format|
format.xml { render :xml => @book.to_xml }
end
end
end
# Non-essential
# + Finicky
# ---------------
# Not in toolkit
# ActiveResource wants:
# [record1,record2,record3]
# Many APIs deliver:
# {:metadata => some_info, :page_number => 3, :total_pages =>7,
# :records => [record1,record2,record3]}
# More low level, at the top for some reason
Net::HTTP
EventMachine::Protocols::HttpClient
Yajl::HttpStream
RestClient
HTTParty
Twitter
Facebooker
# More high level
RestClient # 1.4.2, last updated March 13, 2010
books = RestClient.get 'http://bookstore.example.com/books.xml'
books.body # Raw XML
# Multipart!
RestClient.post '/data', :myfile => File.new("/path/to/image.jpg")
HTTParty # Version 0.5.2, last updated Jan 31, 2010
class Twitter
include HTTParty
base_uri 'twitter.com'
def initialize(u, p)
@auth = {:username => u, :password => p}
end
def timeline(which=:friends, options={})
self.class.get("/statuses/#{which}_timeline.json", options)
end
def post(text)
options = { :query => {:status => text}, :basic_auth => @auth }
self.class.post('/statuses/update.json', options)
end
end
twitter = Twitter.new(email, password)
twitter.timeline # result.to_json -> Hash/Array
Some other related projects:
Addressable
HyperActiveResource
Faraday
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment