Class: Halidator
- Inherits:
-
Object
- Object
- Halidator
- Defined in:
- lib/halidator.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
Returns the value of attribute errors.
Instance Method Summary collapse
- #_embedded ⇒ Object
- #_links ⇒ Object
- #embedded_valid? ⇒ Boolean
- #has_links ⇒ Object
-
#initialize(hal) ⇒ Halidator
constructor
A new instance of Halidator.
- #link_valid?(link) ⇒ Boolean
- #links_all_valid? ⇒ Boolean
- #links_has_self ⇒ Object
- #meets_minimal_JSON_representation? ⇒ Boolean
- #parse_json ⇒ Object
- #self_has_href ⇒ Object
- #show_errors ⇒ Object
- #template_valid?(link) ⇒ Boolean
- #valid? ⇒ Boolean
- #validate_json_as_hal ⇒ Object
Constructor Details
#initialize(hal) ⇒ Halidator
Returns a new instance of Halidator.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/halidator.rb', line 5 def initialize(hal) case hal when String @json_string = hal else @json = hal end @errors = [] end |
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
4 5 6 |
# File 'lib/halidator.rb', line 4 def errors @errors end |
Instance Method Details
#_embedded ⇒ Object
102 103 104 |
# File 'lib/halidator.rb', line 102 def @json['_embedded'] end |
#_links ⇒ Object
106 107 108 |
# File 'lib/halidator.rb', line 106 def _links @json['_links'] end |
#embedded_valid? ⇒ Boolean
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/halidator.rb', line 79 def return true if .nil? .all? do |resource_type, resource| case resource when Array resource.all?{|x| Halidator.new(x).valid?} else Halidator.new(resource).valid? end end end |
#has_links ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/halidator.rb', line 110 def has_links if _links true else errors << '_links does not exist' false end end |
#link_valid?(link) ⇒ Boolean
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/halidator.rb', line 46 def link_valid?(link) if $DEBUG puts " #{link}" end unless link['href'] errors << "no href in #{link}" return false end unless template_valid?(link) errors << "invalid template for #{link}" return false end true end |
#links_all_valid? ⇒ Boolean
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/halidator.rb', line 32 def links_all_valid? _links.all? do |k, v| if $DEBUG puts "\n\n", k, v end case v when Array # is an array of links v.all?{|x| link_valid?(x)} else link_valid?(v) end end end |
#links_has_self ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/halidator.rb', line 119 def links_has_self if _links['self'] true else errors << "no self in #{_links.inspect}" false end end |
#meets_minimal_JSON_representation? ⇒ Boolean
92 93 94 |
# File 'lib/halidator.rb', line 92 def meets_minimal_JSON_representation? has_links && links_has_self && self_has_href end |
#parse_json ⇒ Object
22 23 24 25 26 |
# File 'lib/halidator.rb', line 22 def parse_json return true if @json @json = JSON.parse(@json_string) end |
#self_has_href ⇒ Object
128 129 130 131 132 133 134 135 |
# File 'lib/halidator.rb', line 128 def self_has_href if _links['self']['href'] true else errors << "no href in #{_links['self']}" false end end |
#show_errors ⇒ Object
96 97 98 99 100 |
# File 'lib/halidator.rb', line 96 def show_errors if $DEBUG puts "\nERRORS", "---------------", errors.inspect end end |
#template_valid?(link) ⇒ Boolean
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/halidator.rb', line 61 def template_valid?(link) return true unless link['templated'] == true pairs = 0 res = link['href'].each_char.all? do |c| if '{' == c pairs += 1 pairs == 1 elsif '}' == c pairs -= 1 pairs == 0 else true end end res && (pairs == 0) && link['href'].include?('{') end |
#valid? ⇒ Boolean
16 17 18 19 20 |
# File 'lib/halidator.rb', line 16 def valid? result = parse_json && validate_json_as_hal show_errors result end |
#validate_json_as_hal ⇒ Object
28 29 30 |
# File 'lib/halidator.rb', line 28 def validate_json_as_hal meets_minimal_JSON_representation? && links_all_valid? && end |