Class: Logicmonitor::Simple

Inherits:
Object
  • Object
show all
Defined in:
lib/logicmonitor_simple.rb

Instance Method Summary collapse

Constructor Details

#initialize(domain, username, password) ⇒ Simple

Returns a new instance of Simple.



30
31
32
33
34
35
# File 'lib/logicmonitor_simple.rb', line 30

def initialize(domain, username, password)
  @domain   = domain
  @username = username
  @password = password
  @cookies  = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/logicmonitor_simple.rb', line 37

def method_missing(method, *args)
  method = method.to_s.split('_').each_with_index.map { |e,i| i>0 ? e.capitalize : e }.join
  http_args = (args[0] || {}).merge(auth_credentials).to_camelback_keys.map do |k,v|
    if v.is_a? Array
      v = v.join(',')
    end
    "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
  end.join('&')
   
  g = Curl.get("https://#{@domain}.logicmonitor.com/santaba/rpc/#{method}?#{http_args}") do |http|
    if @cookies.size > 0
      http.headers['Cookie'] = @cookies.join('; ')
    end
  end

  if @cookies.empty?
    @cookies = g.header_str.scan(/^Set-Cookie: ([^\r]+)?\r/m)
  end

  if g.status.to_i > 399
    raise "Received unexpected HTTP status #{g.status} Response Body: #{g.body_str}"
  end
  
  MultiJson.decode(g.body_str) or raise Logicmonitor::Simple::Error 'Received invalid JSON from LogicMonitor'
end

Instance Method Details

#auth_credentialsObject



26
27
28
# File 'lib/logicmonitor_simple.rb', line 26

def auth_credentials
  { 'c' => @domain, 'u' => @username, 'p' => @password }
end