class Validate::NewsApiQueryValidator
Constants
- CATEGORIES
- COUNTRIES
Public Class Methods
new(api_key, args:, format:, endpoint: nil)
click to toggle source
# File lib/metonym/lib/validators/news_api_query_validator.rb, line 8 def initialize(api_key, args:, format:, endpoint: nil) raise 'API Key is required' unless key_present?(api_key) raise 'Invalid parameter sequence' unless query_valid?(args, endpoint) raise 'Invalid country sent' unless country_valid?(args) raise 'Invalid language sent' unless language_valid?(args) raise 'Invalid date or dates sent' unless date_valid?(args) true end
Private Instance Methods
category_valid?(args)
click to toggle source
# File lib/metonym/lib/validators/news_api_query_validator.rb, line 44 def category_valid?(args) template = %w[business entertainment general health science sports technology] template.include?(args[:country]) end
country_valid?(args)
click to toggle source
# File lib/metonym/lib/validators/news_api_query_validator.rb, line 40 def country_valid?(args) args[:country].nil? ? true : COUNTRIES.include?(args[:country].downcase) end
date_valid?(args)
click to toggle source
# File lib/metonym/lib/validators/news_api_query_validator.rb, line 49 def date_valid?(args) if args[:from].nil? && args[:to].nil? true elsif args[:from] && args[:to] (args[:from].respond_to?(:strftime) && args['to'].respond_to?(:strftime)) elsif args[:from] && args['to'].nil? args[:from].respond_to?(:strftime) elsif args[:from].nil? && args['to'] args[:from].respond_to?(:strftime) end end
query_valid?(args, endpoint = 'top-headlines')
click to toggle source
# File lib/metonym/lib/validators/news_api_query_validator.rb, line 20 def query_valid?(args, endpoint = 'top-headlines') case endpoint when 'top-headlines' template = %i[q country category sources page_size page] when 'everything' template = %i[q sources domains excludeDomains from to language sortBy pageSize page] when 'sources' template = %i[category language country] end if endpoint == 'top-headlines' && (args.include?(:sources) && (args.include?(:country) || args.include?(:category))) false elsif (endpoint == 'sources') && args.key?(:category) && (CATEGORIES.include?(args[:category]) == false) false else (args.keys - template).empty? end end