flapjack-diner 2.0.0b1 → 2.0.0.pre.alpha.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +21 -0
- data/.rubocop_todo.yml +135 -0
- data/.travis.yml +10 -5
- data/README.md +125 -143
- data/flapjack-diner.gemspec +1 -1
- data/lib/flapjack-diner.rb +24 -54
- data/lib/flapjack-diner/argument_validator.rb +17 -0
- data/lib/flapjack-diner/resources/checks.rb +52 -0
- data/lib/flapjack-diner/resources/contacts.rb +54 -0
- data/lib/flapjack-diner/resources/events.rb +54 -0
- data/lib/flapjack-diner/resources/maintenance_periods.rb +76 -0
- data/lib/flapjack-diner/resources/media.rb +75 -0
- data/lib/flapjack-diner/resources/metrics.rb +23 -0
- data/lib/flapjack-diner/resources/relationships.rb +275 -0
- data/lib/flapjack-diner/resources/rules.rb +75 -0
- data/lib/flapjack-diner/resources/states.rb +24 -0
- data/lib/flapjack-diner/resources/statistics.rb +24 -0
- data/lib/flapjack-diner/resources/tags.rb +47 -0
- data/lib/flapjack-diner/tools.rb +456 -46
- data/lib/flapjack-diner/version.rb +1 -1
- data/spec/flapjack-diner_spec.rb +18 -9
- data/spec/resources/checks_spec.rb +7 -7
- data/spec/resources/contacts_spec.rb +12 -14
- data/spec/resources/events_spec.rb +13 -13
- data/spec/resources/maintenance_periods_spec.rb +3 -3
- data/spec/resources/media_spec.rb +3 -3
- data/spec/resources/metrics_spec.rb +1 -1
- data/spec/{relationships_spec.rb → resources/relationships_spec.rb} +25 -71
- data/spec/resources/rules_spec.rb +62 -62
- data/spec/resources/states_spec.rb +1 -1
- data/spec/resources/statistics_spec.rb +1 -1
- data/spec/resources/tags_spec.rb +19 -75
- data/spec/support/fixture_data.rb +43 -80
- metadata +22 -17
- data/lib/flapjack-diner/configuration.rb +0 -417
- data/lib/flapjack-diner/log_formatter.rb +0 -22
- data/lib/flapjack-diner/query.rb +0 -114
- data/lib/flapjack-diner/relationships.rb +0 -180
- data/lib/flapjack-diner/request.rb +0 -280
- data/lib/flapjack-diner/resources.rb +0 -64
- data/lib/flapjack-diner/response.rb +0 -91
- data/lib/flapjack-diner/utility.rb +0 -16
@@ -1,64 +0,0 @@
|
|
1
|
-
require 'flapjack-diner/configuration'
|
2
|
-
|
3
|
-
module Flapjack
|
4
|
-
module Diner
|
5
|
-
module Resources
|
6
|
-
def self.included(base)
|
7
|
-
# base.extend ClassMethods
|
8
|
-
base.class_eval do
|
9
|
-
Flapjack::Diner::Configuration::RESOURCES.each_pair do |name, config|
|
10
|
-
requests = config[:requests]
|
11
|
-
next if requests.nil?
|
12
|
-
|
13
|
-
if requests.key?(:post)
|
14
|
-
define_singleton_method("create_#{name}".to_sym) do |*data|
|
15
|
-
resp = Flapjack::Diner::Request.new(
|
16
|
-
name, "/#{name}", :data => data,
|
17
|
-
:validations => requests[:post]
|
18
|
-
).post
|
19
|
-
@response = Flapjack::Diner::Response.new(resp)
|
20
|
-
@response.process
|
21
|
-
@response.output
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
if requests.key?(:get)
|
26
|
-
define_singleton_method(name) do |*data|
|
27
|
-
resp = Flapjack::Diner::Request.new(
|
28
|
-
name, "/#{name}", :data => data,
|
29
|
-
:validations => requests[:get]
|
30
|
-
).get
|
31
|
-
@response = Flapjack::Diner::Response.new(resp)
|
32
|
-
@response.process
|
33
|
-
@response.output
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
if requests.key?(:patch)
|
38
|
-
define_singleton_method("update_#{name}".to_sym) do |*data|
|
39
|
-
resp = Flapjack::Diner::Request.new(
|
40
|
-
name, "/#{name}", :data => data,
|
41
|
-
:validations => requests[:patch]
|
42
|
-
).patch
|
43
|
-
@response = Flapjack::Diner::Response.new(resp)
|
44
|
-
@response.process
|
45
|
-
@response.output
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
next unless requests.key?(:delete)
|
50
|
-
define_singleton_method("delete_#{name}".to_sym) do |*uuids|
|
51
|
-
raise "'#{method_name}' requires at least one #{resource} UUID " \
|
52
|
-
'parameter' if uuids.nil? || uuids.empty?
|
53
|
-
resp = Flapjack::Diner::Request.new(name, "/#{name}",
|
54
|
-
:ids => uuids).delete
|
55
|
-
@response = Flapjack::Diner::Response.new(resp)
|
56
|
-
@response.process
|
57
|
-
@response.output
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
@@ -1,91 +0,0 @@
|
|
1
|
-
require 'flapjack-diner/utility'
|
2
|
-
|
3
|
-
module Flapjack
|
4
|
-
module Diner
|
5
|
-
class Response
|
6
|
-
SUCCESS_STATUS_CODES = [200, 201, 204]
|
7
|
-
|
8
|
-
attr_reader :output, :context, :error
|
9
|
-
|
10
|
-
def initialize(resp, opts = {})
|
11
|
-
@response = resp
|
12
|
-
@output = nil
|
13
|
-
@context = nil
|
14
|
-
@error = nil
|
15
|
-
@return_keys_as_strings = Flapjack::Diner.return_keys_as_strings
|
16
|
-
end
|
17
|
-
|
18
|
-
def process
|
19
|
-
if 204.eql?(@response.code)
|
20
|
-
@output = true
|
21
|
-
@context = nil
|
22
|
-
@error = nil
|
23
|
-
return
|
24
|
-
end
|
25
|
-
if @response.respond_to?(:parsed_response)
|
26
|
-
parsed = @response.parsed_response
|
27
|
-
end
|
28
|
-
strify = @return_keys_as_strings.is_a?(TrueClass)
|
29
|
-
if [200, 201].include?(@response.code)
|
30
|
-
@output = handle_data(parsed, strify)
|
31
|
-
return
|
32
|
-
end
|
33
|
-
@error = handle_errors(parsed, strify)
|
34
|
-
end
|
35
|
-
|
36
|
-
private
|
37
|
-
|
38
|
-
def flatten_jsonapi_data(data)
|
39
|
-
ret = nil
|
40
|
-
case data
|
41
|
-
when Array
|
42
|
-
ret = data.inject([]) do |memo, d|
|
43
|
-
attrs = d['attributes'] || {}
|
44
|
-
d.each_pair do |k, v|
|
45
|
-
next if 'attributes'.eql?(k)
|
46
|
-
attrs.update(k => v)
|
47
|
-
end
|
48
|
-
memo += [attrs]
|
49
|
-
memo
|
50
|
-
end
|
51
|
-
when Hash
|
52
|
-
ret = data['attributes'] || {}
|
53
|
-
data.each_pair do |k, v|
|
54
|
-
next if 'attributes'.eql?(k)
|
55
|
-
ret.update(k => v)
|
56
|
-
end
|
57
|
-
else
|
58
|
-
ret = data
|
59
|
-
end
|
60
|
-
ret
|
61
|
-
end
|
62
|
-
|
63
|
-
def handle_data(parsed, strify)
|
64
|
-
return parsed if parsed.nil? || !parsed.is_a?(Hash) ||
|
65
|
-
!parsed.key?('data')
|
66
|
-
@context = {}
|
67
|
-
c_incl_key = strify ? 'included' : :included
|
68
|
-
if parsed.key?('included')
|
69
|
-
incl = flatten_jsonapi_data(parsed['included'])
|
70
|
-
@context[c_incl_key] = incl.each_with_object({}) do |i, memo|
|
71
|
-
memo[i['type']] ||= {}
|
72
|
-
memo[i['type']][i['id']] = strify ? i : Flapjack::Diner::Utility.symbolize(i)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
(%w(relationships meta) & parsed.keys).each do |k|
|
76
|
-
c = parsed[k]
|
77
|
-
@context[strify ? k : k.to_sym] = (strify ? c : Flapjack::Diner::Utility.symbolize(c))
|
78
|
-
end
|
79
|
-
ret = flatten_jsonapi_data(parsed['data'])
|
80
|
-
strify ? ret : Flapjack::Diner::Utility.symbolize(ret)
|
81
|
-
end
|
82
|
-
|
83
|
-
def handle_errors(parsed, strify)
|
84
|
-
return parsed if parsed.nil? || !parsed.is_a?(Hash) ||
|
85
|
-
!parsed.key?('errors')
|
86
|
-
errs = parsed['errors']
|
87
|
-
strify ? errs : Flapjack::Diner::Utility.symbolize(errs)
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
module Flapjack
|
2
|
-
module Diner
|
3
|
-
module Utility
|
4
|
-
def self.symbolize(obj)
|
5
|
-
case obj
|
6
|
-
when Hash
|
7
|
-
obj.each_with_object({}) {|(k, v), a| a[k.to_sym] = symbolize(v) }
|
8
|
-
when Array
|
9
|
-
obj.each_with_object([]) {|e, a| a << symbolize(e) }
|
10
|
-
else
|
11
|
-
obj
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|