Class: Scout::Account

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/scout_api/account.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_param, username, password) ⇒ Account

Returns a new instance of Account.



9
10
11
12
# File 'lib/scout_api/account.rb', line 9

def initialize(, username, password)
  self.class.param = 
  self.class.basic_auth username, password
end

Class Method Details

.get(uri) ⇒ HTTParty::Response

Wraps GET requests with error handling, attaches to the authenticated account, and adds the library version as a parameter.

Checks for errors via the HTTP status code. If an error is found, a Scout::Error is raised. Otherwise, the response.

Returns:

  • (HTTParty::Response)

Raises:



58
59
60
61
62
63
64
65
# File 'lib/scout_api/account.rb', line 58

def self.get(uri)
  raise Scout::Error, 
        "Authentication is required (scout = Scout::Account.new('youraccountname', '[email protected]', 'sekret'))" if param.nil?
  uri = "/#{param}" + uri + (uri.include?('?') ? '&' : '?') + "api_version=#{Scout::VERSION}"
  #puts "GET: #{uri}"
  response = http_get(uri)
  response.code.to_s =~ /^(4|5)/ ? raise( Scout::Error,response.message) : response
end

.http_getObject



48
# File 'lib/scout_api/account.rb', line 48

alias_method :http_get, :get

Instance Method Details

#alertsArray

Recent alerts across all servers on this account

Returns:



17
18
19
20
# File 'lib/scout_api/account.rb', line 17

def alerts
  response = self.class.get("/activities.xml")
  response['alerts'] ? response['alerts'].map { |alert| Scout::Alert.new(alert) } : Array.new
end

#peopleObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/scout_api/account.rb', line 22

def people
  response = self.class.get("/account_memberships")
  doc = Nokogiri::HTML(response.body)

  tables = doc.css('table.list')
  # first table is pending
  # second is active
  active_table = tables.last
  user_rows = active_table.css('tr')[1..-1] # skip first row, which is headings

  user_rows.map do |row|
    name_td, email_td, admin_td, links_td = *row.css('td')

    name = name_td.content.gsub(/[\t\n]/, '')
    email = email_td.content.gsub(/[\t\n]/, '')
    admin = admin_td.content.gsub(/[\t\n]/, '') == 'Yes'
    id = if links_td.inner_html =~ %r{/#{self.class.param}/account_memberships/(\d+)}
           $1.to_i
         end

    Scout::Person.new :id => id, :name => name, :email => email, :admin => admin
  end

end