Class: AllNewsAPI::Client
- Inherits:
-
Object
- Object
- AllNewsAPI::Client
- Defined in:
- lib/allnewsapi/client.rb
Overview
Main SDK client for interacting with AllNewsAPI
Constant Summary collapse
- DEFAULT_BASE_URL =
Default base URL for the AllNewsAPI
'https://api.allnewsapi.com'- DEFAULT_TIMEOUT =
Default HTTP timeout in seconds
30- AI_PARAMS =
Parameters that should NOT be converted from snake_case to camelCase
%w[ai_sentiment ai_entity_name ai_entity_type].freeze
Instance Method Summary collapse
-
#headlines(options = {}) ⇒ Hash, String
Get top headlines.
-
#initialize(api_key, options = {}) ⇒ Client
constructor
Create a new AllNewsAPI client.
-
#search(options = {}) ⇒ Hash, String
Search for news articles.
-
#usage ⇒ Hash
Get account usage statistics.
Constructor Details
#initialize(api_key, options = {}) ⇒ Client
Create a new AllNewsAPI client
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/allnewsapi/client.rb', line 26 def initialize(api_key, = {}) if api_key.nil? || api_key.to_s.strip.empty? raise Error.new(401, 'Unauthorized - Invalid API Key or Account status is inactive') end @api_key = api_key @base_url = [:base_url] || DEFAULT_BASE_URL @timeout = [:timeout] || DEFAULT_TIMEOUT @search_endpoint = "#{@base_url}/search" @headlines_endpoint = "#{@base_url}/headlines" @usage_endpoint = "#{@base_url}/usage" end |
Instance Method Details
#headlines(options = {}) ⇒ Hash, String
Get top headlines
71 72 73 74 |
# File 'lib/allnewsapi/client.rb', line 71 def headlines( = {}) params = prepare_params() make_request(params, @headlines_endpoint) end |
#search(options = {}) ⇒ Hash, String
Search for news articles
61 62 63 64 |
# File 'lib/allnewsapi/client.rb', line 61 def search( = {}) params = prepare_params() make_request(params, @search_endpoint) end |
#usage ⇒ Hash
Get account usage statistics
79 80 81 |
# File 'lib/allnewsapi/client.rb', line 79 def usage make_request({}, @usage_endpoint) end |