Class: Alpaca::News::Api::Rest::News

Inherits:
Object
  • Object
show all
Defined in:
lib/alpaca/news/api/rest/news.rb

Overview

Alpaca REST API News class

Direct Known Subclasses

HistoricalNews

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers = {}) ⇒ News

Create new News object

Parameters:

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

    a customizable set of options

Options Hash (headers):

  • Apca-Api-Key-Id (String, Symbol)

    Alpaca API Key ID

  • Apca-Api-Secret-Key (String, Symbol)

    Alpaca API Secret Key

  • User-Agent (String, Symbol)

    User agent



21
22
23
# File 'lib/alpaca/news/api/rest/news.rb', line 21

def initialize(headers = {})
  @headers = headers
end

Instance Attribute Details

#paramsHash (readonly)

It should store read only parameters

Returns:

  • (Hash)


12
13
14
# File 'lib/alpaca/news/api/rest/news.rb', line 12

def params
  @params
end

Class Method Details

.allAlpaca::News::Api::Rest::Collection Also known as: recent

Calls for the most recent news



109
110
111
# File 'lib/alpaca/news/api/rest/news.rb', line 109

def all
  new.all
end

.query(params = {}) ⇒ Alpaca::News::Api::Rest::Collection Also known as: search, lookup, get, find_all

Query all with parameters



89
90
91
# File 'lib/alpaca/news/api/rest/news.rb', line 89

def query(params = {})
  new.where(params).all
end

.where(params = {}) ⇒ Alpaca::News::Api::Rest::News

Set params



101
102
103
# File 'lib/alpaca/news/api/rest/news.rb', line 101

def where(params = {})
  new.where(params)
end

Instance Method Details

#allAlpaca::News::Api::Rest::Collection

Send GET request to the host



53
54
55
# File 'lib/alpaca/news/api/rest/news.rb', line 53

def all
  Alpaca::News::Api::Rest::Collection.get('/v1beta1/news', @params, @headers, Alpaca::News::Api::Models::News)
end

#find_each {|Alpaca::News::Api::Models::News| ... } ⇒ Object

Iterate for all objects and call for the next page (all pages)

Yields:



62
63
64
65
66
67
68
69
70
# File 'lib/alpaca/news/api/rest/news.rb', line 62

def find_each
  result = all
  loop do
    result.objects.each do |object|
      yield(object)
    end
    break unless result = result.next_page
  end
end

#find_in_batches {|Array<Alpaca::News::Api::Models::News>| ... } ⇒ Object

Iterate for all objects and call for the next page in batches (all pages)

Yields:



76
77
78
79
80
81
82
# File 'lib/alpaca/news/api/rest/news.rb', line 76

def find_in_batches
  result = all
  loop do
    yield(result.objects)
    break unless result result.next_page
  end
end

#recentAlpaca::News::Api::Rest::Collection

Send GET request to the host



56
57
58
# File 'lib/alpaca/news/api/rest/news.rb', line 56

def all
  Alpaca::News::Api::Rest::Collection.get('/v1beta1/news', @params, @headers, Alpaca::News::Api::Models::News)
end

#where(params = {}) ⇒ Object

Set the request params

Parameters:

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

    API request params

Options Hash (params):

  • symbols (String)

    List of symbols to obtain news

  • start (String) — default: Default: 01-01-2015

    Start date to obtain news

  • end (String) — default: Default: now

    End date to obtain news

  • limit (String) — default: Default: 10, Max: 50

    Limit of news items to be returned for given page

  • sort (String) — default: Default: DESC

    Sort articles by updated date. Options: DESC, ASC

  • include_content (Boolean) — default: Default: false

    Boolean whether to include content for news articles

  • exclude_contentless (Boolean) — default: Default: false

    Exclude news articles that do not contain content (just headline and summary)

  • page_token (String)

    Pagination token to continue to next page



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/alpaca/news/api/rest/news.rb', line 37

def where(params = {})
  @params = ActiveSupport::HashWithIndifferentAccess.new(params)
  @params[:symbols] = @params[:symbols].join(',') if @params[:symbols].is_a?(Array)
  if %w[Time DateTime Date String].include?(@params[:start].class.name)
    @params[:start] = DateTime.parse(@params[:start].to_s).to_time.utc.rfc3339
  end
  if %w[Time DateTime Date String].include?(@params[:end].class.name)
    @params[:end] = DateTime.parse(@params[:end].to_s).to_time.utc.rfc3339
  end
  self
end