0

I want to get my tweets using user_timeline()and save them to my application database.

I'm not sure where to call that function and how to save each tweet associating with my user.

This is the way I'm saving users to the database

class SessionsController < ApplicationController
  def create
    auth = request.env['omniauth.auth']
    session[:omniauth] = auth.except('extra')
    user = User.sign_in_from_omniauth(auth)
    session[:user_id] = user.id

    redirect_to root_url, notice: "SIGNED IN"
  end
end

Here's my model...

class User < ActiveRecord::Base
  def self.sign_in_from_omniauth(auth)
    find_by(provider: auth['provider'], uid: auth['uid']) || create_user_from_omniauth(auth)
  end

  def self.create_user_from_omniauth(auth)
    create(
        provider: auth['provider'],
        uid: auth['uid'],
        name: auth['info']['name'],
        screen_name: auth['info']['nickname'],
        oauth_token: auth['credentials']['token'],
        oauth_secret: auth['credentials']['secret']
    )
  end

  def twitter
    Twitter::REST::Client.new do |config|
        config.consumer_key         = 'XXXXXXXXXXXXXXXX'
        config.consumer_secret      = 'XXXXXXXXXXXXXXXX'
        config.access_token         = oauth_token
        config.access_token_secret  = oauth_secret
    end
  end
end

Where I should call user_timeline() and save all tweets?

  • 1
    Usually the first step is to create a model to capture your data, and the second step is to start calling `create` on that model with the data you want to save. – tadman Apr 08 '16 at 00:21
  • I have other application where I call `user_timeline()` from `SessionsController` then I call `set_tweets` placed in the `Tweet` model, if that tweet doesn't exist then call a create method to save the tweet. I don't know if that way is wrong or not. – Ronny Valdivieso Apr 08 '16 at 00:43

0 Answers0