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.



25
26
27
28
29
30
31
# File 'lib/logicmonitor_simple.rb', line 25

def initialize(domain, username, password)
    @domain   = domain
    @username = username
    @password = password
    @cookies  = []
    self.({ 'c' => @domain, 'u' => @username, 'p' => @password })
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/logicmonitor_simple.rb', line 33

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].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('&') rescue ""
    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