Class: Graphite::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/graphite/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/graphite/client.rb', line 6

def initialize(options={})
  default_options = {}
  default_options[:ssl] = {
    verify: true,
    ca_file: File.expand_path('../cacerts.pem', __FILE__)
  }
  basic_auth = options.delete(:basic_auth)
  @connection = Faraday.new(default_options.merge(options))
  if basic_auth.present?
    @connection.basic_auth(basic_auth[:user],basic_auth[:password])
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



4
5
6
# File 'lib/graphite/client.rb', line 4

def connection
  @connection
end

Instance Method Details

#find_metric(metric) ⇒ Object



21
22
23
# File 'lib/graphite/client.rb', line 21

def find_metric(metric)
  connection.get('/metrics/find',{ query: metric })
end

#metric_exists?(metric) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
# File 'lib/graphite/client.rb', line 24

def metric_exists?(metric)
  exists = false
  response = find_metric(metric)
  # Does this need to handle redirects?
  if response.status == 200 && response.headers['content-type']=='application/json'
    json = JSON.parse(response.body)
    exists = true unless json.empty?
  end
  exists
end

#reachable?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
# File 'lib/graphite/client.rb', line 34

def reachable?
  reachable = false
  begin
    response = connection.get('/render')
    reachable = response.status==200 && response.headers['content-type']=='image/png' && response.headers['content-length'].to_i > 0
  rescue Exception => e
  end
  reachable
end

#render(params = {}) ⇒ Object



18
19
20
# File 'lib/graphite/client.rb', line 18

def render(params={})
  connection.get('/render',params)
end