eodhd.rb 0.13.4 → 0.13.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c96a54dc7124036098037626b7356f343bb1f43d647b1c17c16b4e9e11cd6c6
4
- data.tar.gz: 71c645d8e47a50971d44b1742e727ebe1497d312fd0ccf4af504aa796147964a
3
+ metadata.gz: babe78e5a3c016dc6ec370660e5a2dda227f75458fc0519a411784923c00fb13
4
+ data.tar.gz: 7b9221fc1b1035ddf2b20263d99a7af7a21989cc77f664f4475c7942904307c1
5
5
  SHA512:
6
- metadata.gz: b20d4cddf4eee55eb6c99600e54618ab9fb236c2a0b00a593f838eaace88d0d7278994c1ad8019ff7db5b53525aff16b1a9b20c820a5e40ae371e14b2c097ec7
7
- data.tar.gz: fcabde4719ecd553972d3fedaa29652435e0007813c4cb538b0065f199c427e4a4ba7e38774c22c8c9edc1a4245b3816e9daca404ab4f5f1476d1fa227dad7fe
6
+ metadata.gz: 93761d6297db80a6e6a206a30cf3e653634d569ae4a0bf8a90389aaba840994a487ea317ec40683b866ffd4b7bf118009f40b9574c3022b7c4ef2290727b4a56
7
+ data.tar.gz: fe98cf25eea41c380e2365bedb578761b4a95e6044e9e09e1c9007b0e3c921def3081fd06ff48622df2e2db79b296bae991fe7be3d55aa64c66ee2a0e08ef15b
data/eodhd.rb.gemspec CHANGED
@@ -1,8 +1,8 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'eodhd.rb'
3
3
 
4
- spec.version = '0.13.4'
5
- spec.date = '2024-10-09'
4
+ spec.version = '0.13.6'
5
+ spec.date = '2025-03-19'
6
6
 
7
7
  spec.summary = "Access the eodhd.com API with Ruby."
8
8
  spec.description = "Access the eodhd.com API with Ruby."
data/lib/Eodhd/Client.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # Eodhd/Client.rb
2
2
  # Eodhd::Client
3
3
 
4
+ require 'fileutils'
4
5
  require 'Hash/x_www_form_urlencode'
5
6
  gem 'http.rb'
6
7
  require 'http.rb'
@@ -8,17 +9,40 @@ require 'json'
8
9
  require 'logger'
9
10
 
10
11
  class Eodhd
12
+ class Error < RuntimeError
13
+ attr_reader :code, :message, :body
14
+
15
+ private
16
+
17
+ def initialize(code:, message:, body:)
18
+ @code = code
19
+ @message = message
20
+ @body = body
21
+ end
22
+ end
23
+
11
24
  class Client
12
25
 
13
26
  API_HOST = 'eodhd.com'
14
27
 
15
28
  class << self
16
- def log_filename
17
- File.expand_path('~/log/eodhd/log.txt')
29
+ attr_writer :log_file_path
30
+
31
+ def path_prefix
32
+ '/api'
33
+ end
34
+
35
+ def default_log_file_path
36
+ File.join(%w{~ log eodhd log.txt})
37
+ end
38
+
39
+ def log_file_path
40
+ File.expand_path(@log_file_path || default_log_file_path)
18
41
  end
19
42
 
20
43
  def log_file
21
- File.open(log_filename, File::WRONLY | File::APPEND | File::CREAT)
44
+ FileUtils.mkdir_p(File.dirname(log_file_path))
45
+ File.open(log_file_path, File::WRONLY | File::APPEND | File::CREAT)
22
46
  end
23
47
 
24
48
  def logger
@@ -28,27 +52,27 @@ class Eodhd
28
52
 
29
53
  # This endpoint always returns json regardless of what fmt is specified.
30
54
  def exchanges_list
31
- path = "/api/exchanges-list"
32
- do_request(request_string: request_string(path))
55
+ response = get(path: '/exchanges-list'))
56
+ handle_response(response)
33
57
  end
34
58
 
35
59
  def exchange_symbol_list(exchange_code:)
36
- path = "/api/exchange-symbol-list/#{exchange_code}"
37
- do_request(request_string: request_string(path))
60
+ response = get(path: "/exchange-symbol-list/#{exchange_code}")
61
+ handle_response(response)
38
62
  end
39
63
 
40
64
  def eod_data(exchange_id:, symbol:, period:, from: nil, to: nil)
41
- path = "/api/eod/#{symbol}.#{exchange_id}"
42
65
  args = {period: period}
43
66
  args.merge!(from: from) if from
44
67
  args.merge!(to: to) if to
45
- do_request(request_string: request_string(path), args: args)
68
+ response = get(path: "/eod/#{symbol}.#{exchange_id}", args: args)
69
+ handle_response(response)
46
70
  end
47
71
 
48
72
  def eod_bulk_last_day(exchange_id:, date:)
49
- path = "/api/eod-bulk-last-day/#{exchange_id}"
50
73
  args = {date: date}
51
- do_request(request_string: request_string(path), args: args)
74
+ response = get(path: "/eod-bulk-last-day/#{exchange_id}", args: args)
75
+ handle_response(response)
52
76
  end
