Class: UsersCheckJob

Inherits:
ApplicationJob show all
Defined in:
app/jobs/users_check_job.rb

Overview

This job contain the methods for sync users with REST-API json

Instance Method Summary collapse

Instance Method Details

#perform(username: '') ⇒ Object

get users data from remote api. For each user received run #set_data



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/jobs/users_check_job.rb', line 10

def perform(username: '')
  return if Rails.env.test?

  users = User.all.order(label: :asc)
  users = users.where(username: username) if username.present?
  uri = URI.parse(Settings.api.url)
  opts = {
    http_basic_authentication: [
      Rails.application.credentials.api[:user] || Settings.api.username.to_s,
      Rails.application.credentials.api[:secret_access_key] || Settings.api.secret_access_key.to_s
    ],
    ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE
  }
  users.each do |user|
    next if user.username.blank?

    uri.query = "login=#{user.username}&struttura=true"
    json_data = URI.parse(uri.to_s).open(opts).read
    user_data = JSON.parse(json_data)
    set_data(user, user_data)
    uri.query = ''
  end
end

#set_data(user, data = {}) ⇒ Boolean

update a user with api data

Parameters:

  • user (Object)

    istance of user to update

  • data (Hash) (defaults to: {})

    all user’s data from the api. Default: {}

Returns:

  • (Boolean)

    true if user is updated



38
39
40
41
42
43
44
45
46
47
# File 'app/jobs/users_check_job.rb', line 38

def set_data(user, data = {})
  return if data.blank?

  user.label          = data['nominativo'].presence || [user.username.split('.').second, user.username.split('.').first].join(' ').titleize
  user.email          = data['email']
  user.structure      = data['assegnazione']
  user.responsabile   = data['struttura']['ufficio']['responsabile']['nominativi']
  user.locked_at      = Time.zone.today if !user.locked_at? && data['stato'] == 'scaduto'
  user.save
end