Class: Aadyandana::LatestStockPrice::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/aadyandana/latest_stock_price.rb

Overview

LatestStockPrice is responsible for fetching the latest stock price information from the API. It provides methods to retrieve stock prices and related data.

Example usage:

client = Aadyandana::LatestStockPrice::Client.new(api_key, params)
stock = client.price
stocks = client.prices
stocks = client.price_all

Attributes:

  • api_key: The API key used for authentication with the stock price API.

  • params: Any parameter(s) related to filter, sorting and pagination

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.



27
28
29
30
31
32
33
34
# File 'lib/aadyandana/latest_stock_price.rb', line 27

def initialize(api_key, params = {})
  @api_key = api_key
  @params = params

  @datetime_attrs = %w[lastUpdateTime]
  @string_meta_attrs = %w[companyName industry isin]
  @string_attrs = %w[identifier symbol] + @string_meta_attrs
end

Instance Method Details

#priceObject

Raises:



36
37
38
39
40
41
42
43
44
# File 'lib/aadyandana/latest_stock_price.rb', line 36

def price
  stocks = get

  stocks = filter(stocks)

  raise Error, "Error fetching data: Bad Request" unless stocks.length == 1

  stocks.first
end

#price_allObject



58
59
60
61
62
63
64
# File 'lib/aadyandana/latest_stock_price.rb', line 58

def price_all
  stocks = get

  stocks = sort(stocks, @params[:sort]) if @params[:sort]

  stocks
end

#pricesObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/aadyandana/latest_stock_price.rb', line 46

def prices
  page = (@params[:page] || 1).to_i
  limit = (@params[:limit] || 10).to_i
  
  stocks = get
  stocks = filter(stocks)
  stocks = sort(stocks, @params[:sort]) if @params[:sort]
  stocks = paginate(stocks, page, limit)

  stocks || []
end