Class: Snitcher::API::Client
- Inherits:
-
Object
- Object
- Snitcher::API::Client
- Defined in:
- lib/snitcher/api/client.rb
Constant Summary collapse
- DEFAULT_ENDPOINT =
"https://api.deadmanssnitch.com"
Instance Method Summary collapse
-
#add_tags(token, tags = []) ⇒ Array<String>
Add one or more tags to an existing snitch, identified by token.
-
#create_snitch(attributes = {}) ⇒ Snitcher::API::Snitch
Create a new Snitch.
-
#delete_snitch(token) ⇒ nil
Deletes a Snitch.
-
#initialize(key, options = {}) ⇒ Snitcher::API::Client
constructor
Create a new API Client for taling to Dead Man’s Snitch’s API.
-
#pause_snitch(token) ⇒ nil
Pauses a Snitch if it can be paused.
-
#remove_tag(token, tag) ⇒ Array<String>
Remove a tag from a Snitch.
-
#snitch(token) ⇒ Snitcher::API::Snitch
Get a single Snitch by it’s unique token.
-
#snitches(filters = {}) ⇒ Array<Snitcher::API::Snitch>
Get the list snitches on the account.
-
#update_snitch(token, attributes = {}) ⇒ Object
Update a snitch, identified by token, using passed-in values.
Constructor Details
#initialize(key, options = {}) ⇒ Snitcher::API::Client
Create a new API Client for taling to Dead Man’s Snitch’s API.
31 32 33 34 35 36 37 |
# File 'lib/snitcher/api/client.rb', line 31 def initialize(key, = {}) endpoint = [:endpoint] || DEFAULT_ENDPOINT @key = key @endpoint = URI.parse(endpoint).freeze @timeout = .fetch(:timeout, 5.0) end |
Instance Method Details
#add_tags(token, tags = []) ⇒ Array<String>
Add one or more tags to an existing snitch, identified by token.
218 219 220 221 222 |
# File 'lib/snitcher/api/client.rb', line 218 def (token, = []) token = CGI.escape(token) post("/v1/snitches/#{token}/tags", Array().flatten) end |
#create_snitch(attributes = {}) ⇒ Snitcher::API::Snitch
Create a new Snitch.
134 135 136 137 138 139 140 141 142 |
# File 'lib/snitcher/api/client.rb', line 134 def create_snitch(attributes = {}) if interval = attributes.delete(:interval) type = attributes[:type] ||= {} type[:interval] ||= interval end response = post("/v1/snitches", attributes) Snitcher::API::Snitch.new(response) end |
#delete_snitch(token) ⇒ nil
Deletes a Snitch.
280 281 282 283 284 285 286 |
# File 'lib/snitcher/api/client.rb', line 280 def delete_snitch(token) token = CGI.escape(token) delete("/v1/snitches/#{token}") nil end |
#pause_snitch(token) ⇒ nil
Pauses a Snitch if it can be paused. Snitches can only be paused if their status is currently “failing” or “errored”.
259 260 261 262 263 264 265 |
# File 'lib/snitcher/api/client.rb', line 259 def pause_snitch(token) token = CGI.escape(token) post("/v1/snitches/#{token}/pause") nil end |
#remove_tag(token, tag) ⇒ Array<String>
Remove a tag from a Snitch.
238 239 240 241 242 243 |
# File 'lib/snitcher/api/client.rb', line 238 def remove_tag(token, tag) token = CGI.escape(token) tag = CGI.escape(tag) delete("/v1/snitches/#{token}/tags/#{tag}") end |
#snitch(token) ⇒ Snitcher::API::Snitch
Get a single Snitch by it’s unique token.
98 99 100 101 102 103 |
# File 'lib/snitcher/api/client.rb', line 98 def snitch(token) token = CGI.escape(token) payload = get("/v1/snitches/#{token}") Snitcher::API::Snitch.new(payload) end |
#snitches(filters = {}) ⇒ Array<Snitcher::API::Snitch>
Get the list snitches on the account
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/snitcher/api/client.rb', line 64 def snitches(filters = {}) query = {} # Tags allow for labeling Snitches for better categorization. This allows # filtering by a set of tags. if = filters[:tags] = Array().flatten query[:tags] = .map(&:strip).compact.uniq.join(",") end # JSON array of Snitch attributes response = get("/v1/snitches", query) # Convert the attributes hashes into Objects response.map! do |snitch| Snitcher::API::Snitch.new(snitch) end end |
#update_snitch(token, attributes = {}) ⇒ Object
Update a snitch, identified by token, using passed-in values. Only changes those values included in the attributes hash; other attributes are not changed.
Raise Timeout::Error if the API request times out
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/snitcher/api/client.rb', line 182 def update_snitch(token, attributes = {}) if attributes.key?(:tags) attributes[:tags] = [attributes[:tags]].flatten.compact end # Expand the interval key to the full structure required by the API if interval = attributes.delete(:interval) type = attributes[:type] ||= {} type[:interval] ||= interval end token = CGI.escape(token) payload = patch("/v1/snitches/#{token}", attributes) Snitcher::API::Snitch.new(payload) end |