halidator 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/bin/halidate +14 -0
  2. data/lib/halidator.rb +137 -0
  3. metadata +47 -0
data/bin/halidate ADDED
@@ -0,0 +1,14 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require_relative '../lib/halidator'
4
+
5
+ input = ARGF.read
6
+
7
+ hal = Halidator.new(input)
8
+ res = hal.valid?
9
+ if true == res
10
+ $stdout.puts "true"
11
+ else
12
+ $stdout.puts 'false'
13
+ $stderr.puts hal.errors
14
+ end
data/lib/halidator.rb ADDED
@@ -0,0 +1,137 @@
1
+ require 'json'
2
+
3
+ class Halidator
4
+ attr_accessor :errors
5
+ def initialize(hal)
6
+ case hal
7
+ when String
8
+ @json_string = hal
9
+ else
10
+ @json = hal
11
+ end
12
+
13
+ @errors = []
14
+ end
15
+
16
+ def valid?
17
+ result = parse_json && validate_json_as_hal
18
+ show_errors
19
+ result
20
+ end
21
+
22
+ def parse_json
23
+ return true if @json
24
+
25
+ @json = JSON.parse(@json_string)
26
+ end
27
+
28
+ def validate_json_as_hal
29
+ meets_minimal_JSON_representation? && links_all_valid? && embedded_valid?
30
+ end
31
+
32
+ def links_all_valid?
33
+ _links.all? do |k, v|
34
+ if $DEBUG
35
+ puts "\n\n", k, v
36
+ end
37
+ case v
38
+ when Array # is an array of links
39
+ v.all?{|x| link_valid?(x)}
40
+ else
41
+ link_valid?(v)
42
+ end
43
+ end
44
+ end
45
+
46
+ def link_valid?(link)
47
+ if $DEBUG
48
+ puts " #{link}"
49
+ end
50
+ unless link['href']
51
+ errors << "no href in #{link}"
52
+ return false
53
+ end
54
+ unless template_valid?(link)
55
+ errors << "invalid template for #{link}"
56
+ return false
57
+ end
58
+ true
59
+ end
60
+
61
+ def template_valid?(link)
62
+ return true unless link['templated'] == true
63
+
64
+ pairs = 0
65
+ res = link['href'].each_char.all? do |c|
66
+ if '{' == c
67
+ pairs += 1
68
+ pairs == 1
69
+ elsif '}' == c
70
+ pairs -= 1
71
+ pairs == 0
72
+ else
73
+ true
74
+ end
75
+ end
76
+ res && (pairs == 0) && link['href'].include?('{')
77
+ end
78
+
79
+ def embedded_valid?
80
+ return true if _embedded.nil?
81
+
82
+ _embedded.all? do |resource_type, resource|
83
+ case resource
84
+ when Array
85
+ resource.all?{|x| Halidator.new(x).valid?}
86
+ else
87
+ Halidator.new(resource).valid?
88
+ end
89
+ end
90
+ end
91
+
92
+ def meets_minimal_JSON_representation?
93
+ has_links && links_has_self && self_has_href
94
+ end
95
+
96
+ def show_errors
97
+ if $DEBUG
98
+ puts "\nERRORS", "---------------", errors.inspect
99
+ end
100
+ end
101
+
102
+ def _embedded
103
+ @json['_embedded']
104
+ end
105
+
106
+ def _links
107
+ @json['_links']
108
+ end
109
+
110
+ def has_links
111
+ if _links
112
+ true
113
+ else
114
+ errors << '_links does not exist'
115
+ false
116
+ end
117
+ end
118
+
119
+ def links_has_self
120
+ if _links['self']
121
+ true
122
+ else
123
+ errors << "no self in #{_links.inspect}"
124
+ false
125
+ end
126
+ end
127
+
128
+ def self_has_href
129
+ if _links['self']['href']
130
+ true
131
+ else
132
+ errors << "no href in #{_links['self']}"
133
+ false
134
+ end
135
+ end
136
+
137
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: halidator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bob Larrick
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-22 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A small library to validate hal+json
15
+ email: larrick@gmail.com
16
+ executables:
17
+ - halidate
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/halidator.rb
22
+ - bin/halidate
23
+ homepage: https://github.com/deathbob/halidator
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.24
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: A small library to validate hal+json
47
+ test_files: []