Class: NewsAggregator::News
- Inherits:
-
Object
- Object
- NewsAggregator::News
- 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
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
Instance Method Summary collapse
- #connection ⇒ Object
-
#initialize(api_key:, adapter: Faraday.default_adapter) ⇒ News
constructor
A new instance of News.
- #make_request(endpoint, **query_params) ⇒ Object
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
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
14 15 16 |
# File 'lib/news_aggregator/news.rb', line 14 def adapter @adapter end |
#api_key ⇒ Object (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
#connection ⇒ Object
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 |