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.



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

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.



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

def username
  @username
end

Instance Method Details

#add_probe_to_group(options = {}) ⇒ Object



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

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

url, match_pattern = nil)



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/uptrends/api_client.rb', line 88

def create_http_probe(options = {}) #url, match_pattern = nil)
  base_hash = {"Name"=>"", "URL"=>"", "CheckFrequency"=>5, "IsActive"=>true, "GenerateAlert"=>true, "Notes"=>"", "PerformanceLimit1"=>60000, "PerformanceLimit2"=>60000, "ErrorOnLimit1"=>false, "ErrorOnLimit2"=>false, "MinBytes"=>0, "ErrorOnMinBytes"=>false, "Timeout"=>30000, "TcpConnectTimeout"=>10000, "MatchPattern"=>"", "DnsLookupMode"=>"Local", "UserAgent"=>"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;)", "UserName"=>"", "Password"=>"", "IsCompetitor"=>false, "Checkpoints"=>"", "HttpMethod"=>"Get", "PostData"=>""}

  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] ? options[:match_pattern] : nil

  if url =~ %r{^https:}i
    base_hash.merge!({"Name"=>name, "URL"=>url, "ProbeType"=>"Https", "Port"=>443})
  elsif url =~ %r{^http:}i
    base_hash.merge!({"Name"=>name, "URL"=>url, "ProbeType"=>"Http", "Port"=>80})
  else
    fail("The URL you provided didn't start with http or https!")
  end

  base_hash.merge!({"MatchPattern"=>match_pattern}) unless match_pattern.nil?

  p = Uptrends::Probe.new(base_hash)
  response = self.class.post("/probes", body: p.gen_request_body)
  new_probe = Uptrends::Probe.new(response.parsed_response)

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

#delete_probe(probe) ⇒ Object



81
82
83
84
85
86
# File 'lib/uptrends/api_client.rb', line 81

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



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

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



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

def probe_groups
  @probe_groups ||= get_probe_groups
end

#probesObject



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

def probes
  @probes ||= get_probes
end

#update_probe(probe) ⇒ Object



77
78
79
# File 'lib/uptrends/api_client.rb', line 77

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