Class: SimpleMappr::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-mappr/validator.rb

Class Method Summary collapse

Class Method Details

.validate_bbox(data) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/simple-mappr/validator.rb', line 6

def self.validate_bbox(data)
  validate_type(data, 'String')
  coords = data.split(",")
  if coords.length != 4
    raise InvalidParameterValue, "bbox must have four parameters in the form -110,45,-100,52"
  end
  validate_coordinate(coords[1].to_f, coords[0].to_f)
  validate_coordinate(coords[3].to_f, coords[2].to_f)
end

.validate_color(data) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/simple-mappr/validator.rb', line 27

def self.validate_color(data)
  validate_type(data, 'String')
  rgb = data.split(",")
  if rgb.length != 3
    raise InvalidParameterValue, "color must have three parameters in the form 0,0,0"
  end
  rgb.each do |item|
    begin
      Integer(item || '')
    rescue ArgumentError
      raise InvalidParameterValue, "color value must be an integer <= 255"
    end
    if item.to_i > 255
      raise InvalidParameterValue, "color value must be an integer <= 255"
    end
  end
end

.validate_colors(data) ⇒ Object



22
23
24
25
# File 'lib/simple-mappr/validator.rb', line 22

def self.validate_colors(data)
  validate_type(data, 'Array')
  data.each { |item| validate_color(item) }
end

.validate_coordinate(latitude, longitude) ⇒ Object



16
17
18
19
20
# File 'lib/simple-mappr/validator.rb', line 16

def self.validate_coordinate(latitude, longitude)
  if latitude > 90 || latitude < -90 || longitude > 180 || longitude < -180
    raise InvalidParameterValue, "Coordinate latitude,longitude=#{latitude},#{longitude} is not valid"
  end
end

.validate_dimension(data) ⇒ Object



45
46
47
48
49
50
# File 'lib/simple-mappr/validator.rb', line 45

def self.validate_dimension(data)
  validate_type(data, 'Integer')
  if data > 4_500
    raise InvalidParameterValue, "Accepted integer values are <= 4500"
  end
end

.validate_layers(data) ⇒ Object



106
107
108
109
110
111
# File 'lib/simple-mappr/validator.rb', line 106

def self.validate_layers(data)
  validate_type(data, 'String')
  if !(data.split(",") - LAYERS).empty?
    raise InvalidParameterValue, "Accepted layers are combinations of #{LAYERS.join(",")}"
  end
end

.validate_origin(data) ⇒ Object



119
120
121
122
123
124
# File 'lib/simple-mappr/validator.rb', line 119

def self.validate_origin(data)
  validate_type(data, 'Integer')
  if data > 180 || data < -180
    raise InvalidParameterValue, "Accepted integer values are -180 <= x <= 180"
  end
end

.validate_output(data) ⇒ Object



133
134
135
136
137
# File 'lib/simple-mappr/validator.rb', line 133

def self.validate_output(data)
  if !OUTPUTS.include?(data)
    raise InvalidParameterValue, "Accepted outputs are #{OUTPUTS.join(", ")}"
  end
end

.validate_points(data) ⇒ Object



52
53
54
55
# File 'lib/simple-mappr/validator.rb', line 52

def self.validate_points(data)
  validate_type(data, 'Array')
  data.each { |item| validate_type(item, 'String') }
end

.validate_projection(data) ⇒ Object



113
114
115
116
117
# File 'lib/simple-mappr/validator.rb', line 113

def self.validate_projection(data)
  if !PROJECTIONS.include?(data)
    raise InvalidParameterValue, "Accepted projection value is one of #{PROJECTIONS.join(",")}"
  end
end

.validate_shade(data) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/simple-mappr/validator.rb', line 95

def self.validate_shade(data)
  validate_type(data, 'Hash')
  data = data.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
  if !(data.keys - [:places, :title, :color]).empty?
    raise InvalidParameterValue, "Shade must be Hash in the form { places: \"\", title: \"\", color: \"\" }"
  end
  if data.key?(:color)
    validate_color(data[:color])
  end
end

.validate_shadows(data) ⇒ Object



76
77
78
79
# File 'lib/simple-mappr/validator.rb', line 76

def self.validate_shadows(data)
  validate_type(data, 'Array')
  data.each { |item| validate_type(item, 'Boolean') }
end

.validate_shapes(data) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/simple-mappr/validator.rb', line 57

def self.validate_shapes(data)
  validate_type(data, 'Array')
  data.each do |item|
    if !SHAPES.include?(item)
      raise InvalidParameterValue, "Values must each be one of #{SHAPES.join(", ")}"
    end
  end
end

.validate_sizes(data) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/simple-mappr/validator.rb', line 66

def self.validate_sizes(data)
  validate_type(data, 'Array')
  data.each do |item|
    validate_type(item, 'Integer')
    if item > 14
      raise InvalidParameterValue, "Values must each be integers less than 14"
    end
  end
end

.validate_spacing(data) ⇒ Object



126
127
128
129
130
131
# File 'lib/simple-mappr/validator.rb', line 126

def self.validate_spacing(data)
  validate_type(data, 'Integer')
  if data > 10
    raise InvalidParameterValue, "Accepted integer values are <= 10"
  end
end

.validate_type(data, type) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'lib/simple-mappr/validator.rb', line 139

def self.validate_type(data, type)
  case type
  when 'String', 'Array', 'Integer', 'Hash'
    raise InvalidParameterValue, "Must be a #{type}" unless data.is_a?(Object.const_get(type))
  when 'Boolean'
    raise InvalidParameterValue, "Must be a Boolean" unless [true, false].include?(data)
  when 'File'
    raise InvalidParameterValue, "Must be a file path & file must exist" unless File.file?(data)
  end
end

.validate_url(data) ⇒ Object



81
82
83
84
85
86
# File 'lib/simple-mappr/validator.rb', line 81

def self.validate_url(data)
  validate_type(data, 'String')
  if data !~ /\A#{URI::regexp(['http', 'https'])}\z/
    raise InvalidParameterValue, "URL must be in the form http:// or https://"
  end
end

.validate_watermark(data) ⇒ Object



161
162
163
# File 'lib/simple-mappr/validator.rb', line 161

def self.validate_watermark(data)
  validate_type(data, 'Boolean')
end

.validate_wkt(data) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/simple-mappr/validator.rb', line 150

def self.validate_wkt(data)
  validate_type(data, 'Array')
  data.each { |item| validate_type(item, 'Hash') }
  if !(data[0].keys - [:data, :title, :color, :border]).empty?
    raise InvalidParameterValue, "wkt must be an Array of Hashes in the form [{ data: \"\", title: \"\", color: \"\", border: \"\" }]"
  end
  if data[0].key?(:color)
    validate_color(data[0][:color])
  end
end

.validate_zoom(data) ⇒ Object



88
89
90
91
92
93
# File 'lib/simple-mappr/validator.rb', line 88

def self.validate_zoom(data)
  validate_type(data, 'Integer')
  if data > 10
    raise InvalidParameterValue, "Accepted integer value is <= 10"
  end
end