Fetching contributors…
Cannot retrieve contributors at this time
73 lines (54 sloc) 2.55 KB
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html xmlns="" xml:lang="en" lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>HTTParty by John Nunemaker</title>
<link rel="stylesheet" href="css/common.css" type="text/css" />
<div id="wrapper">
<div id="header">
<p>Tonight we're gonna HTTParty like it's 1999!</p>
<ul id="nav">
<li><a href="rdoc/">Docs</a></li>
<li><a href="">Github</a></li>
<li><a href="">Rubyforge</a></li>
<div id="content">
<pre><code>$ sudo gem install httparty</code></pre>
<h2>Some Quick Examples</h2>
<p>The following is a simple example of wrapping Twitter's API for posting updates.</p>
<pre><code>class Twitter
include HTTParty
base_uri ''
basic_auth 'username', 'password'
end'/statuses/update.json', query: {status: "It's an HTTParty and everyone is invited!"})</code></pre>
<p>That is really it! The object returned is a ruby hash that is decoded from Twitter's json response. JSON parsing is used because of the .json extension in the path of the request. You can also explicitly set a format (see the examples). </p>
<p>That works and all but what if you don't want to embed your username and password in the class? Below is an example to fix that:</p>
<pre><code>class Twitter
include HTTParty
base_uri ''
def initialize(u, p)
@auth = {username: u, password: p}
def post(text)
options = { query: {status: text}, basic_auth: @auth }'/statuses/update.json', options)
end'username', 'password').post("It's an HTTParty and everyone is invited!")</code></pre>
<p><strong>More Examples:</strong> There are <a href="">several examples in the gem itself</a>.</p>
<p>Conversations welcome in the <a href="">google group</a> and bugs/features over at <a href="">Github</a>.</p>
<div id="footer">
<p>Created by <a href="">John Nunemaker</a> |
<a href="">Hire Me at Ordered List</a></p>