class Gnews::Query

Attributes

gnews_api_key[RW]
uri[RW]

Public Class Methods

new(key) click to toggle source
# File lib/metonym/lib/gnews.rb, line 8
def initialize(key)
  @gnews_api_key = key
  @gnews = Gnews::GnewsRepository.new(key)
end

Public Instance Methods

top_news(args, **options) click to toggle source
# File lib/metonym/lib/gnews.rb, line 17
def top_news(args, **options)
  request('top-news', args: args, format: define_response_format(options))
end

Private Instance Methods

define_response_format(options) click to toggle source
# File lib/metonym/lib/gnews.rb, line 23
def define_response_format(options)
  options[:format] || 'json'
end
request(resource, args:, format:) click to toggle source
# File lib/metonym/lib/gnews.rb, line 27
def request(resource, args:, format:)
  validate_query_parameters(args, format)

  case resource
  when 'search'
    response = @gnews.search(query: args)
  when 'top-news'
    response = @gnews.top_news(query: args)
  when 'topics'
    response = @gnews.topics(query: args)
  end
rescue StandardError => e
  response = { errors: e.to_s }
ensure
  presenter = Gnews::GnewsResponsePresenter.new
  return presenter.generate_formatted_response(response, format)
end
validate_query_parameters(args, format) click to toggle source
# File lib/metonym/lib/gnews.rb, line 45
def validate_query_parameters(args, format)
  v = Validate::GnewsQueryValidator.new(@gnews_api_key,
                                        args: args, format: format)
  raise v unless v
end