copperegg-alerts 0.0.2 → 0.1.0

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: 816960ed7460c46475a51a7f02127252e5038906
4
- data.tar.gz: 33ac93c9ce9660e720887cc027e85a864426175a
3
+ metadata.gz: 60f86c02f2b84845f8148475383e391f6fcc7147
4
+ data.tar.gz: 3d4c3e906ac7cc21ab85fa8c6fcf978be49453a9
5
5
  SHA512:
6
- metadata.gz: 7931be5fd60473b9b7ae3cdadd23167014e0a820c7b63086143b4c6243230940a07180e8009880e98517b95f2953f81ecac27d3a90431925304f6389c9bdfaaf
7
- data.tar.gz: 1aa75144163430d2ddc2ccdf6d8497e55b781264db0b0db9367165390996f64d526a0b261c4ae40a9658c8d58ed8d7105d6620090c2f29f1d3ad7322ce55a917
6
+ metadata.gz: 7051e964d2989bde9eeae23cd63316c93637f209ae64af32de0a6922ce31672c6933e40dc95ba161e5cf1a6a86d259020252aa74c9e74bad745a0ebd9cc5a7f5
7
+ data.tar.gz: 5c4402cfa23c097a1876ca0b89e147f2d4dce0a65991dcec3c73678bd78f6d85583f23fad7327f23433bf08ca76b45a1ca62d21b6949f9f6c9996df01a764e61
data/README.md CHANGED
@@ -1,32 +1,43 @@
1
- # Copperegg::Alerts [![Build Status](https://travis-ci.org/cargomedia/copperegg-alerts.png)](https://travis-ci.org/cargomedia/copperegg-alerts)
1
+ Copperegg::Alerts [![Build Status](https://travis-ci.org/cargomedia/copperegg-alerts.png)](https://travis-ci.org/cargomedia/copperegg-alerts)
2
+ =================
2
3
 
3
4
  Minimalistic API client to manipulate [Copperegg's alert schedules](http://dev.copperegg.com/alerts/schedules.html) aka Maintenance Mode
4
5
 
5
- ## Installation
6
+ Installation
7
+ ------------
6
8
 
7
9
  Add this line to your application's Gemfile:
8
10
 
9
11
  gem 'copperegg-alerts'
10
12
 
11
- ## Usage
13
+ Usage
14
+ -----
15
+ see [http://dev.copperegg.com/alerts/schedules.html](http://dev.copperegg.com/alerts/schedules.html) for verbs and arguments
16
+
12
17
 
13
18
  require 'copperegg'
14
19
 
15
20
  # Set up client
16
21
 
17
- Copperegg::Client.instance.auth_setup(API_KEY)
18
- alerts = Copperegg::Alerts.new
22
+ Copperegg::Alerts::Client.instance.auth_setup(API_KEY)
23
+ schedule = Copperegg::Alerts::Schedule.new
19
24
 
20
25
  # Create a new alert schedule (maintenance mode)
21
26
  #
22
27
  # Arguments:
23
28
  # title - A name for the alert schedule
24
29
  # [<arg1>[..<argN>] - Any argument from 'The Alert Schedule Hash'
25
- # see http://dev.copperegg.com/alerts/schedules.html
30
+ # see http://dev.copperegg.com/alerts/schedules.html for verbs and arguments
26
31
 
27
- alerts.create_schedule('spec_test',
32
+ schedule.create('spec_test',
28
33
  'match' => {'tag' => ['foo', 'foo-bar']},
29
34
  'state' => 'enabled',
30
35
  'duration' => 7,
31
36
  'start_time' => '2014-09-14T10:21:40Z'
32
37
  )
38
+
39
+ schedule.update('spec_test',
40
+ 'state' => 'disabled'
41
+ )
42
+
43
+ schedule.destroy('spec_test')
@@ -1,59 +1,8 @@
1
- require 'deep_merge'
1
+ require 'copperegg/alerts/client'
2
+ require 'copperegg/alerts/schedule'
3
+ require 'copperegg/alerts/version'
2
4
 
3
5
  module Copperegg
4
- class Alerts
5
- attr_reader :schedules
6
-
7
- def initialize
8
- @client = Copperegg::Client.instance
9
- @schedules = @client.get('alerts/schedules.json')
10
- end
11
-
12
- def set_schedule(name, tags = {})
13
- delete_schedules(name)
14
- create_schedule(name, tags)
15
- end
16
-
17
- def modify_schedule(name, *args)
18
- body = {}
19
- args.each { |arg| body.deep_merge!(arg) }
20
- selected_schedules = @schedules.select { |h| h['name'] == name }
21
- if selected_schedules
22
- @schedules -= selected_schedules
23
- selected_schedules.each do |s|
24
- result = @client.put?("alerts/schedules/#{s['id']}.json", body)
25
- if result == nil
26
- @schedules << s
27
- else
28
- @schedules << result
29
- end
30
- end
31
- end
32
- end
33
-
34
- def create_schedule(name, *args)
35
- defaults = {
36
- 'name' => name,
37
- 'state' => 'enabled',
38
- 'duration' => 10,
39
- 'start_time' => Time.now.gmtime.strftime('%Y-%m-%dt%H:%M:%Sz'),
40
- }
41
- args.each { |arg| defaults.deep_merge!(arg) }
42
- if result = @client.post?('alerts/schedules.json', defaults)
43
- @schedules << result.parsed_response
44
- end
45
- end
46
-
47
- def delete_schedules(name)
48
- selected_schedules = @schedules.select { |h| h['name'] == name }
49
- if selected_schedules
50
- @schedules -= selected_schedules
51
- selected_schedules.each do |s|
52
- if @client.delete?("alerts/schedules/#{s['id']}.json") == nil
53
- @schedules << s
54
- end
55
- end
56
- end
57
- end
6
+ module Alerts
58
7
  end
59
8
  end
@@ -0,0 +1,63 @@
1
+ require 'singleton'
2
+ require 'httparty'
3
+ require 'deep_merge'
4
+
5
+ module Copperegg
6
+ module Alerts
7
+ class Client
8
+ include Singleton
9
+
10
+ attr_reader :api_base_uri
11
+
12
+ def initialize
13
+ @api_base_uri = 'https://api.copperegg.com/v2/'
14
+ end
15
+
16
+ def auth_setup(api_key)
17
+ @auth = {:basic_auth => {:username => api_key, :password => 'U'}}
18
+ return self
19
+ end
20
+
21
+ JSON = {'content-type' => 'application/json'}
22
+
23
+ def method_missing(method, resource, *args)
24
+ if method.to_s =~ /\?$/
25
+ method = method.to_s.sub!(/\?$/, '')
26
+ result = self.send(method.to_sym, resource, *args)
27
+ return result if result.code == 200
28
+ end
29
+ end
30
+
31
+ def get(path)
32
+ _send(:get, path)
33
+ end
34
+
35
+ def post(path, body)
36
+ _send(:post, path, body)
37
+ end
38
+
39
+ def put(path, body)
40
+ _send(:put, path, body)
41
+ end
42
+
43
+ def delete(path)
44
+ _send(:delete, path)
45
+ end
46
+
47
+ private
48
+
49
+ def _send(method, path, body = {})
50
+ auth = @auth.clone
51
+ unless body.empty?
52
+ auth.merge!({:headers => JSON}.merge!({:body => body.to_json}))
53
+ end
54
+ response = HTTParty.send(method.to_sym, @api_base_uri + path, auth.to_hash)
55
+ if response.code != 200
56
+ raise("HTTP/#{method} Request failed. Response code `#{response.code}`, message `#{response.message}`, body `#{response.body}`")
57
+ end
58
+ response
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,64 @@
1
+ require 'deep_merge'
2
+ require 'copperegg/alerts'
3
+
4
+
5
+ module Copperegg
6
+ module Alerts
7
+ class Schedule
8
+ attr_reader :schedules
9
+
10
+ def initialize
11
+ @client = Copperegg::Alerts::Client.instance
12
+ @schedules = @client.get('alerts/schedules.json')
13
+ end
14
+
15
+ def create(name, tags = {})
16
+ delete(name)
17
+ add(name, tags)
18
+ end
19
+
20
+ def update(name, *args)
21
+ body = {}
22
+ args.each { |arg| body.deep_merge!(arg) }
23
+ selected_schedules = @schedules.select { |h| h['name'] == name }
24
+ if selected_schedules
25
+ @schedules -= selected_schedules
26
+ selected_schedules.each do |s|
27
+ result = @client.put?("alerts/schedules/#{s['id']}.json", body)
28
+ if result == nil
29
+ @schedules << s
30
+ else
31
+ @schedules << result
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ def add(name, *args)
38
+ defaults = {
39
+ 'name' => name,
40
+ 'state' => 'enabled',
41
+ 'duration' => 10,
42
+ 'start_time' => Time.now.gmtime.strftime('%Y-%m-%dt%H:%M:%Sz'),
43
+ }
44
+ args.each { |arg| defaults.deep_merge!(arg) }
45
+ if result = @client.post?('alerts/schedules.json', defaults)
46
+ @schedules << result.parsed_response
47
+ end
48
+ end
49
+
50
+ def delete(name)
51
+ selected_schedules = @schedules.select { |h| h['name'] == name }
52
+ if selected_schedules
53
+ @schedules -= selected_schedules
54
+ selected_schedules.each do |s|
55
+ if @client.delete?("alerts/schedules/#{s['id']}.json") == nil
56
+ @schedules << s
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+
@@ -0,0 +1,5 @@
1
+ module Copperegg
2
+ module Alerts
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: copperegg-alerts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cargo Media
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-01-20 00:00:00.000000000 Z
12
+ date: 2015-03-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
@@ -95,6 +95,20 @@ dependencies:
95
95
  - - "~>"
96
96
  - !ruby/object:Gem::Version
97
97
  version: 1.18.0
98
+ - !ruby/object:Gem::Dependency
99
+ name: safe_yaml
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: 1.0.4
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: 1.0.4
98
112
  - !ruby/object:Gem::Dependency
99
113
  name: vcr
100
114
  requirement: !ruby/object:Gem::Requirement
@@ -131,10 +145,10 @@ extra_rdoc_files: []
131
145
  files:
132
146
  - LICENSE
133
147
  - README.md
134
- - lib/copperegg.rb
135
148
  - lib/copperegg/alerts.rb
136
- - lib/copperegg/client.rb
137
- - lib/copperegg/version.rb
149
+ - lib/copperegg/alerts/client.rb
150
+ - lib/copperegg/alerts/schedule.rb
151
+ - lib/copperegg/alerts/version.rb
138
152
  homepage: https://github.com/cargomedia/copperegg-alerts
139
153
  licenses:
140
154
  - MIT
@@ -155,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
155
169
  version: '0'
156
170
  requirements: []
157
171
  rubyforge_project:
158
- rubygems_version: 2.4.1
172
+ rubygems_version: 2.4.6
159
173
  signing_key:
160
174
  specification_version: 4
161
175
  summary: A very minimalistic Copperegg API client for managing alert schedules
@@ -1,5 +0,0 @@
1
- module Copperegg
2
- require 'copperegg/client'
3
- require 'copperegg/alerts'
4
- require "copperegg/version"
5
- end
@@ -1,48 +0,0 @@
1
- require 'singleton'
2
- require 'httparty'
3
- require 'deep_merge'
4
-
5
- module Copperegg
6
- class Client
7
- include Singleton
8
-
9
- attr_reader :api_base_uri
10
-
11
- def initialize
12
- @api_base_uri = 'https://api.copperegg.com/v2/'
13
- end
14
-
15
- def auth_setup(api_key)
16
- @auth = {:basic_auth => {:username => api_key, :password => 'U'}}
17
- return self
18
- end
19
-
20
- def get(resource, *args)
21
- HTTParty.get(@api_base_uri + resource, @auth.to_hash)
22
- end
23
-
24
- JSON = {'content-type' => 'application/json'}
25
-
26
- def method_missing(method, resource, *args)
27
- if method.to_s =~ /\?$/
28
- method = method.to_s.sub!(/\?$/, '')
29
- result = self.send(method.to_sym, resource, *args)
30
- return result if result.code == 200
31
- end
32
- end
33
-
34
- ['post', 'put'].each do |method|
35
- define_method(method) do |resource, *args|
36
- body = {}
37
- args.each { |arg| body.deep_merge!(arg) }
38
- HTTParty.send(method.to_sym, @api_base_uri + resource, @auth.merge({:headers => JSON}.merge({:body => body.to_json})))
39
- end
40
- end
41
-
42
- def delete(resource, *args)
43
- HTTParty.delete(@api_base_uri + resource, @auth.to_hash)
44
- end
45
-
46
- end
47
- end
48
-
@@ -1,3 +0,0 @@
1
- module Copperegg
2
- VERSION = "0.0.2"
3
- end