snitcher 0.4.0.rc2 → 0.4.0.rc4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6d59a08f5f3886dc3f7b0aef1943627c22795c2f
4
- data.tar.gz: 108054b71ff9b607b91ac3808d01e8c9173b5b0a
3
+ metadata.gz: 269d8a39f4f95c7cf5d1d574d7e0db92f3ca3d95
4
+ data.tar.gz: 9b6ce02c920e04cb8e544b0d1b2d0b2510626ca5
5
5
  SHA512:
6
- metadata.gz: 5fb6accdbc955dd55cf0f1e1a994660557a2425882fe125fa09caa1051510585bc4fea89d4f56bc7588985bf01c487b35b78db8b0b454b5b6c7fe5813786e930
7
- data.tar.gz: 3dfdbc8579aac588a50626ed6993bbbf380ca0579c355554a9711fd1d9d0aaa57d84ef03a04f984f1e0e21beccef787e0b7ab2a272f9685461e9a9fac11bd7e2
6
+ metadata.gz: 394d5c82f089074d385f237da314701ea7cf00a353d2339698f90da97a988a4387c41ce7c422bdfddc42719026b9721c5e737f90ba21379883e4bdb8705571f7
7
+ data.tar.gz: b9126ddc040154e125d763a13cfef5b09897d916fcb04c49cce36a00af93b1b838dd96bd8bb05e725a8bc15d0dce5baa7f409ef060266ff912f424bacdaaf754
@@ -1,6 +1,3 @@
1
- branches:
2
- only:
3
- - master
4
1
  language: ruby
5
2
  matrix:
6
3
  allow_failures:
@@ -17,6 +14,6 @@ rvm:
17
14
  - 2.0.0
18
15
  - 2.1
19
16
  - 2.2
20
- - 2.3.0
17
+ - 2.3
21
18
  - jruby
22
19
  - ruby-head
@@ -1,5 +1,6 @@
1
1
  ## 0.4.0 / TBD
