20140525150140_migrate_agents_to_service_authentication.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. class MigrateAgentsToServiceAuthentication < ActiveRecord::Migration[4.2]
  2. def twitter_consumer_key(agent)
  3. agent.options['consumer_key'].presence || agent.credential('twitter_consumer_key')
  4. end
  5. def twitter_consumer_secret(agent)
  6. agent.options['consumer_secret'].presence || agent.credential('twitter_consumer_secret')
  7. end
  8. def twitter_oauth_token(agent)
  9. agent.options['oauth_token'].presence || agent.options['access_key'].presence || agent.credential('twitter_oauth_token')
  10. end
  11. def twitter_oauth_token_secret(agent)
  12. agent.options['oauth_token_secret'].presence || agent.options['access_secret'].presence || agent.credential('twitter_oauth_token_secret')
  13. end
  14. def up
  15. agents = Agent.where(type: ['Agents::TwitterUserAgent', 'Agents::TwitterStreamAgent', 'Agents::TwitterPublishAgent']).each do |agent|
  16. service = agent.user.services.create!(
  17. provider: 'twitter',
  18. name: "Migrated '#{agent.name}'",
  19. token: twitter_oauth_token(agent),
  20. secret: twitter_oauth_token_secret(agent)
  21. )
  22. agent.service_id = service.id
  23. agent.save!(validate: false)
  24. end
  25. migrated = false
  26. if agents.length > 0
  27. puts <<-EOF.strip_heredoc
  28. Your Twitter agents were successfully migrated. You need to update your .env file and add the following two lines:
  29. TWITTER_OAUTH_KEY=#{twitter_consumer_key(agents.first)}
  30. TWITTER_OAUTH_SECRET=#{twitter_consumer_secret(agents.first)}
  31. To authenticate new accounts with your twitter OAuth application you need to log in the to twitter application management page (https://apps.twitter.com/)
  32. and set the callback URL of your application to "http#{ENV['FORCE_SSL'] == 'true' ? 's' : ''}://#{ENV['DOMAIN']}/auth/twitter/callback"
  33. EOF
  34. migrated = true
  35. end
  36. sleep 20 if migrated
  37. end
  38. def down
  39. raise ActiveRecord::IrreversibleMigration, "Cannot revert migration to OAuth services"
  40. end
  41. end