|
| 1 | +class ErrplaneGenerator < Rails::Generator::Base |
| 2 | + def add_options!(option) |
| 3 | + option.on("-k", "--api-key=API_KEY", String, "API key for your Errplane organization") {|v| options[:api_key] = v} |
| 4 | + option.on("-a", "--application-id=APP_ID", String, "Your Errplane application id (optional)") {|v| options[:application_id] = v} |
| 5 | + end |
| 6 | + |
| 7 | + def manifest |
| 8 | + if options[:api_key].blank? |
| 9 | + puts "You must provide an API key using -k or --api-key." |
| 10 | + exit |
| 11 | + end |
| 12 | + |
| 13 | + begin |
| 14 | + puts "Contacting Errplane API" |
| 15 | + application_name = "ApplicationName" |
| 16 | + api_key = options[:api_key] |
| 17 | + |
| 18 | + connection = Net::HTTP.new("errplane.com", 443) |
| 19 | + connection.use_ssl = true |
| 20 | + connection.verify_mode = OpenSSL::SSL::VERIFY_NONE |
| 21 | + url = "/api/v1/applications?api_key=#{api_key}&name=#{application_name}" |
| 22 | + response = connection.post(url, nil) |
| 23 | + |
| 24 | + @application = JSON.parse(response.body) |
| 25 | + |
| 26 | + unless response.is_a?(Net::HTTPSuccess) |
| 27 | + raise "The Errplane API returned an error: #{response.inspect}" |
| 28 | + end |
| 29 | + rescue => e |
| 30 | + puts "We ran into a problem creating your application via the API!" |
| 31 | + puts "If this issue persists, contact us at [email protected] with the following details:" |
| 32 | + puts "API Key: #{e.class}: #{options[:api_key]}" |
| 33 | + puts "#{e.class}: #{e.message}" |
| 34 | + end |
| 35 | + |
| 36 | + record do |m| |
| 37 | + m.template "initializer.rb", "config/initializers/errplane.rb", |
| 38 | + :assigns => { |
| 39 | + :application_id => options[:application_id] || @application["key"] || secure_random.hex(4), |
| 40 | + :api_key => options[:api_key] |
| 41 | + } |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + def secure_random |
| 46 | + defined?(SecureRandom) ? SecureRandom : ActiveSupport::SecureRandom |
| 47 | + end |
| 48 | +end |
0 commit comments