Module: Yourub::CountFilter
- Defined in:
- lib/yourub/count_filter.rb
Constant Summary collapse
- VIEWS_COUNT_NUMBER =
/\d*\z/
- ARITHMETIC_OPERATOR =
/\A<(?!=)|>(?!=)|<=|>=|==|!=/
Class Attribute Summary collapse
-
.filter ⇒ Object
Returns the value of attribute filter.
Class Method Summary collapse
- .accept?(entry) ⇒ Boolean
- .apply_filter(video) ⇒ Object
- .get_views_count(video) ⇒ Object
- .validate_count_filter ⇒ Object
- .validate_filter ⇒ Object
Class Attribute Details
.filter ⇒ Object
Returns the value of attribute filter.
5 6 7 |
# File 'lib/yourub/count_filter.rb', line 5 def filter @filter end |
Class Method Details
.accept?(entry) ⇒ Boolean
10 11 12 13 14 |
# File 'lib/yourub/count_filter.rb', line 10 def accept?(entry) return true if @filter.nil? validate_filter apply_filter(entry) end |
.apply_filter(video) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/yourub/count_filter.rb', line 30 def apply_filter(video) operator = @filter[:views].match(ARITHMETIC_OPERATOR).to_s number_to_compare = @filter[:views].match(VIEWS_COUNT_NUMBER).to_s.to_i number_founded = get_views_count(video) return number_founded.send(operator, number_to_compare) end |
.get_views_count(video) ⇒ Object
37 38 39 40 |
# File 'lib/yourub/count_filter.rb', line 37 def get_views_count video return 0 if video['statistics'].nil? return video['statistics']["viewCount"].to_i end |
.validate_count_filter ⇒ Object
23 24 25 26 27 28 |
# File 'lib/yourub/count_filter.rb', line 23 def validate_count_filter count_number_invalid = (@filter[:views] =~ VIEWS_COUNT_NUMBER).nil? operator_invalid = (@filter[:views] =~ ARITHMETIC_OPERATOR).nil? raise ArgumentError.new("arithmetic operator not detected") if operator_invalid raise ArgumentError.new("view count number not detected") if count_number_invalid end |
.validate_filter ⇒ Object
16 17 18 19 20 21 |
# File 'lib/yourub/count_filter.rb', line 16 def validate_filter unless @filter.is_a?(Hash) raise ArgumentError.new("The filter #{@filter} should be an Hash") end validate_count_filter end |