dphuang2 / PoGoBag

Analyze and share your Pokémon through a locally run server! Official Site access has been stopped as of 12/23/2016
146 stars 49 forks source link

Force refresh #12

Closed amallah closed 8 years ago

amallah commented 8 years ago

What is the method to force a refresh of server data from local?

dphuang2 commented 8 years ago

This is the function: https://github.com/dphuang2/PoGoBag/blob/master/app/helpers/sessions_helper.rb

  def refresh_data(user)
    auth_objects = authorized_client(user.refresh_token, 'refresh_token')
    client = auth_objects[:client]
    user = setup_client_user_pair(client, user.refresh_token)
    store_data(client, user)
  end

Before it was being done automatically through crontabs by checking if the saved refresh token can still be used and then running refresh_data: https://github.com/dphuang2/PoGoBag/blob/master/lib/tasks/refresh_data.rake

desc 'update all user data with google accounts'
task refresh_data: :environment do
  include SessionsHelper
  Poke::API::Logging.log_level = :WARN if Rails.env.production?
  Rails.logger.warn "Running rake task"
  @users = User.where.not('refresh_token' => nil)
  @users.each do |user|
    if user.access_token_expire_time > Time.now.to_i
      refresh_data(user)
    end
  end
end