# Twitter by John Nunemaker

**URL:** <https://www.ruby-forum.com/t/twitter-by-john-nunemaker/178234>\
**Category:** Rails\
**Created:** [October 31, 2009, 11:36am UTC](https://www.ruby-forum.com/t/twitter-by-john-nunemaker/178234 "2009-10-31T11:36:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![andkjaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce73a5/32.png) [@andkjaer](https://www.ruby-forum.com/u/andkjaer)\
**Post date:** [October 31, 2009, 11:36am UTC](https://www.ruby-forum.com/t/twitter-by-john-nunemaker/178234/1 "2009-10-31T11:36:46Z")

</div>

Hi Railer’s,  
Im pllaying around with the Twitter API wrapper by John Nunemaker, but  
i’m wondering where to put all the logic.  
It can’t find any examples with this code built into rails.

What im doing now is like this, by the way Im pretty new to rails and  
ruby:

def index  
@twitter\_search = search\_twitter\_for(’#twitter’);  
end

def show  
@twitter\_search = search\_twitter\_for(’#twitter’);  
end

private

def search\_twitter\_for(query)  
Twitter::Search.new(query)  
end

Is that the right way to use it?

What if I want to use the OAuth, where do I put the following?

oauth = Twitter::OAuth.new(‘consumer token’, ‘consumer secret’)  
oauth.authorize\_from\_access(‘access token’, ‘access secret’)

client = Twitter::Base.new(oauth)

Should i live in a model of it’s own or?

---

<div class="post-metadata">

**Author:** ![andkjaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce73a5/32.png) [@andkjaer](https://www.ruby-forum.com/u/andkjaer)\
**Post date:** [November 1, 2009, 6:56pm UTC](https://www.ruby-forum.com/t/twitter-by-john-nunemaker/178234/2 "2009-11-01T18:56:15Z")

</div>

andkjaer wrote:

> Hi Railer’s,  
> Im pllaying around with the Twitter API wrapper by John Nunemaker, but  
> i’m wondering where to put all the logic.  
> It can’t find any examples with this code built into rails.
> 
> What im doing now is like this, by the way Im pretty new to rails and  
> ruby:
> 
> def index  
> @twitter\_search = search\_twitter\_for(‘#twitter’);  
> end
> 
> def show  
> @twitter\_search = search\_twitter\_for(‘#twitter’);  
> end

You should never have two methods with identical bodies. One should  
alias or call the other. Remember, duplication is bad.

make\_resourceful would probably help you here too.

> private
> 
> def search\_twitter\_for(query)  
> Twitter::Search.new(query)  
> end

If it were me, I don’t think I’d bother with this method. But if you  
must have it, the controller is certainly not the right place for it.  
If you can’t just call Twitter::Search from the controller (why not?),  
then make this a model method.

> Is that the right way to use it?

It may be the right way to use the Twitter gem, but it’s the wrong way  
to use Rails.

> What if I want to use the OAuth, where do I put the following?
> 
> oauth = Twitter::OAuth.new(‘consumer token’, ‘consumer secret’)  
> oauth.authorize\_from\_access(‘access token’, ‘access secret’)
> 
> client = Twitter::Base.new(oauth)
> 
> Should i live in a model of it’s own or?

## That should probably go in a controller before\_filter, although you might want to wrap it up into a model method to make the controller cleaner. You might also want to use Authlogic with OAuth. Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![andkjaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce73a5/32.png) [@andkjaer](https://www.ruby-forum.com/u/andkjaer)\
**Post date:** [November 1, 2009, 10:41pm UTC](https://www.ruby-forum.com/t/twitter-by-john-nunemaker/178234/3 "2009-11-01T22:41:23Z")

</div>

Hi Marnen,  
Thank you for the answer, colud you please explain this a little bit  
more: “It may be the right way to use the Twitter gem, but it’s the  
wrong way  
to use Rails.”

On 1 Nov., 18:56, Marnen Laibow-Koser \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![andkjaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce73a5/32.png) [@andkjaer](https://www.ruby-forum.com/u/andkjaer)\
**Post date:** [November 2, 2009, 1:47am UTC](https://www.ruby-forum.com/t/twitter-by-john-nunemaker/178234/4 "2009-11-02T01:47:58Z")

</div>

andkjaer wrote:

> Hi Marnen,  
> Thank you for the answer, colud you please explain this a little bit  
> more: “It may be the right way to use the Twitter gem, but it’s the  
> wrong way  
> to use Rails.”

I mean that – as detailed in my earlier post – you’re not really  
putting things in the best places for the MVC nature of the framework.

> On 1 Nov., 18:56, Marnen Laibow-Koser \<rails-mailing-l…@andreas-

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![andkjaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce73a5/32.png) [@andkjaer](https://www.ruby-forum.com/u/andkjaer)\
**Post date:** [November 2, 2009, 4:32pm UTC](https://www.ruby-forum.com/t/twitter-by-john-nunemaker/178234/5 "2009-11-02T16:32:07Z")

</div>

andkjaer wrote:

> I know, that’s why I ask where to put the different things. What I  
> don’t understand right now is you write “It may be the right way to  
> use the Twitter gem, but it’s the wrong way  
> to use Rails.” How should i then use the GEM the Rails way (Right  
> way)?

I already made specific suggestions about moving stuff into the model.  
Pleasef  
follow them.

> Thanks… :O)
> 
> On 2 Nov., 01:47, Marnen Laibow-Koser \<rails-mailing-l…@andreas-

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![andkjaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce73a5/32.png) [@andkjaer](https://www.ruby-forum.com/u/andkjaer)\
**Post date:** [November 2, 2009, 12:04pm UTC](https://www.ruby-forum.com/t/twitter-by-john-nunemaker/178234/6 "2009-11-02T12:04:48Z")

</div>

I know, that’s why I ask where to put the different things. What I  
don’t understand right now is you write “It may be the right way to  
use the Twitter gem, but it’s the wrong way  
to use Rails.” How should i then use the GEM the Rails way (Right  
way)?

Thanks… :O)

On 2 Nov., 01:47, Marnen Laibow-Koser \<rails-mailing-l…@andreas-
