Class: Halidator

Inherits:
Object
  • Object
show all
Defined in:
lib/halidator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#errorsObject

Returns the value of attribute errors.



4
5
6
# File 'lib/halidator.rb', line 4

def errors
  @errors
end

Instance Method Details

#_embeddedObject



102
103
104
# File 'lib/halidator.rb', line 102

def _embedded
  @json['_embedded']
end


106
107
108
# File 'lib/halidator.rb', line 106

def _links
  @json['_links']
end

#embedded_valid?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/halidator.rb', line 79

def embedded_valid?
  return true if _embedded.nil?

  _embedded.all? do |resource_type, resource|
    case resource
    when Array
      resource.all?{|x| Halidator.new(x).valid?}
    else
      Halidator.new(resource).valid?
    end
  end
end


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

Returns:

  • (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

Returns:

  • (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


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

Returns:

  • (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_jsonObject



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_hrefObject



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_errorsObject



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

Returns:

  • (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

Returns:

  • (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_halObject



28
29
30
# File 'lib/halidator.rb', line 28

def validate_json_as_hal
  meets_minimal_JSON_representation? && links_all_valid? && embedded_valid?
end