algonquin_stocks 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ebfc73b1396d857b582c9e4d60930100280e2747d96b2dd7907e50a98adf8791
4
+ data.tar.gz: f463f4821055e3fd25e406e7c01f49da4221516b2fa3f3c1e64b8951667b003f
5
+ SHA512:
6
+ metadata.gz: 720606af099252a69d7b2bd69fa5a33cabe683fc4caf3893684868b3e0c6e1a2b6fbee17be674cc35064f46d0d794fc25597b2ee612f9b789a6a876e81b9941a
7
+ data.tar.gz: 373348b731c160a6659c6aac39b3600072e669276ed3d0475a844ce23da846bd2fe726c702fc84b24b70eb9c36017f534cdf7fa0ab62ff7f6e359c61e9354964
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # Stocks
2
+
3
+ Stock data provided by [Alpha Vantage](https://www.alphavantage.co/). An [API KEY](https://www.alphavantage.co/support/#api-key) is required.
4
+
5
+ ## Installation
6
+
7
+ To install the 'stocks' ruby gem:
8
+
9
+ `gem install stocks`
10
+
11
+ ## Gem Configuration
12
+
13
+ To use the gem in your Rails Application, include it in your Gemfile:
14
+
15
+ `gem "stocks"`, '~> 1.0.0'
16
+
17
+
18
+ ## Initialization
19
+
20
+ To initialize Stocks with a valid API_KEY:
21
+
22
+ `Stocks::Stock.new(api_key: YOUR_API_KEY)`
23
+
24
+
25
+ ## Usage
26
+
27
+ ### Stocks::StockClient.quote(symbol)
28
+
29
+ Quote is the primary method, returning a Stocks::Stock instance including the following attributes:
30
+
31
+ - symbol
32
+ - open
33
+ - high
34
+ - low
35
+ - price
36
+ - volume
37
+ - date
38
+ - close
39
+ - change
40
+ - percent
41
+
42
+
43
+ ### Stocks::StockClient.data(symbol)
44
+
45
+ Data returns a hash representation of JSON the data.
46
+
47
+
48
+ ### Stocks::Stock.new(json)
49
+
50
+ Stock.new(json) initializes a new Stocks::Stock instance with the given JSON data.
51
+
52
+ ### Stocks::Stock#update(json)
53
+
54
+ Update updates the Stocks::Stock instance with the given JSON data.
55
+
56
+ ### Stocks::Stock#to_s
57
+
58
+ to_s returns a string representation of the Stocks::Stock instance.
@@ -0,0 +1,38 @@
1
+ module Stocks
2
+
3
+ # => Stocks::Stock
4
+ #
5
+ # @param json [Hash] The JSON response from Alpha Vantage.
6
+ # @return [Stocks::Stock] The stock object.
7
+ class Stock
8
+ attr_reader :symbol, :open, :high, :low, :price, :volume, :date, :close, :change, :percent
9
+
10
+ def initialize(json)
11
+ update(json)
12
+ end
13
+
14
+ # => Stocks::Stock#update
15
+ #
16
+ # @param json [Hash] The JSON response from Alpha Vantage.
17
+ # @return [Stocks::Stock] The stock object.
18
+ def update(json)
19
+ @symbol = json['01. symbol']
20
+ @open = json['02. open'].to_f
21
+ @high = json['03. high'].to_f
22
+ @low = json['04. low'].to_f
23
+ @price = json['05. price'].to_f
24
+ @volume = json['06. volume'].to_i
25
+ @date = Date.parse(json['07. latest trading day'])
26
+ @close = json['08. previous close'].to_f
27
+ @change = json['09. change'].to_f
28
+ @percent = json['10. change percent'].to_f
29
+ end
30
+
31
+ # => Stocks::Stock#to_s
32
+ #
33
+ # @return [String] The stock object as a string.
34
+ def to_s
35
+ "#{symbol}: #{price} (#{change}, #{percent}) #{date}"
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,47 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+ require 'date'
4
+
5
+ module Stocks
6
+
7
+ # => Stocks::StockClient
8
+ #
9
+ # @param api_key [String] The API key for Alpha Vantage.
10
+ # @return [Stocks::StockClient] The stock client object.
11
+ class StockClient
12
+ URL_BASE = "https://www.alphavantage.co/query"
13
+ FUNCTION = "GLOBAL_QUOTE"
14
+
15
+ # => Stocks::StockClient#initialize
16
+ #
17
+ # @param api_key [String] The API key for Alpha Vantage.
18
+ # @return [Stocks::StockClient] The stock client object.
19
+ def initialize(api_key)
20
+ @api_key = api_key
21
+ @url = "#{URL_BASE}?apikey=#{@api_key}"
22
+ end
23
+
24
+ # => Stocks::StockClient#data
25
+ #
26
+ # @param symbol [String] The symbol of the stock.
27
+ # @param function [String] The function to use.
28
+ # @return [Hash] The JSON response from Alpha Vantage.
29
+ def data(symbol, function=FUNCTION)
30
+ url = "#{@url}&function=#{function}&symbol=#{symbol}"
31
+ RestClient::Request.execute(url: url, method: :get, verify_ssl: false) do |response|
32
+ raise response.body unless response.code == 200
33
+ JSON.parse(response.body)
34
+ end
35
+ end
36
+
37
+ # => Stocks::StockClient#quote
38
+ #
39
+ # @param symbol [String] The symbol of the stock.
40
+ # @param function [String] The function to use.
41
+ # @return [Stocks::Stock] The stock object.
42
+ def quote(symbol, function=FUNCTION)
43
+ json = data(symbol, function)
44
+ Stock.new(json['Global Quote'])
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,5 @@
1
+ # => Stocks::VERSION
2
+ module Stocks
3
+ VERSION = '1.1.0'
4
+ end
5
+
data/lib/stocks.rb ADDED
@@ -0,0 +1,3 @@
1
+ require_relative "stocks/version"
2
+ require_relative "stocks/stock"
3
+ require_relative "stocks/stock_client"
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: algonquin_stocks
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrei Grobnic
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-04-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A gem for managing stocks
14
+ email:
15
+ - grobnia@algonquincollege.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.md
21
+ - lib/stocks.rb
22
+ - lib/stocks/stock.rb
23
+ - lib/stocks/stock_client.rb
24
+ - lib/stocks/version.rb
25
+ homepage: https://github.com/andreigrob/stocks
26
+ licenses:
27
+ - GPL-2.0
28
+ metadata:
29
+ allowed_push_host: https://rubygems.org
30
+ homepage_uri: https://github.com/andreigrob/stocks
31
+ source_code_uri: https://github.com/andreigrob/stocks
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.1.0
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.4.20
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Stocks
51
+ test_files: []