Reputation: 6981
I'm enjoying a very interesting problem where File.write...
only works occasionally, i.e., seemingly when it's at the end of the method. For some weird reason this works:
def update(id)
r = HTTParty.get("#{user_api_url}/#{id}?token=#{token}").parsed_response
file_path = "/Users/#{server_user}/server/resources/users/"
File.write("#{file_path}#{id}#{extension(r['user_file_name'])}", open("#{r['user_url']}").read, { mode: 'wb' })
File.write("#{file_path}#{id}_logo#{extension(r['logo_file_name'])}", open("#{r['user_logo_url']}").read, { mode: 'wb' })
end
And this doesn't:
def update(id)
r = HTTParty.get("#{user_api_url}/#{id}?token=#{token}").parsed_response
file_path = "/Users/#{server_user}/server/resources/users/"
r['shared_resources'].map do |key, value|
file = "#{file_path}shared_resources/#{value.split('/')[-1]}"
p "#{timestamp}: Saving #{key} from #{api_server}#{value} into #{file}"
File.write(file, open("#{api_server}#{value}").read, mode: 'wb')
end
File.write("#{file_path}#{id}#{extension(r['user_file_name'])}", open("#{r['user_url']}").read, mode: 'wb')
File.write("#{file_path}#{id}_logo#{extension(r['logo_file_name'])}", open("#{r['user_logo_url']}").read, mode: 'wb')
end
The second method doesn't generate any errors at all, but no files are written. The paths and the URL are both correct. Makes me think I'm not opening or closing something correctly but I don't know what. Any ideas?
UPDATE
Getting this error:
Errno::ENOENT: No such file or directory @ rb_sysopen - /Users/user123/server/resources/users/shared_resources/cqap_logo_cmyk-6a82ebd2e336c92188a58cacf26792cf9f43b6d296ae51c2b3fe...
Which is from File.write in the loop.
UPDATE 2
The r['shared_resources']
outputs this:
{\"logo\"=>\"/assets/server/user123-6a82ebd2e336c92188a58cacf26792cf9f43b6d296ae51c2b3fe05a0c1802794.jpg\"}"
But nothing in the loop seems to do anything.
Upvotes: 0
Views: 369
Reputation: 120990
It looks like shared_resources
folder does not exist. First snippet writes directly in existing file_path
, while second one tries to write into subfolder. Put the following right after you have file_path
defined:
Dir.mkdir File.join file_path, 'shared_resources' # unless exists?
Upvotes: 1