2
- * [FEATURE] Add Snitcher::API for integrating with the DMS API ([[@danabrit](https://github.com/danabrit))
2
+ * [FEATURE] Added Snitcher::API for integrating with the DMS API ([@danabrit](https://github.com/danabrit))
3
+ * [FEATURE] Added Snitcher.snitch! which raises exceptions on errors ([@gaffneyc](https://github.com/gaffneyc))
3
4
 
4
5
  ## 0.3.2 / 2015-08-13
5
6
  * [BUG] Fixed passing messaging during a check-in ([@ryoung](https://github.com/ryoung))
data/README.md CHANGED
@@ -89,14 +89,15 @@ Returns an array of Snitches.
89
89
 
90
90
  ### Create a Snitch
91
91
 
92
- Required attributes are name and interval. Optional attributes are notes and
93
- tags.
92
+ Both `:name` and `:interval` are required. Optional attributes include `:notes`,
93
+ and `:tags`. For a full list see [the API documentation](https://deadmanssnitch.com/docs/api/v1#creating-a-snitch).
94
94
 
95
95
  ```ruby
96
- attributes = { "name": "Nightly User Data Backups",
97
- "interval": "daily",
98
- "notes": "User login and usage data",
99
- "tags": ["users", "critical"]
96
+ attributes = {
97
+ name: "Nightly User Data Backups",
98
+ interval: "daily",
99
+ notes: "User login and usage data",
100
+ tags: ["users", "critical"],
100
101
  }
101
102
  client.create_snitch(attributes)
102
103
  ```
@@ -110,8 +111,8 @@ change. The rest of a Snitch's attributes will remain the same.
110
111
 
111
112
  ```ruby
112
113
  token = "c2354d53d2"
113
- new_attributes = { "name": "Important Nightly User Data Backups" }
114
- client.update_snitch(token, new_attributes)
114
+ attrs = { "name": "Important Nightly User Data Backups" }
115
+ client.update_snitch(token, attrs)
115
116
  ```
116
117
 
117
118
  Returns the edited Snitch.
@@ -122,7 +123,7 @@ This function adds tags to a Snitch, retaining whatever tags it already has.
122
123
 
123
124
  ```ruby
124
125
  token = "c2354d53d2"
125
- tags = ["spring_campaign", "support"]
126
+ tags = ["spring_campaign", "support"]
126
127
  client.add_tags(token, tags)
127
128
  ```
128
129
 
@@ -140,6 +141,20 @@ client.remove_tag(token, tag)
140
141
 
141
142
  Returns an array of all of the Snitch's remaining tags.
142
143
 
144
+ ### Setting the Tags on a Snitch
145
+
146
+ ```ruby
147
+ token = "c2354d53d2"
148
+ client.update_snitch(token, tags: [ "production", "critical" ])
149
+ ```
150
+
151
+ ### Removing all Tags from a Snitch
152
+
153
+ ```ruby
154
+ token = "c2354d53d2"
155
+ client.update_snitch(token, tags: [])
156
+ ```
157
+
143
158
  ### Pause a Snitch
144
159
 
145
160
  ```ruby
@@ -6,7 +6,7 @@ require "snitcher/version"
6
6
  module Snitcher
7
7
  extend self
8
8
 
9
- # Check-in to Dead Man's Snitch.
9
+ # Check-in to Dead Man's Snitch, exceptions are raised on failures.
10
10
  #
11
11
  # @param token [String] The unique Snitch token to check-in with. This can be
12
12
  # found on the Setup page as the last part of the HTTP check-in url. For
@@ -19,16 +19,13 @@ module Snitcher
19
19
  #
20
20
  # @option opts [Float, Fixnum] :timeout Number of seconds to wait for a
21
21
  # response from the server. Default is 5 seconds.
22
- # no
23
22
  #
24
23
  # @example
25
24
  # Snitch.snitch("c2354d53d2")
26
25
  # # => true
27
26
  #
28
- # @raise [Timeout::Error] if the request took too long and timed out.
29
- #
30
27
  # @return [Boolean] if the check-in succeeded.
31
- def snitch(token, opts = {})
28
+ def snitch!(token, opts = {})
32
29
  uri = URI.parse(checkin_url(opts, token))
33
30
  uri.query = URI.encode_www_form(m: opts[:message]) if opts[:message]
34
31
 
@@ -41,7 +38,30 @@ module Snitcher
41
38
  response = http.request(request)
42
39
  response.is_a?(Net::HTTPSuccess)
43
40
  end
44
- rescue ::Timeout::Error
41
+ end
42
+
43
+ # Check-in to Dead Man's Snitch.
44
+ #
45
+ # @param token [String] The unique Snitch token to check-in with. This can be
46
+ # found on the Setup page as the last part of the HTTP check-in url. For
47
+ # example, c2354d53d2 is the token in http://nosnch.in/c2354d53d2.
48
+ #
49
+ # @param [Hash] opts
50
+ #
51
+ # @option opts [String] :message Text message to include with the check-in.
52
+ # The message is limited to 256 characters.
53
+ #
54
+ # @option opts [Float, Fixnum] :timeout Number of seconds to wait for a
55
+ # response from the server. Default is 5 seconds.
56
+ #
57
+ # @example
58
+ # Snitch.snitch("c2354d53d2")
59
+ # # => true
60
+ #
61
+ # @return [Boolean] if the check-in succeeded.
62
+ def snitch(*args)
63
+ snitch!(*args)
64
+ rescue StandardError
45
65
  false
46
66
  end
47
67
 
@@ -59,22 +59,22 @@ class Snitcher::API::Client
59
59
  #
60
60
  # @return [Array<Snitcher::API::Snitch>] the snitches on the account.
61
61
  def snitches(filters = {})
62
- path = "/v1/snitches"
63
62
  query = {}
64
63
 
65
64
  # Tags allow for labeling Snitches for better categorization. This allows
66
65
  # filtering by a set of tags.
67
- if filters[:tags]
68
- tags = Array(filters[:tags]).flatten
66
+ if tags = filters[:tags]
67
+ tags = Array(tags).flatten
69
68
  query[:tags] = tags.map(&:strip).compact.uniq.join(",")
70
69
  end
71
70
 
72
- # Only add the query param if any valid filters were given.
73
- if query.any?
74
- path = "#{path}?#{URI.encode_www_form(query)}"
75
- end
71
+ # JSON array of Snitch attributes
72
+ response = get("/v1/snitches", query)
76
73
 
77
- snitch_array(get(path))
74
+ # Convert the attributes hashes into Objects
75
+ response.map! do |snitch|
76
+ Snitcher::API::Snitch.new(snitch)
77
+ end
78
78
  end
79
79
 
80
80
  # Get a single Snitch by it's unique token.
@@ -126,10 +126,10 @@ class Snitcher::API::Client
126
126
  # @raise [Snitcher::API::Error] if any other API errors occur.
127
127
  #
128
128
  # @return [Snitcher::API::Snitch] the new Snitch.
129
- def create_snitch(attributes={})
130
- if attributes.has_key?(:interval)
129
+ def create_snitch(attributes = {})
130
+ if interval = attributes.delete(:interval)
131
131
  type = attributes[:type] ||= {}
132
- type[:interval] ||= attributes.delete(:interval)
132
+ type[:interval] ||= interval
133
133
  end
134
134
 
135
135
  response = post("/v1/snitches", attributes)
@@ -174,14 +174,15 @@ class Snitcher::API::Client
174
174
  # @raise [Snitcher::API::Error] if any other API errors occur.
175
175
  #
176
176
  # Raise Timeout::Error if the API request times out
177
- def update_snitch(token, attributes={})
177
+ def update_snitch(token, attributes = {})
178
178
  if attributes.has_key?(:tags)
179
179
  attributes[:tags] = [attributes[:tags]].flatten.compact
180
180
  end
181
181
 
182
- if attributes.has_key?(:interval)
182
+ # Expand the interval key to the full structure required by the API
183
+ if interval = attributes.delete(:interval)
183
184
  type = attributes[:type] ||= {}
184
- type[:interval] ||= attributes.delete(:interval)
185
+ type[:interval] ||= interval
185
186
  end
186
187
 
187
188
  payload = patch("/v1/snitches/#{token}", attributes)
@@ -207,9 +208,8 @@ class Snitcher::API::Client
207
208
  # @raise [Snitcher::API::Error] if an API errors occur.
208
209
  #
209
210
  # @return [Array<String>] full list of tags on the Snitch.
210
- def add_tags(token, tags=[])
211
- tags = [tags].flatten
212
- post("/v1/snitches/#{token}/tags", tags)
211
+ def add_tags(token, tags = [])
212
+ post("/v1/snitches/#{token}/tags", Array(tags).flatten)
213
213
  end
214
214
 
215
215
  # Remove a tag from a Snitch.
@@ -227,8 +227,7 @@ class Snitcher::API::Client
227
227
  #
228
228
  # @return [Array<String>] list of the remaining tags on the Snitch.
229
229
  def remove_tag(token, tag)
230
- path = "/v1/snitches/#{token}/tags/#{tag}"
231
- delete(URI.encode(path))
230
+ delete("/v1/snitches/#{token}/tags/#{tag}")
232
231
  end
233
232
 
234
233
  # Pauses a Snitch if it can be paused. Snitches can only be paused if their
@@ -279,7 +278,7 @@ class Snitcher::API::Client
279
278
  "Snitcher; #{engine}/#{RUBY_VERSION}; #{RUBY_PLATFORM}; v#{::Snitcher::VERSION}"
280
279
  end
281
280
 
282
- def execute_request(request, options={})
281
+ def execute_request(request)
283
282
  http_options = {
284
283
  open_timeout: @timeout,
285
284
  read_timeout: @timeout,
@@ -295,7 +294,7 @@ class Snitcher::API::Client
295
294
  if request.body
296
295
  request["Content-Type"] = "application/json"
297
296
 
298
- # Some trickiery to allow pushing the JSON rendering down as far as
297
+ # Some trickery to allow pushing the JSON rendering down as far as
299
298
  # possible.
300
299
  if !request.body.is_a?(String)
301
300
  request.body = JSON.generate(request.body)
@@ -326,37 +325,39 @@ class Snitcher::API::Client
326
325
  end
327
326
  end
328
327
 
329
- def get(path, options={})
328
+ def get(path, query = {})
329
+ path = URI.encode(path)
330
+
331
+ # Only add the query param if any valid filters were given.
332
+ if query.any?
333
+ path = "#{path}?#{URI.encode_www_form(query)}"
334
+ end
335
+
330
336
  request = Net::HTTP::Get.new(path)
331
- execute_request(request, options)
337
+ execute_request(request)
332
338
  end
333
339
 
334
- def post(path, data=nil, options={})
340
+ def post(path, data = nil)
341
+ path = URI.encode(path)
342
+
335
343
  request = Net::HTTP::Post.new(path)
336
344
  request.body = data
337
345
 
338
- execute_request(request, options)
346
+ execute_request(request)
339
347
  end
340
348
 
341
- def patch(path, data, options={})
349
+ def patch(path, data)
350
+ path = URI.encode(path)
351
+
342
352
  request = Net::HTTP::Patch.new(path)
343
353
  request.body = data
344
354
 
345
- execute_request(request, options)
355
+ execute_request(request)
346
356
  end
347
357
 
348
- def delete(path, options={})
358
+ def delete(path)
359
+ path = URI.encode(path)
349
360
  request = Net::HTTP::Delete.new(path)
350
- execute_request(request, options)
351
- end
352
-
353
- private
354
-
355
- def snitch_array(json_payload)
356
- arr = []
357
- json_payload.each do |payload|
358
- arr << Snitcher::API::Snitch.new(payload)
359
- end
360
- arr
361
+ execute_request(request)
361
362
  end
362
363
  end
@@ -1,3 +1,3 @@
1
1
  module Snitcher
2
- VERSION = "0.4.0.rc2"
2
+ VERSION = "0.4.0.rc4"
3
3
  end
@@ -9,15 +9,15 @@ describe Snitcher do
9
9
  stub_request(:get, /nosnch\.in/)
10
10
  end
11
11
 
12
- describe ".snitch" do
12
+ describe ".snitch!" do
13
13
  it "pings DMS with the given token" do
14
- Snitcher.snitch(token)
14
+ Snitcher.snitch!(token)
15
15
 
16
16
  expect(a_request(:get, "https://nosnch.in/#{token}")).to have_been_made.once
17
17
  end
18
18
 
19
19
  it "includes a custom user-agent" do
20
- Snitcher.snitch(token)
20
+ Snitcher.snitch!(token)
21
21
 
22
22
  expect(
23
23
  a_request(:get, "https://nosnch.in/#{token}").with(
@@ -26,42 +26,48 @@ describe Snitcher do
26
26
  ).to have_been_made
27
27
  end
28
28
 
29
- context "when successful" do
30
- before do
31
- stub_request(:get, "https://nosnch.in/#{token}").to_return(status: 200)
32
- end
29
+ it "returns true when successful" do
30
+ request = stub_request(:get, "https://nosnch.in/#{token}").to_return(status: 202)
33
31
 
34
- it "returns true" do
35
- expect(Snitcher.snitch(token)).to eq(true)
36
- end
32
+ expect(Snitcher.snitch!(token)).to eq(true)
33
+ expect(request).to have_been_made.once
37
34
  end
38
35
 
39
- context "when unsuccessful" do
40
- before do
41
- stub_request(:get, "https://nosnch.in/#{token}").to_return(status: 404)
42
- end
36
+ it "returns false when unsuccessful" do
37
+ request = stub_request(:get, "https://nosnch.in/#{token}").to_return(status: 404)
43
38
 
44
- it "returns false" do
45
- expect(Snitcher.snitch(token)).to eq(false)
46
- end
39
+ expect(Snitcher.snitch!(token)).to eq(false)
40
+ expect(request).to have_been_made.once
47
41
  end
48
42
 
49
43
  describe "with message" do
50
44
  it "includes the message as a query param" do
51
- Snitcher.snitch(token, message: "A thing just happened")
45
+ Snitcher.snitch!(token, message: "A thing just happened")
52
46
 
53
47
  expect(a_request(:get, "https://nosnch.in/#{token}?m=A%20thing%20just%20happened")).to have_been_made
54
48
  end
55
49
  end
56
50
 
57
- describe "timeout" do
58
- before do
59
- stub_request(:get, "https://nosnch.in/#{token}").to_raise(::Timeout::Error)
60
- end
51
+ it "raises a Timeout::Error if the request timesout" do
52
+ stub_request(:get, "https://nosnch.in/#{token}").to_timeout
61
53
 
62
- it "returns false when timed out" do
63
- expect(Snitcher.snitch(token)).to eq(false)
64
- end
54
+ expect { Snitcher.snitch!(token) }.to raise_error(Timeout::Error)
55
+ end
56
+ end
57
+
58
+ describe ".snitch" do
59
+ it "returns true on a successfuly check-in" do
60
+ stub_request(:get, "https://nosnch.in/#{token}").to_return(status: 202)
61
+
62
+ result = Snitcher.snitch(token)
63
+ expect(result).to be(true)
64
+ end
65
+
66
+ it "returns false on a timeout" do
67
+ stub_request(:get, "https://nosnch.in/#{token}").to_timeout
68
+
69
+ result = Snitcher.snitch(token)
70
+ expect(result).to be(false)
65
71
  end
66
72
  end
67
73
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snitcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0.rc2
4
+ version: 0.4.0.rc4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Collective Idea
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-24 00:00:00.000000000 Z
11
+ date: 2016-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler