Class: Joshua::Params::Parse
- Inherits:
-
Object
- Object
- Joshua::Params::Parse
- Defined in:
- lib/joshua/params/parse.rb,
lib/joshua/params/types.rb,
lib/joshua/params/types_errors.rb
Constant Summary collapse
- ERRORS =
{ en:{ bad_format: 'Bad value format', not_integer: 'Not an integer', min_value: 'Minimal allowed value is: %s', max_value: 'Maximal allowed value is: %s', email_min: 'Email requireds min of 8 characters', email_missing: 'Email is missing @', url_start: 'URL is not starting with http or https', point_format: 'Point should be in format 1.2345678,1.2345678', min_date: 'Minimal allow date is %s', max_date: 'Maximal allow date is %s' }, hr: { bad_format: 'Format vrijednosti ne zadovoljava', not_integer: 'Nije cijeli broj', min_value: 'Minimalna dozvoljena vrijednost je: %s', max_value: 'Maksimalna dozvoljena vrijednost je: %s', email_min: 'Email zatjeva minimalno 8 znakova', email_missing: 'U email-u nedostaje @', url_start: 'URL ne započinje sa http ili https', point_format: 'Geo točka bi trebala biti u formatu 1.2345678,1.2345678', min_date: 'Minimalni dozvoljeni datum je %s', max_date: 'Maksimalni dozvoljeni datum je %s' } }
Class Method Summary collapse
Instance Method Summary collapse
-
#check(type, value, opts = {}) ⇒ Object
check :boolean, ‘on’.
-
#check_boolean(value, opts = {}) ⇒ Object
params.is_active :boolean, default: false.
- #check_date(value, opts = {}) ⇒ Object
- #check_date_time(value, opts = {}) ⇒ Object
- #check_email(email, opts = {}) ⇒ Object
- #check_float(value, opts = {}) ⇒ Object
- #check_hash(value, opts = {}) ⇒ Object
- #check_integer(value, opts = {}) ⇒ Object
- #check_oib(oib, opts = {}) ⇒ Object
-
#check_point(value, opts = {}) ⇒ Object
geolocation point.
- #check_string(value, opts = {}) ⇒ Object
- #check_url(url) ⇒ Object
Class Method Details
.define(name, &block) ⇒ Object
5 6 7 8 9 |
# File 'lib/joshua/params/parse.rb', line 5 def define name, &block define_method 'check_%s' % name do |value, opts| block.call value, opts || {} end end |
Instance Method Details
#check(type, value, opts = {}) ⇒ Object
check :boolean, ‘on’
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/joshua/params/parse.rb', line 15 def check type, value, opts={} opts[:required] = true if opts.delete(:req) if value.to_s == '' if !opts[:default].nil? opts[:default] elsif opts[:required] error 'Argument missing' end else m = 'check_%s' % type hard_error 'Unsupported paramter type: %s' % type unless respond_to?(m) if opts[:array] delimiter = opts[:array].is_a?(TrueClass) ? /\s*[,:;]\s*/ : opts[:array] value = value.split(delimiter) unless value.is_a?(Array) value.map { |_| check_send m, _, opts } else check_send m, value, opts end end end |
#check_boolean(value, opts = {}) ⇒ Object
params.is_active :boolean, default: false
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/joshua/params/types.rb', line 7 def check_boolean value, opts={} return false unless value if %w(true 1 on).include?(value.to_s) true elsif %w(false 0 off).include?(value.to_s) false else error 'Unsupported boolean param value: %s' % value end end |
#check_date(value, opts = {}) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/joshua/params/types.rb', line 48 def check_date value, opts={} date = DateTime.parse(value) date = DateTime.new(date.year, date.month, date.day) check_date_min_max date, opts end |
#check_date_time(value, opts = {}) ⇒ Object
55 56 57 58 |
# File 'lib/joshua/params/types.rb', line 55 def check_date_time value, opts={} date = DateTime.parse(value) check_date_min_max date, opts end |
#check_email(email, opts = {}) ⇒ Object
72 73 74 75 76 |
# File 'lib/joshua/params/types.rb', line 72 def check_email email, opts={} error localized(:email_min) unless email.to_s.length > 7 error localized(:email_missing) unless email.include?('@') email.downcase end |
#check_float(value, opts = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/joshua/params/types.rb', line 34 def check_float value, opts={} value = if opts[:round] value.to_f.round(opts[:round]) else value.to_f end error localized(:min_value) % opts[:min] if opts[:min] && value < opts[:min] error localized(:max_value) % opts[:max] if opts[:max] && value > opts[:max] value end |
#check_hash(value, opts = {}) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/joshua/params/types.rb', line 60 def check_hash value, opts={} value = {} unless value.is_a?(Hash) if opts[:allow] for key in value.keys value.delete(key) unless opts[:allow].include?(key) end end value end |
#check_integer(value, opts = {}) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/joshua/params/types.rb', line 19 def check_integer value, opts={} value.to_i.tap do |test| error localized(:not_integer) if test.to_s != value.to_s error localized(:min_value) % opts[:min] if opts[:min] && test < opts[:min] error localized(:max_value) % opts[:max] if opts[:max] && test > opts[:max] end end |
#check_oib(oib, opts = {}) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/joshua/params/types.rb', line 98 def check_oib oib, opts={} oib = oib.to_s return false unless oib.match(/^[0-9]{11}$/) control_sum = (0..9).inject(10) do |middle, position| middle += oib.at(position).to_i middle %= 10 middle = 10 if middle == 0 middle *= 2 middle %= 11 end control_sum = 11 - control_sum control_sum = 0 if control_sum == 10 if control_sum == oib.at(10).to_i oib.to_i else error 'Wrong OIB' end end |
#check_point(value, opts = {}) ⇒ Object
geolocation point. google maps url will be automaticly converted www.google.com/maps/@51.5254742,-0.1057319,13z
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/joshua/params/types.rb', line 85 def check_point value, opts={} parts = value.split(/\s*,\s*/) unless parts.is_a?(Array) error localized(:point_format) unless parts[1] for part in parts error localized(:point_format) unless part.include?('.') error localized(:point_format) unless part.length > 5 end parts.join(',') end |
#check_string(value, opts = {}) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/joshua/params/types.rb', line 27 def check_string value, opts={} value .to_s .sub(/^\s+/, '') .sub(/\s+$/, '') end |
#check_url(url) ⇒ Object
78 79 80 81 |
# File 'lib/joshua/params/types.rb', line 78 def check_url url error localized(:url_start) unless url =~ /^https?:\/\/./ url end |