Class: Uptrends::Client
- Inherits:
-
Object
- Object
- Uptrends::Client
- Includes:
- HTTParty
- Defined in:
- lib/uptrends/client.rb
Instance Attribute Summary collapse
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #add_probe(opts = {}) ⇒ Object
- #add_probe_to_group(opts = {}) ⇒ Object
- #checkpoints(opts = {}) ⇒ Object
- #create_http_probe(opts = {}) ⇒ Object
- #delete_probe(probe) ⇒ Object
- #get_probe_group_members(opts = {}) ⇒ Object
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
- #probe_groups ⇒ Object
- #probes(opts = {}) ⇒ Object
- #update_probe(probe) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Client
Returns a new instance of Client.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/uptrends/client.rb', line 16 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
#username ⇒ Object (readonly)
Returns the value of attribute username.
14 15 16 |
# File 'lib/uptrends/client.rb', line 14 def username @username end |
Instance Method Details
#add_probe(opts = {}) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/uptrends/client.rb', line 61 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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/uptrends/client.rb', line 72 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 |
#checkpoints(opts = {}) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/uptrends/client.rb', line 36 def checkpoints(opts = {}) if opts[:refresh] @checkpoints = get_checkpoints else @checkpoints ||= get_checkpoints end end |
#create_http_probe(opts = {}) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/uptrends/client.rb', line 99 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
92 93 94 95 96 97 |
# File 'lib/uptrends/client.rb', line 92 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
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/uptrends/client.rb', line 48 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_groups ⇒ Object
44 45 46 |
# File 'lib/uptrends/client.rb', line 44 def probe_groups @probe_groups ||= get_probe_groups end |
#probes(opts = {}) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/uptrends/client.rb', line 28 def probes(opts = {}) if opts[:refresh] @probes = get_probes else @probes ||= get_probes end end |
#update_probe(probe) ⇒ Object
87 88 89 90 |
# File 'lib/uptrends/client.rb', line 87 def update_probe(probe) res = self.class.put("/probes/#{probe.guid}", body: Uptrends::Utils.gen_request_body(probe)) raise_or_return(res) end |