2019-08-14 09:48:44 +00:00
|
|
|
class AddPicToInboxMigration < ActiveRecord::Migration[5.0]
|
|
|
|
def change
|
|
|
|
FacebookPage.find_each do |inbox|
|
|
|
|
begin
|
2019-10-20 08:47:26 +00:00
|
|
|
url = 'http://graph.facebook.com/' << inbox.page_id << '/picture?type=large'
|
2019-08-14 09:48:44 +00:00
|
|
|
uri = URI.parse(url)
|
|
|
|
tries = 3
|
2019-10-20 08:47:26 +00:00
|
|
|
begin
|
|
|
|
response = uri.open(redirect: false)
|
|
|
|
rescue OpenURI::HTTPRedirect => e
|
|
|
|
uri = e.uri # assigned from the "Location" response header
|
|
|
|
retry if (tries -= 1) > 0
|
|
|
|
raise
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
pic_url = response.base_uri.to_s
|
|
|
|
puts pic_url.inspect
|
2019-10-20 08:47:26 +00:00
|
|
|
rescue StandardError => e
|
2019-08-14 09:48:44 +00:00
|
|
|
pic_url = nil
|
|
|
|
end
|
2019-10-20 08:47:26 +00:00
|
|
|
inbox.remote_avatar_url = pic_url
|
|
|
|
inbox.save!
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|