Class: Uptrends::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/uptrends/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/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/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/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 Uptrends::ApiError.new(response.parsed_response)
  end
end

.parse(client, response) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/uptrends/base.rb', line 41

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/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/base.rb', line 26

def delete!
  response = @client.class.delete("#{api_url}/#{@guid}")
  self.class.check_error!(response)
end

#to_sObject



53
54
55
56
57
58
59
60
# File 'lib/uptrends/base.rb', line 53

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/base.rb', line 21

def update!
  response = @client.class.put("#{api_url}/#{@guid}", body: gen_request_body)
  self.class.check_error!(response)
end