Class: Uptrends::ApiClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ApiClient

Returns a new instance of ApiClient.



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

def initialize(options = {})
  @username = options[:username] ? options[:username] : fail("You must specify the :username option")
  password  = options[:password] ? options[: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.



12
13
14
# File 'lib/uptrends/api_client.rb', line 12

def username
  @username
end

Instance Method Details

#add_probe_to_group(options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/uptrends/api_client.rb', line 74

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

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

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


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

#create_http_probe(options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/uptrends/api_client.rb', line 99

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

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

  @probes ||= get_probes
  @probes << new_probe
end

#delete_probe(probe) ⇒ Object



92
93
94
95
96
97
# File 'lib/uptrends/api_client.rb', line 92

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

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

#get_probe_group_members(options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/uptrends/api_client.rb', line 62

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

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

#probe_groupsObject



30
31
32
# File 'lib/uptrends/api_client.rb', line 30

def probe_groups
  @probe_groups ||= get_probe_groups
end

#probesObject



26
27
28
# File 'lib/uptrends/api_client.rb', line 26

def probes
  @probes ||= get_probes
end

#update_probe(probe) ⇒ Object



88
89
90
# File 'lib/uptrends/api_client.rb', line 88

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