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.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +21 -0
  3. data/.rubocop_todo.yml +135 -0
  4. data/.travis.yml +10 -5
  5. data/README.md +125 -143
  6. data/flapjack-diner.gemspec +1 -1
  7. data/lib/flapjack-diner.rb +24 -54
  8. data/lib/flapjack-diner/argument_validator.rb +17 -0
  9. data/lib/flapjack-diner/resources/checks.rb +52 -0
  10. data/lib/flapjack-diner/resources/contacts.rb +54 -0
  11. data/lib/flapjack-diner/resources/events.rb +54 -0
  12. data/lib/flapjack-diner/resources/maintenance_periods.rb +76 -0
  13. data/lib/flapjack-diner/resources/media.rb +75 -0
  14. data/lib/flapjack-diner/resources/metrics.rb +23 -0
  15. data/lib/flapjack-diner/resources/relationships.rb +275 -0
  16. data/lib/flapjack-diner/resources/rules.rb +75 -0
  17. data/lib/flapjack-diner/resources/states.rb +24 -0
  18. data/lib/flapjack-diner/resources/statistics.rb +24 -0
  19. data/lib/flapjack-diner/resources/tags.rb +47 -0
  20. data/lib/flapjack-diner/tools.rb +456 -46
  21. data/lib/flapjack-diner/version.rb +1 -1
  22. data/spec/flapjack-diner_spec.rb +18 -9
  23. data/spec/resources/checks_spec.rb +7 -7
  24. data/spec/resources/contacts_spec.rb +12 -14
  25. data/spec/resources/events_spec.rb +13 -13
  26. data/spec/resources/maintenance_periods_spec.rb +3 -3
  27. data/spec/resources/media_spec.rb +3 -3
  28. data/spec/resources/metrics_spec.rb +1 -1
  29. data/spec/{relationships_spec.rb → resources/relationships_spec.rb} +25 -71
  30. data/spec/resources/rules_spec.rb +62 -62
  31. data/spec/resources/states_spec.rb +1 -1
  32. data/spec/resources/statistics_spec.rb +1 -1
  33. data/spec/resources/tags_spec.rb +19 -75
  34. data/spec/support/fixture_data.rb +43 -80
  35. metadata +22 -17
  36. data/lib/flapjack-diner/configuration.rb +0 -417
  37. data/lib/flapjack-diner/log_formatter.rb +0 -22
  38. data/lib/flapjack-diner/query.rb +0 -114
  39. data/lib/flapjack-diner/relationships.rb +0 -180
  40. data/lib/flapjack-diner/request.rb +0 -280
  41. data/lib/flapjack-diner/resources.rb +0 -64
  42. data/lib/flapjack-diner/response.rb +0 -91
  43. 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