Class: Bricks::Errbit

Inherits:
Base
  • Object
show all
Defined in:
lib/active_admin/generator/bricks/errbit.rb

Instance Attribute Summary

Attributes inherited from Base

#base_path, #context

Instance Method Summary collapse

Methods inherited from Base

#ask, #choose, #commit_all, #format, #initialize, #say, #template, #title, #yes?

Constructor Details

This class inherits a constructor from Bricks::Base

Instance Method Details

#abs_path(path) ⇒ Object


89
90
91
# File 'lib/active_admin/generator/bricks/errbit.rb', line 89

def abs_path(path)
  @errbit_url + path
end

#agentObject


45
46
47
# File 'lib/active_admin/generator/bricks/errbit.rb', line 45

def agent
  @agent ||= Mechanize.new
end

#apply?Boolean

Returns:

  • (Boolean)

7
8
9
# File 'lib/active_admin/generator/bricks/errbit.rb', line 7

def apply?
  yes? "Do you want to configure Errbit?"
end

#ask_name_and_collaborators(force = false) ⇒ Object


40
41
42
43
# File 'lib/active_admin/generator/bricks/errbit.rb', line 40

def ask_name_and_collaborators(force = false)
  @app_name = ENV['APP_NAME']
  @collaborators = ( ENV['ERRBIT_COLLABORATORS'] || ask("Specify collaborators' User IDs:") ).split(/\s*,\s*/).map(&:strip)
end

#before_bundleObject


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_admin/generator/bricks/errbit.rb', line 11

def before_bundle
  gem 'airbrake'

  ask_name_and_collaborators

  @errbit_url = ENV['ERRBIT_URL'] || ask("Errbit URL")
  uri = URI(@errbit_url)

  @errbit_host = uri.host
  @errbit_port = uri.scheme == "https" ? 443 : 80

  @errbit_email = ENV['ERRBIT_EMAIL'] || ask("Errbit email")
  @errbit_password = ENV['ERRBIT_PASSWORD'] || ask("Errbit password")

  login!
  @api_key = find_app_key

  while @api_key.blank?
    begin
      @api_key = create_app_key
    rescue RuntimeError => e
      say e.message
      ask_name_and_collaborators
    end
  end

  template "config/initializers/errbit.rb"
end

#create_app_keyObject


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/active_admin/generator/bricks/errbit.rb', line 70

def create_app_key
  new_page = agent.get(abs_path('/apps/new'))
  result_page = new_page.form_with(action: '/apps') do |f|
    f["app[name]"] = @app_name
    f["app[notify_on_errs]"] = "1"
    @collaborators.each_with_index do |uid, i|
      f["app[watchers_attributes][#{i}][user_id]"] = uid
    end
  end.submit

  api_key = fetch_api_key(result_page)

  if api_key.nil?
    fail result_page.search(".error-messages ul li").map(&:text).join("\n")
  end

  api_key
end

#fetch_api_key(page) ⇒ Object


58
59
60
61
62
# File 'lib/active_admin/generator/bricks/errbit.rb', line 58

def fetch_api_key(page)
  page.search("span.meta").text.gsub(/^.*API Key:/m, '').strip
rescue
  nil
end

#find_app_keyObject


64
65
66
67
68
# File 'lib/active_admin/generator/bricks/errbit.rb', line 64

def find_app_key
  page = agent.get(abs_path('/apps'))
  link = page.search("td.name a").find { |link| link.text == @app_name }
  link.present? ? fetch_api_key(agent.get(link["href"])) : nil
end

#login!Object


49
50
51
52
53
54
55
56
# File 'lib/active_admin/generator/bricks/errbit.rb', line 49

def login!
  agent.get(abs_path('/users/sign_in')) do |page|
    page.form_with(action: '/users/sign_in') do |f|
      f["user[email]"] = @errbit_email
      f["user[password]"] = @errbit_password
    end.submit
  end
end