Class: SpiderMonkey::Validator
- Inherits:
-
Object
- Object
- SpiderMonkey::Validator
- Defined in:
- lib/spider_monkey/validator.rb
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Validator
constructor
A new instance of Validator.
- #is_allowed_annotate_key?(symbol) ⇒ Boolean
- #is_allowed_composite_key?(symbol) ⇒ Boolean
- #is_allowed_root_key?(symbol) ⇒ Boolean
- #is_boolean?(input) ⇒ Boolean
- #is_color?(input) ⇒ Boolean
- #is_colorspace?(input) ⇒ Boolean
- #is_gravity?(input) ⇒ Boolean
- #is_integer?(input) ⇒ Boolean
- #is_integer_like?(input) ⇒ Boolean
- #is_percent?(input) ⇒ Boolean
- #is_resize_method?(input) ⇒ Boolean
- #is_source?(input, prefix = nil) ⇒ Boolean
- #is_string?(input) ⇒ Boolean
- #is_symbol?(input) ⇒ Boolean
- #is_url?(input) ⇒ Boolean
- #validate_option(validation_type, key, required = false) ⇒ Object
- #validate_options ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Validator
Returns a new instance of Validator.
3 4 5 |
# File 'lib/spider_monkey/validator.rb', line 3 def initialize( = {}) = end |
Instance Method Details
#is_allowed_annotate_key?(symbol) ⇒ Boolean
264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/spider_monkey/validator.rb', line 264 def is_allowed_annotate_key?(symbol) [ :gravity, :weight, :pointsize, :color, :translate_x, :translate_y, :text ].include?(symbol) end |
#is_allowed_composite_key?(symbol) ⇒ Boolean
276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/spider_monkey/validator.rb', line 276 def is_allowed_composite_key?(symbol) [ :url, :key, :bucket, :width, :height, :resize_gravity, :disolve_percent ].include?(symbol) end |
#is_allowed_root_key?(symbol) ⇒ Boolean
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/spider_monkey/validator.rb', line 242 def is_allowed_root_key?(symbol) [ :key, :source_url, :source_key, :source_bucket, :read_density, :read_colorspace, :quality, :width, :height, :resize_method, :resize_gravity, :thumbnail, :density, :colorspace, :annotate, :composite, :background_color ].include?(symbol) end |
#is_boolean?(input) ⇒ Boolean
122 123 124 |
# File 'lib/spider_monkey/validator.rb', line 122 def is_boolean?(input) input.kind_of?(TrueClass) || input.kind_of?(FalseClass) end |
#is_color?(input) ⇒ Boolean
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/spider_monkey/validator.rb', line 126 def is_color?(input) # This is really just a semi-validator. return false unless is_string?(input) || is_symbol?(input) input = input.to_s if input.starts_with?("#") # We have a hash color # All Valid: # #00F # #0000FF # #0000FFFF (w/ alpha) return false unless [4, 7, 9].include?(input.length) hex_input = input[1..(input.length)] # everything except the first character return !hex_input[/\H/] elsif input.starts_with?("rgba") # 1 of each parentheses and 3 commas return input.scan("(").count == 1 && input.scan(")").count == 1 && input.scan(",").count == 3 elsif input.starts_with?("rgb") # 1 of each parentheses and 2 commas return input.scan("(").count == 1 && input.scan(")").count == 1 && input.scan(",").count == 2 else # Some other string. We're going to just assume it's a color. In an # ideal world we would check it against the valid list, but that's way # too detailed. return true end end |
#is_colorspace?(input) ⇒ Boolean
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/spider_monkey/validator.rb', line 169 def is_colorspace?(input) %w( CIELab CMY CMYK Gray HCL HCLp HSB HSI HSL HSV HWB Lab LCH LCHab LCHuv LMS Log Luv OHTA Rec601Luma Rec601YCbCr Rec709Luma Rec709YCbCr RGB scRGB sRGB Transparent XYZ xyY YCbCr YDbDr YCC YIQ YPbPr YUV ).include?(input.to_s) end |
#is_gravity?(input) ⇒ Boolean
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/spider_monkey/validator.rb', line 155 def is_gravity?(input) %w( northwest north northeast west center east southwest south southeast ).include?(input.to_s.downcase) end |
#is_integer?(input) ⇒ Boolean
110 111 112 |
# File 'lib/spider_monkey/validator.rb', line 110 def is_integer?(input) input.kind_of? Integer end |
#is_integer_like?(input) ⇒ Boolean
106 107 108 |
# File 'lib/spider_monkey/validator.rb', line 106 def is_integer_like?(input) is_integer?(input) || /\A[-+]?\d+\z/ === input end |
#is_percent?(input) ⇒ Boolean
209 210 211 212 |
# File 'lib/spider_monkey/validator.rb', line 209 def is_percent?(input) # String, with a percent sign, and an integer input.kind_of?(String) && input.include?('%') && is_integer_like?(input.gsub('%', '')) end |
#is_resize_method?(input) ⇒ Boolean
219 220 221 222 |
# File 'lib/spider_monkey/validator.rb', line 219 def is_resize_method?(input) return false unless is_string?(input) || is_symbol?(input) ["crop", "fit"].include? input.to_s.downcase end |
#is_source?(input, prefix = nil) ⇒ Boolean
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/spider_monkey/validator.rb', line 224 def is_source?(input, prefix = nil) return false unless input.kind_of? Hash if prefix.present? prefix = prefix + "_" end if input.has_key?("#{prefix}url".to_sym) return false unless !input.has_key?("#{prefix}bucket".to_sym) && !input.has_key?("#{prefix}key".to_sym) return is_url?(input["#{prefix}url".to_sym]) elsif (input.has_key?("#{prefix}bucket".to_sym) && input.has_key?("#{prefix}key".to_sym)) return false unless !input.has_key?("#{prefix}url".to_sym) return (is_string?(input["#{prefix}bucket".to_sym]) && is_string?(input["#{prefix}key".to_sym])) else return false end end |
#is_string?(input) ⇒ Boolean
114 115 116 |
# File 'lib/spider_monkey/validator.rb', line 114 def is_string?(input) input.kind_of?(String) && !input.blank? end |
#is_symbol?(input) ⇒ Boolean
118 119 120 |
# File 'lib/spider_monkey/validator.rb', line 118 def is_symbol?(input) input.kind_of? Symbol end |
#is_url?(input) ⇒ Boolean
214 215 216 217 |
# File 'lib/spider_monkey/validator.rb', line 214 def is_url?(input) # Super lame test is_string?(input) && ( input.starts_with?("http://") || input.starts_with?("https://") ) && input.length > 10 end |
#validate_option(validation_type, key, required = false) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/spider_monkey/validator.rb', line 89 def validate_option(validation_type, key, required = false) if [key].present? if send("is_#{validation_type}?".to_sym, [key]) [key] = [key] else @recoverable = false << "Invalid parameter: #{key}." [key] = [key] end elsif required @recoverable = false << "Missing parameter: #{key}" end .delete(key) end |
#validate_options ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/spider_monkey/validator.rb', line 7 def @passed = true @recoverable = true = {} = {} = [] .each do |key, value| # First, lets loop through them all and remove any invalid keys if !is_allowed_root_key?(key) [key] = value .delete(key) << "Extra Key present: #{key}" end end if is_source?(, "source") [:source_url] = [:source_url] if [:source_url] [:source_key] = [:source_key] if [:source_key] [:source_bucket] = [:source_bucket] if [:source_bucket] else << "Invalid Source." @recoverable = false [:source_url] = [:source_url] if [:source_url] [:source_key] = [:source_key] if [:source_key] [:source_bucket] = [:source_bucket] if [:source_bucket] end .delete(:source_url) .delete(:source_key) .delete(:source_bucket) validate_option(:colorspace, :read_colorspace) validate_option(:integer_like, :read_density) validate_option(:integer_like, :quality) validate_option(:integer_like, :width, true) validate_option(:integer_like, :height, true) validate_option(:resize_method, :resize_method) validate_option(:gravity, :resize_gravity) validate_option(:boolean, :thumbnail) validate_option(:integer_like, :density) validate_option(:string, :key, true) validate_option(:colorspace, :colorspace) validate_option(:color, :background_color) #:annotate, [:annotate] = [:annotate] if [:annotate] #:composite [:composite] = [:composite] if [:composite] #:annotate # array, or single element # :gravity # :weight integer # :pointsize interger # :color "blue", "#123456", "rgb(1,1,1)", "rgba(12,12,12)" # :translate_x integer # :translate_y integer # :text (required) string # :composite # :url / :key && :bucket # width numeric or percentage # height # :resize_gravity # :disolve_percent optional - integer 0-100 if .size > 0 @passed = false end SpiderMonkey.configuration[:validation_error_handler].call(, @recoverable, , ) unless @passed return { passed: @passed, recoverable: @recoverable, valid_options: } end |