Class: Uptrends::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/uptrends/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/uptrends/client.rb', line 15

def initialize(opts = {})
  @username = opts[:username] ? opts[:username] : fail("You must specify the :username option")
  password  = opts[:password] ? opts[:password] : fail("You must specify the :password option")

  # This makes it so that every request uses basic auth
  self.class.basic_auth(@username, password)
  # This makes it so that every request uses ?format=json
  self.class.default_params({format: 'json'})
  # This makes it so that every request uses ?format=json
  self.class.headers({'Content-Type' => 'application/json', 'Accept' => 'application/json'})
end

Instance Attribute Details

#usernameObject (readonly)

Returns the value of attribute username.



13
14
15
# File 'lib/uptrends/client.rb', line 13

def username
  @username
end

Instance Method Details

#add_probe(opts = {}) ⇒ Object



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

def add_probe(opts = {})
  probe = Uptrends::Probe.new(opts)
  res  = self.class.post("/probes", body: Uptrends::Utils.gen_request_body(probe))
  parsed_response = raise_or_return(res)
  new_probe = Uptrends::Probe.new(parsed_response)

  probes << new_probe

  new_probe
end

#add_probe_to_group(opts = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/uptrends/client.rb', line 63

def add_probe_to_group(opts = {})
  probe = opts[:probe]
  group = opts[:group]

  fail("You must pass a probe and probe group using probe: and group: opts.") unless Uptrends::Probe === probe && Uptrends::ProbeGroup === group

  probe_guid = opts[:probe].guid ? opts[:probe].guid : fail("The probe you passed does not have a guid.")
  group_guid = opts[:group].guid ? opts[:group].guid : fail("The probe group you passed does not have a guid.")


  post_body = JSON.dump({"ProbeGuid" => probe_guid})
  res = self.class.post("/probegroups/#{group_guid}/members", body: post_body)
  raise_or_return(res)
end

#create_http_probe(opts = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/uptrends/client.rb', line 90

def create_http_probe(opts = {})
  name            = opts[:name]            ? opts[:name]            : fail("You must provide a name!")
  url             = opts[:url]             ? opts[:url]             : fail("You must provide a URL!")
  check_frequency = opts[:check_frequency] ? opts[:check_frequency] : 15
  match_pattern   = opts[:match_pattern]

  probe     = Uptrends::Probe.new(gen_new_probe_hash(name, url, check_frequency, match_pattern))
  res  = self.class.post("/probes", body: Uptrends::Utils.gen_request_body(probe))
  parsed_response = raise_or_return(res)
  new_probe = Uptrends::Probe.new(parsed_response)

  probes << new_probe

  new_probe
end

#delete_probe(probe) ⇒ Object



83
84
85
86
87
88
# File 'lib/uptrends/client.rb', line 83

def delete_probe(probe)
  res = self.class.delete("/probes/#{probe.guid}")
  raise_or_return(res)

  probes.delete_if { |x| x.guid == probe.guid }
end

#get_probe_group_members(opts = {}) ⇒ Object



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

def get_probe_group_members(opts = {})
  group = opts[:group]
  fail("You must pass a probe group using group: option.") unless Uptrends::ProbeGroup === group
  group_guid = opts[:group].guid ? opts[:group].guid : fail("The probe group you passed does not have a guid.")

  res = self.class.get("/probegroups/#{group_guid}/members")
  parsed_response = raise_or_return(res)
  probe_group_members = parsed_response.inject([]) do |memo, x|
    memo << Uptrends::Probe.new(x)
    memo
  end
end

#probe_groupsObject



35
36
37
# File 'lib/uptrends/client.rb', line 35

def probe_groups
  @probe_groups ||= get_probe_groups
end

#probes(opts = {}) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/uptrends/client.rb', line 27

def probes(opts = {})
  if opts[:refresh]
    @probes = get_probes
  else
    @probes ||= get_probes
  end
end

#update_probe(probe) ⇒ Object



78
79
80
81
# File 'lib/uptrends/client.rb', line 78

def update_probe(probe)
  res = self.class.put("/probes/#{probe.guid}", body: Uptrends::Utils.gen_request_body(probe))
  raise_or_return(res)
end