class UptrendsExtended::Base

Attributes

attributes[R]

Public Class Methods

check_error!(response) click to toggle source
# File lib/uptrends_extended/base.rb, line 31
def self.check_error!(response)
  response_code = response.response.code.to_i
  case response_code
    when 200...300
      response.parsed_response
    else
      raise UptrendsExtended::ApiError.new(response.parsed_response)
  end
end
new(client, response, attributes = {}) click to toggle source
# File lib/uptrends_extended/base.rb, line 10
def initialize(client, response, attributes = {})
  @client     = client
  @attributes = attributes
  gen_and_set_accessors
end
parse(client, response) click to toggle source
# File lib/uptrends_extended/base.rb, line 68
def self.parse(client, response)
  check_error!(response)
  parsed_response = response.parsed_response
  if Array === parsed_response
    parsed_response.map do |item|
      new(client, response, item)
    end
  else
    new(client, response, parsed_response)
  end
end

Public Instance Methods

create!() click to toggle source
# File lib/uptrends_extended/base.rb, line 16
def create!
  response = @client.class.post(api_url, body: gen_request_body)
  self.class.parse(@client, response)
end
delete!() click to toggle source
# File lib/uptrends_extended/base.rb, line 26
def delete!
  response = @client.class.delete("#{api_url}/#{@guid}")
  self.class.check_error!(response)
end
statistics(start_of_period, end_of_period, dimension) click to toggle source

@param [yyyy/mm/dd] start_of_period @param [yyyy/mm/dd] end_of_period @param [Day, Week, Month, Year, ProbeGroup, Probe, Checkpoint, ErrorCode, ErrorLevel] dimension

# File lib/uptrends_extended/base.rb, line 44
def statistics(start_of_period, end_of_period, dimension)
  response = @client.class.get("#{api_url}/#{@guid}/statistics?Start=#{start_of_period}&End=#{end_of_period}&Dimension=#{dimension}", body: gen_request_body)
  self.class.check_error!(response)
end
to_s() click to toggle source
# File lib/uptrends_extended/base.rb, line 80
def to_s
  string = []
  attributes.each do |attr|
    string << "#{attr}: #{self.send(attr)}"
  end

  "#{string.join("\n")}"
end
update!() click to toggle source
# File lib/uptrends_extended/base.rb, line 21
def update!
  response = @client.class.put("#{api_url}/#{@guid}", body: gen_request_body)
  self.class.check_error!(response)
end
uptime_last_12_months() click to toggle source
# File lib/uptrends_extended/base.rb, line 58
def uptime_last_12_months
  start_period = Time.now - 12.months
  stats = statistics(start_period.strftime('%Y/%m/%d'), Time.now.strftime('%Y/%m/%d'), 'Year')
  if stats.blank?
    {sla: 0, uptime: 0} # We return this because the probe has not been tested yet.
  else
    {sla: stats[0]['SLAPercentage'], uptime: stats[0]['PercentageUptime']}
  end
end
uptime_this_year() click to toggle source
# File lib/uptrends_extended/base.rb, line 49
def uptime_this_year
  stats = statistics("#{Time.now.year}/01/01", "#{Time.now.year}/12/31", 'Year')
  if stats.blank?
    {sla: 0, uptime: 0} # We return this because the probe has not been tested yet.
  else
    {sla: stats[0]['SLAPercentage'], uptime: stats[0]['PercentageUptime']}
  end
end

Private Instance Methods

gen_and_set_accessors() click to toggle source

This method sets up all of our attr_accessor so we can easily edit probe attributes

# File lib/uptrends_extended/base.rb, line 92
def gen_and_set_accessors
  attributes = []
  self.attributes.each_pair do |k,v|

    k = k.to_s.underscore
    case k
    when 'guid'
      # setup attr_reader for guid and set it's value.
      self.class.send(:attr_reader, k)
      self.instance_variable_set("@#{k}", v)
    else
      # setup a attr_accessor for all other attributes
      self.class.send(:attr_accessor, k)
      self.send("#{k}=", v)
    end
    attributes << k.to_sym

  end

  @attributes = attributes
end
gen_request_body() click to toggle source
# File lib/uptrends_extended/base.rb, line 114
def gen_request_body
  new_hash = @attributes.inject({}) do |memo,(k,v)|
    if k.to_s.underscore == 'guid'
      memo
    else
      memo[k.to_s.camelize] = self.send(k.to_s.underscore)
      memo
    end
  end

  request_body = JSON.dump(new_hash)
end