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.
28 29 30 31 32 33 34 |
# File 'lib/snitcher/api/client.rb', line 28 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.
211 212 213 |
# File 'lib/snitcher/api/client.rb', line 211 def (token, = []) post("/v1/snitches/#{token}/tags", Array().flatten) end |
#create_snitch(attributes = {}) ⇒ Snitcher::API::Snitch
Create a new Snitch.
129 130 131 132 133 134 135 136 137 |
# File 'lib/snitcher/api/client.rb', line 129 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.
266 267 268 269 270 |
# File 'lib/snitcher/api/client.rb', line 266 def delete_snitch(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”.
247 248 249 250 251 |
# File 'lib/snitcher/api/client.rb', line 247 def pause_snitch(token) post("/v1/snitches/#{token}/pause") nil end |
#remove_tag(token, tag) ⇒ Array<String>
Remove a tag from a Snitch.
229 230 231 |
# File 'lib/snitcher/api/client.rb', line 229 def remove_tag(token, tag) delete("/v1/snitches/#{token}/tags/#{tag}") end |
#snitch(token) ⇒ Snitcher::API::Snitch
Get a single Snitch by it’s unique token.
95 96 97 98 |
# File 'lib/snitcher/api/client.rb', line 95 def snitch(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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/snitcher/api/client.rb', line 61 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
177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/snitcher/api/client.rb', line 177 def update_snitch(token, attributes = {}) if attributes.has_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 payload = patch("/v1/snitches/#{token}", attributes) Snitcher::API::Snitch.new(payload) end |