Module: Yourub::Validator
- Defined in:
- lib/yourub/validator.rb
Constant Summary collapse
- DEFAULT_COUNTRY =
"US"
- COUNTRIES =
[ 'AR','AU','AT','BE','BR','CA','CL','CO','CZ','EG','FR','DE','GB','HK', 'HU','IN','IE','IL','IT','JP','JO','MY','MX','MA','NL','NZ','PE','PH', 'PL','RU','SA','SG','ZA','KR','ES','SE','CH','TW','AE','US']
- ORDERS =
['date', 'rating', 'relevance', 'title', 'videoCount', 'viewCount']
- VALID_PARAMS =
[:country, :category, :query, :max_results, :count_filter, :order ]
- MINIMUM_PARAMS =
[:country, :category, :query]
Class Method Summary collapse
- .add_default_country_if_category_is_present ⇒ Object
- .available_countries ⇒ Object
- .confirm(criteria) ⇒ Object
- .countries_to_array ⇒ Object
- .minimum_param_present? ⇒ Boolean
- .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
Class Method Details
.add_default_country_if_category_is_present ⇒ Object
57 58 59 60 61 |
# File 'lib/yourub/validator.rb', line 57 def add_default_country_if_category_is_present if (@criteria.has_key? :category) && (!@criteria.has_key? :country) @criteria[:country] = [ DEFAULT_COUNTRY ] end end |
.available_countries ⇒ Object
117 118 119 |
# File 'lib/yourub/validator.rb', line 117 def available_countries COUNTRIES end |
.confirm(criteria) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/yourub/validator.rb', line 13 def confirm(criteria) valid_format?(criteria) @criteria = symbolize_keys(criteria) remove_empty_and_non_valid_params minimum_param_present? validate_order countries_to_array add_default_country_if_category_is_present validate_countries @criteria end |
.countries_to_array ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/yourub/validator.rb', line 48 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 |
.minimum_param_present? ⇒ Boolean
85 86 87 88 89 90 91 |
# File 'lib/yourub/validator.rb', line 85 def minimum_param_present? if @criteria.none?{|k,_| MINIMUM_PARAMS.include? k} raise ArgumentError.new( "minimum params to start a search is at least one of: #{MINIMUM_PARAMS.join(',')}" ) end end |
.remove_empty_and_non_valid_params ⇒ Object
44 45 46 |
# File 'lib/yourub/validator.rb', line 44 def remove_empty_and_non_valid_params @criteria.keep_if{|k,v| ( (VALID_PARAMS.include? k) && v.size > 0) } end |
.set_filter_count_options ⇒ Object
63 64 65 66 67 |
# File 'lib/yourub/validator.rb', line 63 def if @criteria.has_key? :count_filter Yourub::CountFilter.filter = @criteria.delete(:count_filter) end end |
.symbolize_keys(hash) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/yourub/validator.rb', line 29 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
69 70 71 72 73 74 75 76 77 |
# File 'lib/yourub/validator.rb', line 69 def valid_category(categories, selected_category) return categories if selected_category == 'all' categories = categories.select {|k| k.has_value?(selected_category.downcase)} if categories.first.nil? raise ArgumentError.new( "The category #{selected_category} does not exists in the following ones: #{categories.join(',')}") end return categories end |
.valid_format?(criteria) ⇒ Boolean
79 80 81 82 83 |
# File 'lib/yourub/validator.rb', line 79 def valid_format?(criteria) raise ArgumentError.new( "give an hash as search criteria" ) unless( criteria.is_a? Hash ) end |
.validate_countries ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/yourub/validator.rb', line 101 def validate_countries if @criteria.has_key? :country raise ArgumentError.new( "the given country is not in the available ones: #{COUNTRIES.join(',')}" ) unless( (@criteria[:country] - COUNTRIES).size == 0 ) end end |
.validate_max_results ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/yourub/validator.rb', line 109 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
93 94 95 96 97 98 99 |
# File 'lib/yourub/validator.rb', line 93 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 |