module GraphiteDashboardApi::Api

Public Instance Methods

exists?(uri, name = nil) click to toggle source
# File lib/graphite-dashboard-api/api.rb, line 24
def exists?(uri, name = nil)
  pattern = name || @name
  search(uri, pattern).map { |el| el['name'] }.include? (pattern)
end
load!(uri, name = nil) click to toggle source
# File lib/graphite-dashboard-api/api.rb, line 14
def load!(uri, name = nil)
  response = rest_request(uri, "load/#{name || @name}", :get)
  self.from_hash!(response)
end
save!(uri) click to toggle source

this mixin requires a from_hash! and to_hash methods + @name

# File lib/graphite-dashboard-api/api.rb, line 8
def save!(uri)
  data = encode
  response = rest_request(uri, "save/#{@name}", :post, data)
  response
end

Private Instance Methods

rest_request(uri, endpoint, method, payload = nil) click to toggle source
# File lib/graphite-dashboard-api/api.rb, line 30
def rest_request(uri, endpoint, method, payload = nil)
  uri = "#{uri}/dashboard/#{endpoint}"
  begin
    r = RestClient::Request.new(
      url: uri,
      payload: payload,
      method: method,
      timeout: 2,
      open_timeout: 2,
      headers: {
        accept: :json,
        content_type: :json,
      }
    )
    response = r.execute
  rescue => e
    raise "Rest client error: #{e}"
  end
  if response.code != 200 || JSON.parse(response.body)['error']
    fail "Error calling dashboard API: #{response.code} #{response.body}"
  end
  JSON.parse(response.body)
end