Class: Uptrends::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/uptrends/utils.rb

Class Method Summary collapse

Class Method Details

.gen_and_set_accessors(object) ⇒ Object

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/uptrends/utils.rb', line 29

def self.gen_and_set_accessors(object)
  attributes = []
  object.original_hash.each_pair do |k,v|

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

  end
  object.instance_variable_set(:@attributes, attributes)
end

.gen_request_body(object) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/uptrends/utils.rb', line 6

def self.gen_request_body(object)
  new_hash = object.original_hash.inject({}) do |memo,(k,v)|
    if k.to_s.underscore == 'guid'
      memo
    else
      memo[k.to_s.camelize] = object.send(k.to_s.underscore)
      memo
    end
  end

  request_body = JSON.dump(new_hash)
end

.to_s(object) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/uptrends/utils.rb', line 19

def self.to_s(object)
  string = []
  object.attributes.each do |attr|
    string << "#{attr}: #{object.send(attr)}"
  end

  "#{string.join("\n")}"
end