Class: NewsAggregator::News

Inherits:
Object
  • Object
show all
Defined in:
lib/news_aggregator/news.rb

Overview

Locate articles and breaking news headlines from news sources and blogs across the web with newsapi.org

Constant Summary collapse

BASE_URL =
"https://newsapi.org/v2"
END_POINTS =
%w[top-headlines everything sources].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, adapter: Faraday.default_adapter) ⇒ News

Returns a new instance of News.



16
17
18
19
# File 'lib/news_aggregator/news.rb', line 16

def initialize(api_key:, adapter: Faraday.default_adapter)
  @api_key = api_key
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



14
15
16
# File 'lib/news_aggregator/news.rb', line 14

def adapter
  @adapter
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



14
15
16
# File 'lib/news_aggregator/news.rb', line 14

def api_key
  @api_key
end

Instance Method Details

#connectionObject



21
22
23
24
25
26
27
28
# File 'lib/news_aggregator/news.rb', line 21

def connection
  @connection ||= Faraday.new do |conn|
    conn.url_prefix = BASE_URL
    conn.request :json
    conn.response :json, content_type: "application/json"
    conn.adapter adapter
  end
end

#make_request(endpoint, **query_params) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/news_aggregator/news.rb', line 30

def make_request(endpoint, **query_params)
  response = connection.get(endpoint, query_params, { authorization: "bearer #{api_key}" })

  return response.body if response.status == 200

  raise_exception(response)
end