Module: HomeAway::API::Util::Validators
- Defined in:
- lib/homeaway/api/util/validators.rb
Class Method Summary collapse
- .array(input) ⇒ Object
- .boolean(input) ⇒ Object
- .camel_case(input) ⇒ Object
- .date(input) ⇒ Object
- .float(input, min = nil, max = nil) ⇒ Object
- .integer(input, min = nil, max = nil) ⇒ Object
- .query_keys(hash) ⇒ Object
- .snake_case(input) ⇒ Object
- .uri_encoded(input) ⇒ Object
- .uuid(input) ⇒ Object
Class Method Details
.array(input) ⇒ Object
111 112 113 114 |
# File 'lib/homeaway/api/util/validators.rb', line 111 def self.array(input) return nil if input == nil [input].flatten end |
.boolean(input) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/homeaway/api/util/validators.rb', line 76 def self.boolean(input) input_s = input.to_s return true if input_s =~ (/^(true)$/i) return false if input_s.empty? || input_s =~ (/^(false)$/i) raise ArgumentError.new("Supplied argument #{input} must be a valid boolean value") end |
.camel_case(input) ⇒ Object
84 85 86 |
# File 'lib/homeaway/api/util/validators.rb', line 84 def self.camel_case(input) input.to_s.camelize(:lower) end |
.date(input) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/homeaway/api/util/validators.rb', line 31 def self.date(input) if input.is_a? String input = Chronic.parse(input, :ambiguous_time_range => :none) end raise ArgumentError.new('dates must be a parseable date string or a Ruby Time object') unless input.is_a? Time input.to_date.to_s end |
.float(input, min = nil, max = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/homeaway/api/util/validators.rb', line 40 def self.float(input, min=nil, max=nil) begin input_s = input.to_s !!Float(input_s) f = input_s.to_f unless min == nil raise ArgumentError.new("Supplied argument #{f} must be greater than #{min}") if f < min end unless max == nil raise ArgumentError.new("Supplied argument #{f} must be less than #{max}") if f > max end return f rescue raise ArgumentError.new("Supplied argument #{input} must be a valid float value") end end |
.integer(input, min = nil, max = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/homeaway/api/util/validators.rb', line 58 def self.integer(input, min=nil, max=nil) begin input_s = input.to_s !!Integer(input_s) i = input_s.to_i unless min == nil raise ArgumentError.new("Supplied argument #{i} must be greater than #{min}") if i < min end unless max == nil raise ArgumentError.new("Supplied argument #{i} must be less than #{max}") if i > max end return i rescue raise ArgumentError.new("Supplied argument #{input} must be a valid integer value") end end |
.query_keys(hash) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/homeaway/api/util/validators.rb', line 94 def self.query_keys(hash) raise ArgumentError.new('arguments must be a hash') unless hash.is_a? Hash transformed = {} hash.each_key do |key| transformed[self.camel_case(key)] = hash[key] end transformed end |
.snake_case(input) ⇒ Object
89 90 91 |
# File 'lib/homeaway/api/util/validators.rb', line 89 def self.snake_case(input) input.to_s.underscore end |
.uri_encoded(input) ⇒ Object
26 27 28 |
# File 'lib/homeaway/api/util/validators.rb', line 26 def self.uri_encoded(input) CGI.escape input end |
.uuid(input) ⇒ Object
104 105 106 107 108 |
# File 'lib/homeaway/api/util/validators.rb', line 104 def self.uuid(input) uuid_regex = /\A([0-9a-fA-F]{32}|[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})\z/ raise ArgumentError.new("#{uuid.to_s} must be a valid uuid") unless input.to_s =~ uuid_regex input.to_s end |