Class: SpreeCmCommissioner::ProfileImageUpdater

Inherits:
BaseInteractor show all
Defined in:
app/interactors/spree_cm_commissioner/profile_image_updater.rb

Instance Method Summary collapse

Instance Method Details

#callObject

url: ‘



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/interactors/spree_cm_commissioner/profile_image_updater.rb', line 4

def call
  presigned_url = SpreeCmCommissioner::S3UrlGenerator.s3_presigned_url(context.url)

  response = Faraday.get(presigned_url)

  if response.success?
    user = context.user
    io = StringIO.new(response.body)
    filename = File.basename(context.url)

    profile = user.profile || SpreeCmCommissioner::UserProfile.new(viewable: user)

    profile.attachment.attach(io: io, filename: filename)

    if profile.save
      context.result = profile
    else
      context.fail!(message: profile.errors.full_messages.join(','))
    end
  end
rescue StandardError => e
  context.fail!(message: "Error fetching the remote image: #{e.message}")
end