Home > Mobile >  Rails 7 RestClient::Not found in find_each loop updating avatar from Microsoft Graph
Rails 7 RestClient::Not found in find_each loop updating avatar from Microsoft Graph

Time:09-01

I'm trying to attach all pictures from the Microsoft Graph to my new Rails application using active storage and the rest-client gem.

It works for a single user I do it like this:

User.find_by_email("[email protected]).avatar.attach io:StringIO.open(image.to_blob), filename: "avatar.jpg", content_type: metadata["mimeType"], identify: false

But in a batch loop, it doesn't work.

class RestController < ApplicationController
    require 'rest-client'     

    def sync_azure_picture
        @token = RestController.get_azure_token

        User.find_each do |currentUser|
            request_url = 'https://graph.microsoft.com/v1.0/users/' currentUser[:id_azure] '/photo/$value'

            puts request_url

            resp = RestClient.get(request_url,'Authorization' => @token)

            image = MiniMagick::Image.read(resp.body)
            metadata = image.data

            currentUser.avatar.attach io:StringIO.open(image.to_blob), filename: "avatar.jpg", content_type: metadata["mimeType"], identify: false
        end
    end
end`

The error i'm getting is

RestClient::NotFound

CodePudding user response:

Probably not all users have attachments? You can log errors and then check these urls

User.find_each do |user|
  # ...
rescue RestClient::NotFound => e
  Rails.logger.error("#{request_url}:")
  Rails.logger.error(e)
end
  • Related