views:

1208

answers:

3

I'm working on something like a social networking mesh; I am using different API's from various websites, e.g. Last.FM, Delicious, Twitter, ...

I've created one controller per each website (currently, there are 7).

Sample views:

localhost:3000/lastfm <- All datas i gathered from user's Last.fm account
localhost:3000/twitter <- All datas i gathered from user's Twitter account
...

Now I want to show these datas in one view (localhost:3000/index.hmtl) by using these different controllers.

Components are deprecated, creating one controller and bury all the API's in that seems ugly, too..

So I don't know how to do this. Any idea?

+3  A: 

First off, you should put all of the data-storing and data-gathering methods into Resources and Models so they are accessible from all controllers. You can keep the internal data-mutating operations in your individual controllers though. Once you have it organized like this, you could do what Hobo does: create a controller just for the front page, "front_controller" if you will. Here you can display data gathered from all of your models and resources, as well as links to your other controller actions.

Nick Retallack
This.Put your data collection into models. Searching Twitter? That's a odel. Scraping Last.fm? That's a model. Controllers are for clients to talk to *you* - they process requests.
Brian Hogan
+1  A: 

I think you should read a bit about rails' MVC architecture. It seems to me that you are neglecting the M(odel) part of it. The models should hold the data, thus being the most important part of your application.
These are a few interesting thoughts on better organizing your models and controllers (fat models, skinny controllers is a rule of thumb.
Since you said you are using other API's (like lastfm and twitter), you might want to take a look at this railscast about creating non ActiveRecord models (models that are not tied to a database)

If you also provide an API for your users, I suggest using a RESTful approach, as it can really be easy to develop and maintain once you get the hang of it.
You should read more about resources, as your localhost/lastfm and localhost/twitter are resources and not views.

Hope this helps. Good luck

andi
Thanks! I was obviously neglecting M part of MVC.
pinar
+3  A: 

First off, you should put all of the data-storing and data-gathering methods into Resources and Models so they are accessible from all controllers. You can keep the internal data-mutating operations in your individual controllers though. Once you have it organized like this, you could do what Hobo does: create a controller just for the front page, "front_controller" if you will. Here you can display data gathered from all of your models and resources, as well as links to your other controller actions.

These are a few interesting thoughts on better organizing your models and controllers (fat models, skinny controllers is a rule of thumb. Since you said you are using other API's (like lastfm and twitter), you might want to take a look at this railscasts about creating non ActiveRecord models (models that are not tied to a database)

here is some pseudo code, keep in mind its really only targeted at your question.

#  pseudo code  

  class TwitterController < ApplicationController
    def index
      @services = {
        :twitter => TwitterModel.find(:all, ...),
      }
    end
    def update_twitter
      TwitterUpdaterClass.update { |twit|
        _m = TwitterModel.new 
        _m.message = twit.msg
        _m.from = twit.from
        # ..
        _m.save
      }
    end
  end


  class MyIndexController < ApplicationController
    def index
      @services = {
        :twitter => TwitterModel.find(:all, ...),
        :lastfm => LastFmModel.find(:all, ...)
      }
    end
  end

it might be much better to have background-workers update your rest-services instead a controller which you need to invoke every time you want to fetch recent tweets. here is nice article showing - 6 ways to run background jobs in rubyonrails

  # more pseudo code

  class TwitterWorker < BackgrounDRb::MetaWorker
    set_worker_name :twitter_worker
    def create(args = nil) # instead of TwitterController.update_twitter
      TwitterUpdaterClass.update { |twit|
        _m = TwitterModel.new 
        _m.message = twit.msg
        _m.from = twit.from
        # ..
        _m.save
      }
    end
  end
lian
GJ ganking my exact text :P
Nick Retallack