Class: UptrendsExtended::Base

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

Direct Known Subclasses

Checkpoint, Probe, ProbeGroup

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, response, attributes = {}) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
# File 'lib/uptrends_extended/base.rb', line 10

def initialize(client, response, attributes = {})
  @client     = client
  @attributes = attributes
  gen_and_set_accessors
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



8
9
10
# File 'lib/uptrends_extended/base.rb', line 8

def attributes
  @attributes
end

Class Method Details

.check_error!(response) ⇒ Object



31
32
33
34
35
36
37
38
39
# 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

.parse(client, response) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# 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

Instance Method Details

#create!Object



16
17
18
19
# 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!Object



26
27
28
29
# 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) ⇒ Object

Parameters:

  • start_of_period (yyyy/mm/dd)
  • end_of_period (yyyy/mm/dd)
  • dimension (Day, Week, Month, Year, ProbeGroup, Probe, Checkpoint, ErrorCode, ErrorLevel)


44
45
46
47
# 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_sObject



80
81
82
83
84
85
86
87
# 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!Object



21
22
23
24
# 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_monthsObject



58
59
60
61
62
63
64
65
66
# 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_yearObject



49
50
51
52
53
54
55
56
# 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