Class: Graphite::Base

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

Direct Known Subclasses

Graph, Metric

Constant Summary collapse

@@path =
""

Class Method Summary collapse

Class Method Details

.authenticate(user = @@user, password = @@password) ⇒ Object

If the operation needs authentication you have to call this first



19
20
21
22
23
24
25
# File 'lib/graphite/base.rb', line 19

def self.authenticate(user = @@user,password = @@password)
  connection = self.connection
  url = @@path + "/account/login"
  url.gsub!("//","/")
  response = connection.post(url,"nextPage=/&password=#{password}&username=#{user}")
  @@init_header =  {"Cookie" => response.get_fields('Set-Cookie').first}
end

.connectionObject

connection instance (shared with every instance of the class)



9
10
11
12
13
14
15
16
# File 'lib/graphite/base.rb', line 9

def self.connection      
  @init_header ||= {}
  @@connection ||= begin
                     endpoint_uri = URI.parse(@@endpoint)
                     @@path = endpoint_uri.path
                     Net::HTTP.new(endpoint_uri.host, endpoint_uri.port)
                   end
end

.get(path, args) ⇒ Object

Get



34
35
36
37
# File 'lib/graphite/base.rb', line 34

def self.get(path,args)      
  mpath = (@@path + path).gsub("//","/")
  self.connection.get(mpath + "?" + args.map { |i,j| i.to_s + "=" + j }.join("&"),@@init_header)
end

.set_connection(endpoint, user = "", password = "") ⇒ Object



27
28
29
30
31
# File 'lib/graphite/base.rb', line 27

def self.set_connection(endpoint,user = "",password = "")
  @@endpoint = endpoint
  @@user ||= user
  @@password ||= password
end