eodhd.rb 0.13.4

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: 7c96a54dc7124036098037626b7356f343bb1f43d647b1c17c16b4e9e11cd6c6
4
+ data.tar.gz: 71c645d8e47a50971d44b1742e727ebe1497d312fd0ccf4af504aa796147964a
5
+ SHA512:
6
+ metadata.gz: b20d4cddf4eee55eb6c99600e54618ab9fb236c2a0b00a593f838eaace88d0d7278994c1ad8019ff7db5b53525aff16b1a9b20c820a5e40ae371e14b2c097ec7
7
+ data.tar.gz: fcabde4719ecd553972d3fedaa29652435e0007813c4cb538b0065f199c427e4a4ba7e38774c22c8c9edc1a4245b3816e9daca404ab4f5f1476d1fa227dad7fe
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # eodhd.rb
2
+
3
+ ## Description
4
+
5
+ Access the eodhd.com API with Ruby.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+ ```ruby
11
+ gem 'eodhd.rb'
12
+ ```
13
+ And then execute:
14
+ ```bash
15
+ $ bundle
16
+ ```
17
+ Or install it yourself as:
18
+ ```bash
19
+ $ gem install eodhd.rb
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ### Setup
25
+ ```ruby
26
+ api_token = 'api_token'
27
+ eodhd_api ||= Eodhd.new(api_token: api_token)
28
+ ```
29
+
30
+ ### List of Exchanges
31
+ ```ruby
32
+ eodhd_api.exchanges
33
+ ```
34
+
35
+ ### List of Exchange Symbols
36
+ ```ruby
37
+ exchange = eodhd_api.exchanges.first
38
+
39
+ eod_api.exchange_symbols(exchange: exchange)
40
+ # OR
41
+ eod_api.exchange_symbols(exchange_code: exchange.code)
42
+ ```
43
+
44
+ ### Retrieve EOD Data For One Symbol
45
+ ```ruby
46
+ exchange = eodhd_api.exchanges.first
47
+ exchange_symbol = eod_api.exchange_symbols(exchange: exchange).first
48
+
49
+ eod_api.eod_data(exchange: exchange, exchange_symbol: exchange_symbol)
50
+ # OR
51
+ eod_api.eod_data(exchange_code: exchange.code, exchange_symbol: exchange_symbol)
52
+ # OR
53
+ eod_api.eod_data(exchange: exchange, symbol: exchange_symbol.code)
54
+ # OR
55
+ eod_api.eod_data(exchange_code: exchange.code, symbol: exchange_symbol.code)
56
+ ```
57
+
58
+ ### Retrieve EOD Data For Multiple Symbols For One Date
59
+ ```ruby
60
+ exchange = eodhd_api.exchanges.first
61
+
62
+ eod_api.eod_bulk_last_day(exchange: exchange, date: Date.today)
63
+ # OR
64
+ eod_api.eod_bulk_last_day(exchange_code: exchange.code, date: Date.today)
65
+ ```
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it ( https://github.com/thoran/eodhd.rb/fork )
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create a new pull request
data/eodhd.rb.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = 'eodhd.rb'
3
+
4
+ spec.version = '0.13.4'
5
+ spec.date = '2024-10-09'
6
+
7
+ spec.summary = "Access the eodhd.com API with Ruby."
8
+ spec.description = "Access the eodhd.com API with Ruby."
9
+
10
+ spec.author = 'thoran'
11
+ spec.email = 'code@thoran.com'
12
+ spec.homepage = 'http://github.com/thoran/eodhd.rb'
13
+ spec.license = 'Ruby'
14
+
15
+ spec.required_ruby_version = '>= 2.5'
16
+
17
+ spec.add_dependency('http.rb')
18
+ spec.files = [
19
+ 'eodhd.rb.gemspec',
20
+ 'Gemfile',
21
+ Dir['lib/**/*.rb'],
22
+ 'README.md',
23
+ Dir['test/**/*.rb']
24
+ ].flatten
25
+ spec.require_paths = ['lib']
26
+ end
@@ -0,0 +1,85 @@
1
+ # Eodhd/Client.rb
2
+ # Eodhd::Client
3
+
4
+ require 'Hash/x_www_form_urlencode'
5
+ gem 'http.rb'
6
+ require 'http.rb'
7
+ require 'json'
8
+ require 'logger'
9
+
10
+ class Eodhd
11
+ class Client
12
+
13
+ API_HOST = 'eodhd.com'
14
+
15
+ class << self
16
+ def log_filename
17
+ File.expand_path('~/log/eodhd/log.txt')
18
+ end
19
+
20
+ def log_file
21
+ File.open(log_filename, File::WRONLY | File::APPEND | File::CREAT)
22
+ end
23
+
24
+ def logger
25
+ @logger ||= Logger.new(log_file, 'daily')
26
+ end
27
+ end # class << self
28
+
29
+ # This endpoint always returns json regardless of what fmt is specified.
30
+ def exchanges_list
31
+ path = "/api/exchanges-list"
32
+ do_request(request_string: request_string(path))
33
+ end
34
+
35
+ def exchange_symbol_list(exchange_code:)
36
+ path = "/api/exchange-symbol-list/#{exchange_code}"
37
+ do_request(request_string: request_string(path))
38
+ end
39
+
40
+ def eod_data(exchange_id:, symbol:, period:, from: nil, to: nil)
41
+ path = "/api/eod/#{symbol}.#{exchange_id}"
42
+ args = {period: period}
43
+ args.merge!(from: from) if from
44
+ args.merge!(to: to) if to
45
+ do_request(request_string: request_string(path), args: args)
46
+ end
47
+
48
+ def eod_bulk_last_day(exchange_id:, date:)
49
+ path = "/api/eod-bulk-last-day/#{exchange_id}"
50
+ args = {date: date}
51
+ do_request(request_string: request_string(path), args: args)
52
+ end
53
+
54
+ private
55
+
56
+ def initialize(api_token:)
57
+ @api_token = api_token
58
+ end
59
+
60
+ def request_string(path)
61
+ "https://#{API_HOST}#{path}"
62
+ end
63
+
64
+ def log_args?(args)
65
+ !args.values.all?(&:nil?)
66
+ end
67
+
68
+ def log(request_string:, args:)
69
+ log_string = "GET #{request_string}"
70
+ if log_args?(args)
71
+ log_string << "?#{args.x_www_form_urlencode}"
72
+ end
73
+ self.class.logger.info(log_string)
74
+ end
75
+
76
+ def do_request(request_string:, args: {})
77
+ log(request_string: request_string, args: args)
78
+ api_token = args[:api_token] || @api_token
79
+ fmt = args[:fmt] || 'json'
80
+ args.merge!(api_token: api_token, fmt: fmt)
81
+ response = HTTP.get(request_string, args)
82
+ JSON.parse(response.body)
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,54 @@
1
+ # Eodhd/EodBulkLastDay.rb
2
+ # Eodhd::EodBulkLastDay
3
+
4
+ class Eodhd
5
+ class EodBulkLastDay
6
+ class << self
7
+ def all(client: nil, api_token: nil, exchange_code:, date:)
8
+ load(client: client, api_token: api_token, exchange_code: exchange_code, date:)
9
+ end
10
+
11
+ private
12
+
13
+ def load(client: nil, api_token: nil, exchange_code:, date:)
14
+ client ||= Client.new(api_token: api_token)
15
+ client.eod_bulk_last_day(exchange_id: exchange_code, date: date).collect do |eod_bulk_last_day|
16
+ self.new(
17
+ code: eod_bulk_last_day['code'],
18
+ exchange_short_name: eod_bulk_last_day['exchange_short_name'],
19
+ date: eod_bulk_last_day['date'],
20
+ open: eod_bulk_last_day['open'],
21
+ high: eod_bulk_last_day['high'],
22
+ low: eod_bulk_last_day['low'],
23
+ close: eod_bulk_last_day['close'],
24
+ adjusted_close: eod_bulk_last_day['adjusted_close'],
25
+ volume: eod_bulk_last_day['volume']
26
+ )
27
+ end
28
+ end
29
+ end # class << self
30
+
31
+ attr_reader\
32
+ :code,
33
+ :exchange_short_name,
34
+ :date,
35
+ :open,
36
+ :high,
37
+ :low,
38
+ :close,
39
+ :adjusted_close,
40
+ :volume
41
+
42
+ def initialize(code:, exchange_short_name:, date:, open:, high:, low:, close:, adjusted_close:, volume:)
43
+ @code = code
44
+ @exchange_short_name = exchange_short_name
45
+ @date = date
46
+ @open = open
47
+ @high = high
48
+ @low = low
49
+ @close = close
50
+ @adjusted_close = adjusted_close
51
+ @volume = volume
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,54 @@
1
+ # Eodhd/EodData.rb
2
+ # Eodhd::EodData
3
+
4
+ class Eodhd
5
+ class EodData
6
+ class << self
7
+ def all(client: nil, api_token: nil, exchange_code:, symbol:, period: 'd', from: nil, to: nil)
8
+ load(client: client, api_token: api_token, exchange_code: exchange_code, symbol: symbol, period: period, from: from, to: to)
9
+ end
10
+
11
+ private
12
+
13
+ def load(client: nil, api_token: nil, exchange_code:, symbol:, period:, from:, to:)
14
+ client ||= Client.new(api_token: api_token)
15
+ client.eod_data(exchange_id: exchange_code, symbol: symbol, period: period, from: from, to: to).collect do |eod_data|
16
+ self.new(
17
+ exchange_code: eod_data['exchange_code'],
18
+ symbol: eod_data['symbol'],
19
+ date: eod_data['date'],
20
+ open: eod_data['open'],
21
+ high: eod_data['high'],
22
+ low: eod_data['low'],
23
+ close: eod_data['close'],
24
+ adjusted_close: eod_data['adjusted_close'],
25
+ volume: eod_data['volume']
26
+ )
27
+ end
28
+ end
29
+ end # class << self
30
+
31
+ attr_reader\
32
+ :exchange_code,
33
+ :symbol,
34
+ :date,
35
+ :open,
36
+ :high,
37
+ :low,
38
+ :close,
39
+ :adjusted_close,
40
+ :volume
41
+
42
+ def initialize(exchange_code:, symbol:, date:, open:, high:, low:, close:, adjusted_close:, volume:)
43
+ @exchange_code = exchange_code
44
+ @symbol = symbol
45
+ @date = date
46
+ @open = open
47
+ @high = high
48
+ @low = low
49
+ @close = close
50
+ @adjusted_close = adjusted_close
51
+ @volume = volume
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,48 @@
1
+ # Eodhd/Exchange.rb
2
+ # Eodhd::Exchange
3
+
4
+ class Eodhd
5
+ class Exchange
6
+ class << self
7
+ def all(client: nil, api_token: nil)
8
+ load(client: client, api_token: api_token)
9
+ end
10
+
11
+ private
12
+
13
+ def load(client: nil, api_token: nil)
14
+ client ||= Client.new(api_token: api_token)
15
+ client.exchanges_list.collect do |exchange|
16
+ self.new(
17
+ name: exchange['Name'],
18
+ code: exchange['Code'],
19
+ operating_mic: exchange['OperatingMIC'],
20
+ country: exchange['Country'],
21
+ currency: exchange['Currency'],
22
+ country_iso2: exchange['CountryISO2'],
23
+ country_iso3: exchange['CountryISO3']
24
+ )
25
+ end
26
+ end
27
+ end # class << self
28
+
29
+ attr_reader\
30
+ :name,
31
+ :code,
32
+ :operating_mic,
33
+ :country,
34
+ :currency,
35
+ :country_iso2,
36
+ :country_iso3
37
+
38
+ def initialize(name:, code:, operating_mic:, country:, currency:, country_iso2:, country_iso3:)
39
+ @name = name
40
+ @code = code
41
+ @operating_mic = operating_mic
42
+ @country = country
43
+ @currency = currency
44
+ @country_iso2 = country_iso2
45
+ @country_iso3 = country_iso3
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,48 @@
1
+ # Eodhd/ExchangeSymbol.rb
2
+ # Eodhd::ExchangeSymbol
3
+
4
+ class Eodhd
5
+ class ExchangeSymbol
6
+ class << self
7
+ def all(client: nil, api_token: nil, exchange_code: nil)
8
+ load(client: client, api_token: api_token, exchange_code: exchange_code)
9
+ end
10
+
11
+ private
12
+
13
+ def load(client: nil, api_token: nil, exchange_code: nil)
14
+ client ||= Client.new(api_token: api_token)
15
+ client.exchange_symbol_list(exchange_code: exchange_code).collect do |symbol|
16
+ self.new(
17
+ code: symbol['Code'],
18
+ name: symbol['Name'],
19
+ country: symbol['Country'],
20
+ exchange: symbol['Exchange'],
21
+ currency: symbol['Currency'],
22
+ type: symbol['Type'],
23
+ isin: symbol['Isin']
24
+ )
25
+ end
26
+ end
27
+ end # class << self
28
+
29
+ attr_reader\
30
+ :code,
31
+ :name,
32
+ :country,
33
+ :exchange,
34
+ :currency,
35
+ :type,
36
+ :isin
37
+
38
+ def initialize(code:, name:, country:, exchange:, currency:, type:, isin:)
39
+ @code = code
40
+ @name = name
41
+ @country = country
42
+ @exchange = exchange
43
+ @currency = currency
44
+ @type = type
45
+ @isin = isin
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,7 @@
1
+ # Hash/x_www_form_urlencode.rb
2
+ # Hash#x_www_form_urlencode
3
+
4
+ # 20241009
5
+ # 0.1.0
6
+
7
+ require 'Thoran/Hash/XWwwFormUrlencode/x_www_form_urlencode'
@@ -0,0 +1,26 @@
1
+ # Thoran/Hash/XWwwFormUrlEncode/x_www_form_urlencode.rb
2
+ # Thoran::Hash::XWwwFormUrlEncode#x_www_form_urlencode
3
+
4
+ # 20241009
5
+ # 0.2.0
6
+
7
+ # Changes since 0.1:
8
+ # -/0: (The class name and the snake case name are consistent now.)
9
+ # 1. /XWWWFormUrlEncode/XWwwFormUrlEncode/
10
+
11
+ require 'Thoran/String/UrlEncode/url_encode'
12
+
13
+ module Thoran
14
+ module Hash
15
+ module XWwwFormUrlEncode
16
+
17
+ def x_www_form_url_encode(joiner = '&')
18
+ inject([]){|a,e| a << "#{e.first.to_s.url_encode}=#{e.last.to_s.url_encode}" unless e.last.nil?; a}.join(joiner)
19
+ end
20
+ alias_method :x_www_form_urlencode, :x_www_form_url_encode
21
+
22
+ end
23
+ end
24
+ end
25
+
26
+ Hash.send(:include, Thoran::Hash::XWwwFormUrlEncode)
@@ -0,0 +1,26 @@
1
+ # Thoran/String/UrlEncode/url_encode.rb
2
+ # Thoran::String::UrlEncode#url_encode
3
+
4
+ # 20160505
5
+ # 0.3.0
6
+
7
+ # Acknowledgements: I've simply ripped off and refashioned the code from the CGI module!...
8
+
9
+ # Changes since 0.2:
10
+ # 1. + Thoran namespace.
11
+
12
+ module Thoran
13
+ module String
14
+ module UrlEncode
15
+
16
+ def url_encode
17
+ self.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
18
+ '%' + $1.unpack('H2' * $1.size).join('%').upcase
19
+ end.tr(' ', '+')
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+
26
+ String.send(:include, Thoran::String::UrlEncode)
data/lib/eodhd.rb ADDED
@@ -0,0 +1,60 @@
1
+ # Eodhd.rb
2
+ # Eodhd
3
+
4
+ # 20241009
5
+ # 0.13.4
6
+
7
+ # Changes since 0.12:
8
+ # -/0: Add logging.
9
+ # 1. + require 'logger'
10
+ # 2. + Eodhd::Client.log_filename
11
+ # 3. + Eodhd::Client.log_file
12
+ # 4. + Eodhd::Client.logger
13
+ # 5. + Eodhd::Client#log
14
+ # 6. ~ Eodhd::Client#do_request: Call log().
15
+ # 0/1: Add args to the log string.
16
+ # 7. ~ Eodhd::Client#do_request: Add the args to the log string.
17
+ # 1/2: Log '?' and args only if args are present.
18
+ # 8. ~ Eodhd::Client#log: Construct the log string in the method, constructing a log string with arguments if present.
19
+ # 9. ~ Eodhd::Client#do_request: Call the new interface for log().
20
+ # 2/3: Fix logging '?' for when args values are nil.
21
+ # 10. + Eod::Client#log_args? Only need to check for whether the values are nil, since this will still work for empty args also.
22
+ # 11. ~ Eod::Client#log: Use log_args?
23
+ # 3/4: + eod.rb.gemspec, + additional necessary library files
24
+ # 12. + eod.rb.gemspec
25
+ # 13. ~ Gemfile: Use gemspec.
26
+ # 14. + lib/Hash/x_www_form_urlencode.rb
27
+ # 15. + lib/Thoran/Hash/XWwwFormUrlEncode/x_www_form_url_encode.rb
28
+ # 16. + lib/Thoran/String/UrlEncode/url_encode.rb
29
+
30
+ require_relative 'Eodhd/Client'
31
+ require_relative 'Eodhd/EodBulkLastDay'
32
+ require_relative 'Eodhd/EodData'
33
+ require_relative 'Eodhd/Exchange'
34
+ require_relative 'Eodhd/ExchangeSymbol'
35
+
36
+ class Eodhd
37
+ def initialize(api_token:)
38
+ @api_token = api_token
39
+ end
40
+
41
+ def exchanges
42
+ Eodhd::Exchange.all(api_token: @api_token)
43
+ end
44
+
45
+ def exchange_symbols(exchange: nil, exchange_code: nil)
46
+ exchange_code ||= exchange.code
47
+ Eodhd::ExchangeSymbol.all(api_token: @api_token, exchange_code: exchange_code)
48
+ end
49
+
50
+ def eod_data(exchange: nil, exchange_code: nil, exchange_symbol: nil, symbol: nil, period: nil, from: nil, to: nil)
51
+ exchange_code ||= exchange.code
52
+ symbol ||= exchange_symbol.code
53
+ Eodhd::EodData.all(api_token: @api_token, exchange_code: exchange_code, symbol: symbol, period: period, from: from, to: to)
54
+ end
55
+
56
+ def eod_bulk_last_day(exchange: nil, exchange_code: nil, date:)
57
+ exchange_code ||= exchange.code
58
+ Eodhd::EodBulkLastDay.all(api_token: @api_token, exchange_code: exchange_code, date: date)
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eodhd.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.13.4
5
+ platform: ruby
6
+ authors:
7
+ - thoran
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-10-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http.rb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Access the eodhd.com API with Ruby.
28
+ email: code@thoran.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - Gemfile
34
+ - README.md
35
+ - eodhd.rb.gemspec
36
+ - lib/Eodhd/Client.rb
37
+ - lib/Eodhd/EodBulkLastDay.rb
38
+ - lib/Eodhd/EodData.rb
39
+ - lib/Eodhd/Exchange.rb
40
+ - lib/Eodhd/ExchangeSymbol.rb
41
+ - lib/Hash/x_www_form_urlencode.rb
42
+ - lib/Thoran/Hash/XWwwFormUrlencode/x_www_form_urlencode.rb
43
+ - lib/Thoran/String/UrlEncode/url_encode.rb
44
+ - lib/eodhd.rb
45
+ homepage: http://github.com/thoran/eodhd.rb
46
+ licenses:
47
+ - Ruby
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '2.5'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubygems_version: 3.5.21
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Access the eodhd.com API with Ruby.
68
+ test_files: []