class Expri::Metric

Constants

ATTRIBUTE_DEFAULTS
VALID_ATTRIBUTES

Attributes

attributes[RW]
name[RW]
options[RW]

Public Class Methods

list(options={}) click to toggle source
# File lib/expri/metric.rb, line 9
def self.list(options={})
  metric = Metric.new(nil, {}, {})
  MultiJson.decode(metric.api.get(path: "/metrics", expects: 200).body)
end
new(name, attributes={}, options={}) click to toggle source
# File lib/expri/metric.rb, line 14
def initialize(name, attributes={}, options={})
  @name           = name
  @options        = options
  self.attributes = attributes
end

Public Instance Methods

==(other) click to toggle source
# File lib/expri/metric.rb, line 20
def ==(other)
  attributes_without_defaults == other.attributes_without_defaults
end
api() click to toggle source
# File lib/expri/metric.rb, line 24
def api
  @api ||= Excon.new(api_url)
end
attributes=(attributes) click to toggle source
# File lib/expri/metric.rb, line 28
def attributes=(attributes)
  # symbolize keys
  attributes.keys.each do |key|
    attributes[(key.to_sym rescue key) || key] = attributes.delete(key)
  end

  attributes.delete_if { |k, v| !VALID_ATTRIBUTES.include?(k) }
  attributes.delete_if { |k, v| v == nil }

  # manual normalization
  attributes[:type] = attributes[:type].to_sym if attributes[:type]

  @attributes = attributes
end
attributes_without_defaults() click to toggle source

only the attributes that are not at their default value

# File lib/expri/metric.rb, line 44
def attributes_without_defaults
  attributes = @attributes.dup
  attributes.delete_if { |k, v| ATTRIBUTE_DEFAULTS[k] == v }
end
destroy() click to toggle source
# File lib/expri/metric.rb, line 49
def destroy
  puts "destroy name=#{name}" if @options[:verbose]
  api.delete(path: "/metrics/#{name}", body: MultiJson.encode(@attributes),
    expects: 200)
end
get() click to toggle source
# File lib/expri/metric.rb, line 55
def get
  puts "show name=#{name}" if @options[:verbose]
  self.attributes =
    MultiJson.decode(api.get(path: "/metrics/#{name}", expects: 200).body)
end
post() click to toggle source
# File lib/expri/metric.rb, line 61
def post
  puts "create name=#{name}" if @options[:verbose]
  # exprd's put is like most api's post
  api.put(path: "/metrics/#{name}", body: MultiJson.encode(@attributes),
    expects: 200)
rescue Excon::Errors::BadRequest => e
  message = MultiJson.decode(e.response.body)["message"]
  raise(message)
end
put() click to toggle source
# File lib/expri/metric.rb, line 71
def put
  begin
    existing = Metric.new(name, {}, @options)
    existing.get
    if self != existing
      existing.destroy
      post
    end
  rescue Excon::Errors::NotFound
    post
  end
end

Private Instance Methods

api_url() click to toggle source
# File lib/expri/metric.rb, line 86
def api_url
  @options[:exprd_api_url] || ENV["EXPRD_API_URL"] ||
    raise("need EXPRD_API")
end