Noob basics

10 views
Skip to first unread message

micahalcorn

unread,
Jun 8, 2010, 10:04:43 PM6/8/10
to Grackle Development
This should be a simple question. I feel like I have learned the
basics of rails, and am successfully using twitter-auth for my
authentication. I am unclear on how to use grackle outside of the irb
though. Do I put "require 'grackle'" in my environment.rb or
controller(s)? Do I define the client in a model? It would be nice to
reference an example app, but I appreciate whatever advice you may
offer. Thanks for your patience!

Hayes Davis

unread,
Jun 8, 2010, 10:38:38 PM6/8/10
to grac...@googlegroups.com
Hi,

There's no one right way to do these things, of course. I'll share
with you how I've done it in my apps. See below. Hope it helps.

Hayes

On Tue, Jun 8, 2010 at 9:04 PM, micahalcorn <ma...@micahalcorn.com> wrote:
>
> This should be a simple question. I feel like I have learned the
> basics of rails, and am successfully using twitter-auth for my
> authentication. I am unclear on how to use grackle outside of the irb
> though. Do I put "require 'grackle'" in my environment.rb or
> controller(s)?

In environment.rb add the following:
config.gem 'grackle', :version=>'0.1.9'

The above is the rails mechanism for using gems in your apps.

> Do I define the client in a model? It would be nice to
> reference an example app, but I appreciate whatever advice you may
> offer. Thanks for your patience!

It all depends on what you want to do. I typically define a factory
that generates a configured grackle client that I can then either use
from a controller, model, etc. I would *not* recommend setting the
client as a class variable on a controller, however. If you plan to
use the client in the request response cycle, I would grab a new
instance from your factory in your action. Anyway, here's an example
of the type of factory I might use:

module GrackleFactory
class << self
def new_client(options=>{})
# Generate some app specific options like a User-Agent header,
proxy, authentication, etc
opts = YOUR_APPS_OPTIONS.merge(options)
Grackle::Client.new(opts)
end
end
end

Then, in (for example) a model:

class User < ActiveRecord::Base

def sync_with_twitter
client = GrackleFactory.new_client
twitter_user = client.users.show? :id=>twitter_id
# Do some stuff with the data
end

end

Reply all
Reply to author
Forward
0 new messages