Class: Duodealer::RotateDuodealerTokenJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
lib/generators/duodealer_app/rotate_duodealer_token_job/templates/rotate_duodealer_token_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(params) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/generators/duodealer_app/rotate_duodealer_token_job/templates/rotate_duodealer_token_job.rb', line 3

def perform(params)
  @account = Account.find_by(duodealer_domain: params[:account_domain])
  return unless @account

  config = DuodealerApp.configuration
  uri = URI("https://#{@account.duodealer_domain}/admin/oauth/access_token")
  post_data = {
    client_id: config.api_key,
    client_secret: config.secret,
    refresh_token: params[:refresh_token],
    access_token: @account.duodealer_token,
  }

  @response = Net::HTTP.post_form(uri, post_data)
  return log_error(response_exception_error_message) unless @response.is_a?(Net::HTTPSuccess)

  access_token = JSON.parse(@response.body)['access_token']
  return log_error(no_access_token_error_message) unless access_token

  @account.update(duodealer_token: access_token)
end