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.



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

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

Class Method Details

.allObject



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

def self.all
  response=get("/accounts_for_user.json", :exclude_param=>true)
  (response.first && response.first['account']) ? response.map{|h| Hashie::Mash.new(h['account'])} : []
end

.get(uri, options = {}) ⇒ 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:



62
63
64
65
66
67
68
69
70
# File 'lib/scout_api/account.rb', line 62

def self.get(uri, options={})
  raise Scout::Error, 
        "Authentication is required (scout = Scout::Account.new('youraccountname', '[email protected]', 'sekret'))" if param.nil?

  uri = (options[:exclude_param] ? "" : "/#{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['error'] ? response['error']['message'] : response.message) : response
end

.http_getObject



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

alias_method :http_get, :get

Instance Method Details

#alertsArray

Recent alerts across all servers on this account

Returns:



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

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

#peopleObject



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

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