53
77
 
54
78
  private
@@ -58,28 +82,43 @@ class Eodhd
58
82
  end
59
83
 
60
84
  def request_string(path)
61
- "https://#{API_HOST}#{path}"
85
+ "https://#{API_HOST}#{self.class.path_prefix}#{path}"
62
86
  end
63
87
 
64
88
  def log_args?(args)
65
89
  !args.values.all?(&:nil?)
66
90
  end
67
91
 
68
- def log(request_string:, args:)
69
- log_string = "GET #{request_string}"
92
+ def log(verb:, request_string:, args:)
93
+ log_string = "#{verb} #{request_string}"
70
94
  if log_args?(args)
71
95
  log_string << "?#{args.x_www_form_urlencode}"
72
96
  end
73
97
  self.class.logger.info(log_string)
74
98
  end
75
99
 
76
- def do_request(request_string:, args: {})
77
- log(request_string: request_string, args: args)
100
+ def do_request(verb:, path:, args: {})
101
+ log(verb: verb, request_string: request_string(path), args: args)
78
102
  api_token = args[:api_token] || @api_token
79
103
  fmt = args[:fmt] || 'json'
80
104
  args.merge!(api_token: api_token, fmt: fmt)
81
- response = HTTP.get(request_string, args)
82
- JSON.parse(response.body)
105
+ HTTP.send(verb.to_s.downcase, request_string(path), args)
106
+ end
107
+
108
+ def get(path:, args: {})
109
+ do_request(verb: 'GET', path: path, args: args)
110
+ end
111
+
112
+ def handle_response(response)
113
+ if response.success?
114
+ JSON.parse(response.body)
115
+ else
116
+ raise Eodhd::Error.new(
117
+ code: response.code,
118
+ message: response.message,
119
+ body: response.body,
120
+ )
121
+ end
83
122
  end
84
123
  end
85
124
  end
File without changes
data/lib/Eodhd/EodData.rb CHANGED
File without changes
File without changes
File without changes
File without changes
File without changes
data/lib/eodhd.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # Eodhd.rb
2
2
  # Eodhd
3
3
 
4
- # 20241009
5
- # 0.13.4
4
+ # 20250319
5
+ # 0.13.6
6
6
 
7
7
  # Changes since 0.12:
8
8
  # -/0: Add logging.
@@ -18,14 +18,26 @@
18
18
  # 8. ~ Eodhd::Client#log: Construct the log string in the method, constructing a log string with arguments if present.
19
19
  # 9. ~ Eodhd::Client#do_request: Call the new interface for log().
20
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
21
+ # 10. + Eodhd::Client#log_args? Only need to check for whether the values are nil, since this will still work for empty args also.
22
+ # 11. ~ Eodhd::Client#log: Use log_args?
23
+ # 3/4: + eodhd.rb.gemspec, + additional necessary library files
24
+ # 12. + eodhd.rb.gemspec
25
25
  # 13. ~ Gemfile: Use gemspec.
26
26
  # 14. + lib/Hash/x_www_form_urlencode.rb
27
27
  # 15. + lib/Thoran/Hash/XWwwFormUrlEncode/x_www_form_url_encode.rb
28
28
  # 16. + lib/Thoran/String/UrlEncode/url_encode.rb
29
+ # 4/5: Create a directory for the log file when running for the first time and also allow specification of the log file path.
30
+ # 17. ~ Eodhd::Client#log_file: Create the log file directory if necessary.
31
+ # 18. ~ Eodhd::Client: + attr_writer :log_file_path
32
+ # 19. + Eodhd::Client#default_log_file_path
33
+ # 20. ~ Eodhd::Client: /log_filename/log_file_path/
34
+ # 5/6: Error handling for HTTP responses.
35
+ # 21. + Eodhd::Client#handle_response
36
+ # 22. + Eodhd::Client#get
37
+ # 23. ~ Eodhd::Client#do_request: Accepts a verb and a path now as well as arguments, instead of a request string and arguments.
38
+ # 23. + Eodhd::Error
39
+ # 24. ~ Eodhd::Client#log: + verb argument
40
+ # 25. ~ Eodhd::Client public methods: Use get() and handle_response().
29
41
 
30
42
  require_relative 'Eodhd/Client'
31
43
  require_relative 'Eodhd/EodBulkLastDay'
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eodhd.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.4
4
+ version: 0.13.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-10-09 00:00:00.000000000 Z
10
+ date: 2025-03-19 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: http.rb
@@ -46,7 +45,6 @@ homepage: http://github.com/thoran/eodhd.rb
46
45
  licenses:
47
46
  - Ruby
48
47
  metadata: {}
49
- post_install_message:
50
48
  rdoc_options: []
51
49
  require_paths:
52
50
  - lib
@@ -61,8 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
59
  - !ruby/object:Gem::Version
62
60
  version: '0'
63
61
  requirements: []
64
- rubygems_version: 3.5.21
65
- signing_key:
62
+ rubygems_version: 3.6.5
66
63
  specification_version: 4
67
64
  summary: Access the eodhd.com API with Ruby.
68
65
  test_files: []