Module: Yourub::Validator
- Defined in:
- lib/yourub/validator.rb
Constant Summary collapse
- COUNTRIES =
Yourub::CountryCodes::ISO_3166_1_ALPHA2
- ORDERS =
['date', 'rating', 'relevance', 'title', 'videoCount', 'viewCount']
- VALID_PARAMS =
[:country, :category, :query, :max_results, :count_filter, :order ]
Class Method Summary collapse
- .available_countries ⇒ Object
- .confirm(criteria) ⇒ Object
- .countries_to_array ⇒ Object
- .remove_empty_and_non_valid_params ⇒ Object
- .set_filter_count_options ⇒ Object
- .symbolize_keys(hash) ⇒ Object
- .valid_category(categories, selected_category) ⇒ Object
- .valid_format?(criteria) ⇒ Boolean
- .validate_countries ⇒ Object
- .validate_max_results ⇒ Object
- .validate_order ⇒ Object
- .validate_search_query ⇒ Object
Class Method Details
.available_countries ⇒ Object
110 111 112 |
# File 'lib/yourub/validator.rb', line 110 def available_countries COUNTRIES end |
.confirm(criteria) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/yourub/validator.rb', line 9 def confirm(criteria) valid_format?(criteria) @criteria = symbolize_keys(criteria) remove_empty_and_non_valid_params validate_search_query validate_order validate_max_results countries_to_array validate_countries @criteria end |
.countries_to_array ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/yourub/validator.rb', line 47 def countries_to_array if @criteria.has_key? :country if @criteria[:country].is_a?(String) @criteria[:country] = @criteria[:country].split(',').collect(&:strip) end @criteria[:country] = @criteria[:country].to_a end end |
.remove_empty_and_non_valid_params ⇒ Object
41 42 43 44 45 |
# File 'lib/yourub/validator.rb', line 41 def remove_empty_and_non_valid_params @criteria.reject! do |k, v| !VALID_PARAMS.include?(k) || v.nil? || (v.respond_to?(:empty?) && v.empty?) end end |
.set_filter_count_options ⇒ Object
56 57 58 59 60 |
# File 'lib/yourub/validator.rb', line 56 def if @criteria.has_key? :count_filter Yourub::CountFilter.filter = @criteria.delete(:count_filter) end end |
.symbolize_keys(hash) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/yourub/validator.rb', line 25 def symbolize_keys(hash) hash.inject({}){|result, (key, value)| new_key = case key when String then key.to_sym else key end new_value = case value when Hash then symbolize_keys(value) else value end result[new_key] = new_value result } end |
.valid_category(categories, selected_category) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/yourub/validator.rb', line 62 def valid_category(categories, selected_category) categories = categories.select {|k| k.has_value?(selected_category.downcase.gsub(/\s+/, ""))} if categories.first.nil? raise ArgumentError.new("The category #{selected_category} does not exists...") end return categories end |
.valid_format?(criteria) ⇒ Boolean
70 71 72 73 74 |
# File 'lib/yourub/validator.rb', line 70 def valid_format?(criteria) raise ArgumentError.new( "give an hash as search criteria" ) unless( criteria.is_a? Hash ) end |
.validate_countries ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/yourub/validator.rb', line 92 def validate_countries return unless @criteria.has_key?(:country) unknown = @criteria[:country] - COUNTRIES return if unknown.empty? raise ArgumentError, "invalid ISO 3166-1 alpha-2 country code(s): #{unknown.join(', ')}" end |
.validate_max_results ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/yourub/validator.rb', line 102 def validate_max_results raise ArgumentError.new( 'max 50 videos pro categories or country' ) unless( @criteria[:max_results].to_i < 51 || @criteria[:max_results].to_i == 0 ) end |
.validate_order ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/yourub/validator.rb', line 84 def validate_order if @criteria.has_key? :order raise ArgumentError.new( "the given order is not in the available ones: #{ORDERS.join(',')}" ) unless( ORDERS.include? @criteria[:order] ) end end |
.validate_search_query ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/yourub/validator.rb', line 76 def validate_search_query q = @criteria[:query] if !@criteria.key?(:query) || q.nil? || q.to_s.strip.empty? raise ArgumentError, "search requires a :query parameter." end end |