Class: AllNewsAPI::Client

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(api_key, options = {}) ⇒ Client

Create a new AllNewsAPI client

Parameters:

  • api_key (String)

    Your AllNewsAPI key

  • options (Hash) (defaults to: {})

    Optional configuration options

Options Hash (options):

  • :base_url (String)

    The base URL for the API

  • :timeout (Integer)

    HTTP timeout in seconds (default: 30)



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/allnewsapi/client.rb', line 26

def initialize(api_key, options = {})
  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 = options[:base_url] || DEFAULT_BASE_URL
  @timeout = options[: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

Parameters:

  • options (Hash) (defaults to: {})

    Headlines options (same as search)

Returns:

  • (Hash, String)

    Headlines results (Hash for JSON, String for CSV/XLSX)



71
72
73
74
# File 'lib/allnewsapi/client.rb', line 71

def headlines(options = {})
  params = prepare_params(options)
  make_request(params, @headlines_endpoint)
end

#search(options = {}) ⇒ Hash, String

Search for news articles

Parameters:

  • options (Hash) (defaults to: {})

    Search options

Options Hash (options):

  • :q (String)

    Keywords to search for

  • :start_date (String, Date)

    Start date (YYYY-MM-DD or Date object)

  • :end_date (String, Date)

    End date (YYYY-MM-DD or Date object)

  • :content (Boolean)

    Whether to include full content

  • :lang (String, Array<String>)

    Language(s) to filter by

  • :country (String, Array<String>)

    Country/countries to filter by

  • :region (String, Array<String>)

    Region(s) to filter by

  • :category (String, Array<String>)

    Category/categories to filter by

  • :max (Integer)

    Maximum number of results (1-100)

  • :attributes (String, Array<String>)

    Attributes to search in

  • :page (Integer)

    Page number for pagination

  • :sortby (String)

    Sort by 'publishedAt' or 'relevance'

  • :publisher (String, Array<String>)

    Publisher(s) to filter by

  • :format (String)

    Response format (json, csv, xlsx)

  • :ai_sentiment (String)

    AI sentiment filter

  • :ai_entity_name (String)

    AI entity name filter

  • :ai_entity_type (String)

    AI entity type filter

Returns:

  • (Hash, String)

    Search results (Hash for JSON, String for CSV/XLSX)



61
62
63
64
# File 'lib/allnewsapi/client.rb', line 61

def search(options = {})
  params = prepare_params(options)
  make_request(params, @search_endpoint)
end

#usageHash

Get account usage statistics

Returns:

  • (Hash)

    Usage statistics



79
80
81
# File 'lib/allnewsapi/client.rb', line 79

def usage
  make_request({}, @usage_endpoint)
end