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, :id, :max_results, :count_filter, :order ]
MINIMUM_PARAMS =
[:country, :category, :query, :id]

Class Method Summary collapse

Class Method Details

.add_default_country_if_category_is_presentObject



64
65
66
67
68
# File 'lib/yourub/validator.rb', line 64

def add_default_country_if_category_is_present
  if (@criteria.has_key? :category) && (!@criteria.has_key? :country)
    @criteria[:country] = [ DEFAULT_COUNTRY ]
  end
end

.available_countriesObject



124
125
126
# File 'lib/yourub/validator.rb', line 124

def available_countries
  COUNTRIES
end

.confirm(criteria) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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?

  keep_only_the_id_if_present
  validate_order
  countries_to_array
  add_default_country_if_category_is_present
  validate_countries
  set_filter_count_options

  @criteria
end

.countries_to_arrayObject



55
56
57
58
59
60
61
62
# File 'lib/yourub/validator.rb', line 55

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

.keep_only_the_id_if_presentObject



49
50
51
52
53
# File 'lib/yourub/validator.rb', line 49

def keep_only_the_id_if_present
  if @criteria.has_key? :id
    @criteria.keep_if{|k, _| k == :id}
  end
end

.minimum_param_present?Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
# File 'lib/yourub/validator.rb', line 92

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_paramsObject



45
46
47
# File 'lib/yourub/validator.rb', line 45

def remove_empty_and_non_valid_params
  @criteria.keep_if{|k,v| ( (VALID_PARAMS.include? k) && v.size > 0) }
end

.set_filter_count_optionsObject



70
71
72
73
74
# File 'lib/yourub/validator.rb', line 70

def set_filter_count_options
  if @criteria.has_key? :count_filter
    Yourub::CountFilter.filter = @criteria.delete(:count_filter)
  end
end

.symbolize_keys(hash) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/yourub/validator.rb', line 30

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



76
77
78
79
80
81
82
83
84
# File 'lib/yourub/validator.rb', line 76

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

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


86
87
88
89
90
# File 'lib/yourub/validator.rb', line 86

def valid_format?(criteria)
  raise ArgumentError.new(
    "give an hash as search criteria"
  ) unless( criteria.is_a? Hash )
end

.validate_countriesObject



108
109
110
111
112
113
114
# File 'lib/yourub/validator.rb', line 108

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_resultsObject

Raises:

  • (ArgumentError)


116
117
118
119
120
121
122
# File 'lib/yourub/validator.rb', line 116

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_orderObject



100
101
102
103
104
105
106
# File 'lib/yourub/validator.rb', line 100

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