# -*- encoding: utf-8 -*- require "papertrail-provisioner/version" module Papertrail class Provisioner require 'uri' require 'net/http' require 'json' require 'capybara' require 'capybara/dsl' require 'capybara/poltergeist' Capybara.register_driver :poltergeist do |app| Capybara::Poltergeist::Driver.new( lambda{|e|}, :js_errors => false, :phantomjs_options => ['--load-images=no', '--ignore-ssl-errors=yes'] ) end Capybara.current_driver = :poltergeist Capybara.app_host = 'https://papertrailapp.com' Capybara.run_server = false include Capybara::DSL def initialize(email, password) @account_email = email @account_password = password end def login! visit('/sessions/new') click_on('Sign In') fill_in('Email', :with => @account_email) fill_in('Password', :with => @account_password) click_on('Login') unless current_path == '/dashboard' or current_path == '/start' raise "Failed to login" end @logged_in = true end def add_heroku_system(name) uri = URI("https://papertrailapp.com/api/v1/systems/#{name}.json") Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http| request = Net::HTTP::Get.new uri.request_uri request.basic_auth @account_email, @account_password response = http.request request if response.code == '200' body = JSON.load(response.body) if syslog = body['syslog'] return "#{syslog["hostname"]}:#{syslog["port"]}" end end end login! unless @logged_in visit('/dashboard') unless current_path == '/dashboard' raise "Failed to visit the dashboard" end click_on('Add Systems') click_on('Alternatives') unless current_path == '/systems/new' raise 'Failed to load new system page' end find('li[data-formid="form2"]').click within('div#form2') do fill_in('What should we call it?', :with => name) click_on('Save →') end unless current_path == "/systems/#{name}/events" raise "Failed to create system" end find('div.quick-help').find('strong').text end end end