<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Manoel Lemos .Com &#187; Uncategorized</title>
	<atom:link href="http://manoellemos.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://manoellemos.com</link>
	<description>meu canto na internet</description>
	<lastBuildDate>Sun, 29 Nov 2009 22:26:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Tutorial : Integrando sua aplicação Ruby (ou Rails) com o Twitter</title>
		<link>http://manoellemos.com/2009/03/17/tutorial-integrando-sua-aplicacao-ruby-ou-rails-com-o-twitter/</link>
		<comments>http://manoellemos.com/2009/03/17/tutorial-integrando-sua-aplicacao-ruby-ou-rails-com-o-twitter/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 23:44:51 +0000</pubDate>
		<dc:creator>mlemos</dc:creator>
				<category><![CDATA[Integrações]]></category>
		<category><![CDATA[Projetos]]></category>
		<category><![CDATA[Tutoriais]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tuiter]]></category>
		<category><![CDATA[tuitersfera]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[webco]]></category>

		<guid isPermaLink="false">http://manoellemos.com/?p=247</guid>
		<description><![CDATA[Nesta semana liberamos a implementação da API do Twitter que desenvolvemos e utilizamos no mais novo projeto da Webco, a Tuitersfera. Liberamos a API no GitHub com o nome de Tuiter (isto mesmo, Tuiter com TU, bem abrasileirado). Neste post farei um pequeno tutorial de introducão ao uso da Gem Tuiter para integrar sua aplicação [...]]]></description>
			<content:encoded><![CDATA[<p>Nesta semana liberamos a implementação da <a href="http://github.com/webco/tuiter">API do Twitter</a> que desenvolvemos e utilizamos no <a href="http://manoellemos.com/2009/03/15/tuitersfera-mapeando-o-uso-do-twitter-no-brasil/">mais novo projeto da Webco, a Tuitersfera</a>. Liberamos a API no GitHub com o nome de <a href="http://github.com/webco/tuiter">Tuiter</a> (isto mesmo, Tuiter com TU, bem abrasileirado). Neste post farei um pequeno tutorial de introducão ao uso da <a href="http://github.com/webco/tuiter">Gem Tuiter</a> para integrar sua aplicação com o Twitter.</p>
<h2>Instalando a gema Tuiter</h2>
<p>Se você ainda não adicionou o <a href="http://github.com/">GitHub</a> às suas fontes de gemas, execute o comando abaixo. Se já fez isto, pule este passo e vá direto para o comando seguinte para a instalação da gema.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="" style="font-family:monospace;">% gem sources -a http://gems.github.com</pre></td></tr></table></div>

<p>Instalando a gema em seu computador:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="" style="font-family:monospace;">% sudo gem install webco-tuiter</pre></td></tr></table></div>

<h2>Testando a instalação </h2>
<p>Para testar a instalação, basta você abrir o interpretador Ruby e executar os comandos abaixo. Utilize seu usuário do Twitter e sua senha na chamada que instancia a classe cliente da API. Se tudo der certo, o código deve apresentar a sua descrição (de seu perfil) no Twitter.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'tuiter'</span>
&nbsp;
cli = <span style="color:#6666ff; font-weight:bold;">Tuiter::Client</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:username</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;seu_login&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;sua_senha&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
voce = cli.<span style="color:#9900CC;">get_user</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;seu_login&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> voce.<span style="color:#9900CC;">description</span></pre></td></tr></table></div>

<p>Todos os métodos de manipulação da API estão na classe <strong>Client</strong>.</p>
<h2>Atualizando seu status no Twitter</h2>
<p>Para atualizar seu status no Twitter basta usar o método <strong>update()</strong> da classe <strong>Client</strong>. Veja:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'tuiter'</span>
&nbsp;
cli = <span style="color:#6666ff; font-weight:bold;">Tuiter::Client</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:username</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;seu_login&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;sua_senha&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
cli.<span style="color:#9900CC;">update</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Olá mundo, estou no Twitter!!!&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<h2>Consultando seus @replies</h2>
<p>Utilize o método <strong>get_replies()</strong> da classe <strong>Client</strong>. Este método trará os 20 últimos updates recebidos pelo usuário que você usou para inicializar a classe <strong>Client</strong>. Você pode pegar mais replies paginando os resultados, para isto basta passar o parâmetro <strong>page</strong> na chamada do método. O código abaixo mostrará seus 40 últimos replies:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'tuiter'</span>
&nbsp;
cli = <span style="color:#6666ff; font-weight:bold;">Tuiter::Client</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:username</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;seu_login&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;sua_senha&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
replies = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
replies <span style="color:#006600; font-weight:bold;">|</span>= cli.<span style="color:#9900CC;">get_replies</span>
replies <span style="color:#006600; font-weight:bold;">|</span>= cli.<span style="color:#9900CC;">get_replies</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
replies.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>reply<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;From #{reply.user.screen_name} @ #{reply.created_at.to_s} : #{reply.text}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<h2>Enviando uma mensagem direta (direct)</h2>
<p>Utilize o método <strong>direct_new()</strong> da classe <strong>Client</strong>. Os parâmetros são o <strong>id</strong> ou o <strong>login</strong> do usuário para quem você deseja enviar a mensagem e a <strong>mensagem</strong> a ser enviada. Veja:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'tuiter'</span>
&nbsp;
cli = <span style="color:#6666ff; font-weight:bold;">Tuiter::Client</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:username</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;seu_login&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;sua_senha&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
cli.<span style="color:#9900CC;">direct_new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;crisdias&quot;</span>,<span style="color:#996600;">&quot;olá cris, quanto vale um whuffie?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<h2>Consultando dados de um usuário</h2>
<p>Para isto você deve usar o método <strong>get_user</strong> da classe <strong>Client</strong>. O resultado é um objeto da classe <strong>ExtendedUser</strong> e ela contém métodos para consultar cada atributo de um usuário. Veja:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'tuiter'</span>
&nbsp;
cli = <span style="color:#6666ff; font-weight:bold;">Tuiter::Client</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:username</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;seu_login&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;sua_senha&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
edney = cli.<span style="color:#9900CC;">get_user</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;interney&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> edney.<span style="color:#9900CC;">name</span>  <span style="color:#008000; font-style:italic;"># Nome do usuário</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> edney.<span style="color:#9900CC;">screen_name</span>  <span style="color:#008000; font-style:italic;"># Login do usuário</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> edney.<span style="color:#9900CC;">description</span>  <span style="color:#008000; font-style:italic;"># Descrição do perfil do usuário</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> edney.<span style="color:#9900CC;">url</span>  <span style="color:#008000; font-style:italic;"># URL do perfil do usuário</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> edney.<span style="color:#9900CC;">followers_count</span>  <span style="color:#008000; font-style:italic;"># Quantidade de seguidores do usuário</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> edney.<span style="color:#9900CC;">friends_count</span>  <span style="color:#008000; font-style:italic;"># Quantidade de amigos (pessoas que ele segue) do usuário</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> edney.<span style="color:#9900CC;">location</span>  <span style="color:#008000; font-style:italic;"># Localização do perfil do usuário</span></pre></td></tr></table></div>

<h2>Dicas finais</h2>
<p>Existe um limite de utilização da API do Twitter para cada usuário. Este limite, normalmente, é de 100 requisições por hora. Então, cuidado para não estourar o limite, isto fará com que você não consiga interagir com o Twitter até acabar aquela hora. Este limite é chamado de <strong>Rate Limit</strong> e também pode ser consultado pela nossa implementação da API. Para isto utilize o método <strong>rate_limit</strong> da classe <strong>Client</strong>. O resultado é uma estrutura que traz dados sobre como anda seu limites de uso da API. Veja:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'tuiter'</span>
&nbsp;
cli = <span style="color:#6666ff; font-weight:bold;">Tuiter::Client</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:username</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;seu_login&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;sua_senha&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
cli.<span style="color:#9900CC;">rate_limit</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;Tuiter::RateLimit:0x1802e3c @reset_window=2706.502303, @reset_time=Tue Mar 17 21:15:48 -0300 2009, @hourly_limit=100, @reset_time_in_seconds=Tue Mar 17 21:15:48 -0300 2009, @remaining_hits=84&gt;</span></pre></td></tr></table></div>

<p>Observe que, no exemplo, ainda temos 84 requisições restantes (<strong>remaining_hits</strong>) até estourar o limite de uso da API na hora corrente. E ainda, a hora corrente acaba às 21:15 (<strong>reset_time)</strong> e ai teremos mais 100 requisições de limite.</p>
<p>A outra dica é consultar a <a href="http://apiwiki.twitter.com/">documentação oficial da API do Twitter</a> para entender melhor como ela funciona.</p>
<h2>Colabore com nossa API</h2>
<p>O projeto está hospedado no GitHub e qualquer um pode colaborar. Para entender melhor como isto pode acontecer, entre em contato com os desenvolvedores que estão cuidando dela (<a href="http://twitter.com/lsdr">Luiz Rocha</a>, <a href="http://twitter.com/lfcipriani">Luis Cipriani</a> e o <a href="http://twitter.com/lucashungaro">Lucas Húngaro</a>) através do email <a href="mailto:tuitersfera@webcointernet.com">tuitersfera (arroba) webcointernet.com</a>. Se você encontrar algum bug, tiver alguma sugestão ou desenvolver qualquer coisa legal com a API, também não deixe de <a href="mailto:tuitersfera@webcointernet.com">avisar</a>.</p>
<p>Boa Sorte e boas Tuitadas!!!</p>
]]></content:encoded>
			<wfw:commentRss>http://manoellemos.com/2009/03/17/tutorial-integrando-sua-aplicacao-ruby-ou-rails-com-o-twitter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
