eodhd.rb 0.13.5 → 0.13.7

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: 72b64180485bc086dd1306c1269feaf9a3bd10fe7ad7a43da2c1731043004908
4
- data.tar.gz: 8fb6a353e72d30bc9dec792a2ce4f356b74a4e62506887f09d56b2d4ba68ec4d
3
+ metadata.gz: afa9158cc3f9bb4c08a8d547cd2011ed7291e04f5e4b2ecba57bec99c0423a20
4
+ data.tar.gz: '0209d6df466503644d89e99bcc8c55a717707cbf466524de79e53dcb834bfacd'
5
5
  SHA512:
6
- metadata.gz: 37ae6f52d8be92689d04a2b695d505c11ddd083b9f7d15d41ba9b367a27b8b1c3f61868055832c38257bef95bb282e051f0a3838f8aab18f3f266927d7c1ab0c
7
- data.tar.gz: 0e9a591954d3a57e89c7e106ff09769e67d6aa5936fc5904b9de96d4eb499cd01d5c70564ef389b1dd504f3bfe0cd53cd088b6c70972703cd044c5f2496d6bb1
6
+ metadata.gz: a4e8a34f9f8fbc6abc74abbbfa2d66efe96a3b18c9a6092ffc775484068ccf36b93168a690d04634f5723f156d3bdf87f5e2943b8ec5e4ce660de1519a50dc83
7
+ data.tar.gz: b87d229a34f838e524ac7d67ec9d4171087f708080700a797572a3913d859f33d8a7bab10aa5b0d9f71d77aad5814500ef66eac2f57e74a1e139f04c4065cd2b
data/eodhd.rb.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'eodhd.rb'
3
3
 
4
- spec.version = '0.13.5'
4
+ spec.version = '0.13.7'
5
5
  spec.date = '2025-03-19'
6
6
 
7
7
  spec.summary = "Access the eodhd.com API with Ruby."
data/lib/Eodhd/Client.rb CHANGED
@@ -9,6 +9,18 @@ require 'json'
9
9
  require 'logger'
10
10
 
11
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
+
12
24
  class Client
13
25
 
14
26
  API_HOST = 'eodhd.com'
@@ -16,6 +28,10 @@ class Eodhd
16
28
  class << self
17
29
  attr_writer :log_file_path
18
30
 
31
+ def path_prefix
32
+ '/api'
33
+ end
34
+
19
35
  def default_log_file_path
20
36
  File.join(%w{~ log eodhd log.txt})
21
37
  end
@@ -36,27 +52,27 @@ class Eodhd
36
52
 
37
53
  # This endpoint always returns json regardless of what fmt is specified.
38
54
  def exchanges_list
39
- path = "/api/exchanges-list"
40
- do_request(request_string: request_string(path))
55
+ response = get(path: '/exchanges-list'))
56
+ handle_response(response)
41
57
  end
42
58
 
43
59
  def exchange_symbol_list(exchange_code:)
44
- path = "/api/exchange-symbol-list/#{exchange_code}"
45
- do_request(request_string: request_string(path))
60
+ response = get(path: "/exchange-symbol-list/#{exchange_code}")
61
+ handle_response(response)
46
62
  end
47
63
 
48
64
  def eod_data(exchange_id:, symbol:, period:, from: nil, to: nil)
49
- path = "/api/eod/#{symbol}.#{exchange_id}"
50
65
  args = {period: period}
51
66
  args.merge!(from: from) if from
52
67
  args.merge!(to: to) if to
53
- do_request(request_string: request_string(path), args: args)
68
+ response = get(path: "/eod/#{symbol}.#{exchange_id}", args: args)
69
+ handle_response(response)
54
70
  end
55
71
 
56
72
  def eod_bulk_last_day(exchange_id:, date:)
57
- path = "/api/eod-bulk-last-day/#{exchange_id}"
58
73
  args = {date: date}
59
- 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)
60
76
  end
61
77
 
62
78
  private
@@ -66,28 +82,53 @@ class Eodhd
66
82
  end
67
83
 
68
84
  def request_string(path)
69
- "https://#{API_HOST}#{path}"
85
+ "https://#{API_HOST}#{self.class.path_prefix}#{path}"
70
86
  end
71
87
 
72
88
  def log_args?(args)
73
89
  !args.values.all?(&:nil?)
74
90
  end
75
91
 
76
- def log(request_string:, args:)
77
- log_string = "GET #{request_string}"
92
+ def log_request(verb:, request_string:, args:)
93
+ log_string = "#{verb} #{request_string}"
78
94
  if log_args?(args)
79
95
  log_string << "?#{args.x_www_form_urlencode}"
80
96
  end
81
97
  self.class.logger.info(log_string)
82
98
  end
83
99
 
84
- def do_request(request_string:, args: {})
85
- log(request_string: request_string, args: args)
100
+ def log_error(code:, message:, body:)
101
+ log_string = "#{code}\n#{message}\n#{body}"
102
+ self.class.logger.error(log_string)
103
+ end
104
+
105
+ def do_request(verb:, path:, args: {})
106
+ log_request(verb: verb, request_string: request_string(path), args: args)
86
107
  api_token = args[:api_token] || @api_token
87
108
  fmt = args[:fmt] || 'json'
88
109
  args.merge!(api_token: api_token, fmt: fmt)
89
- response = HTTP.get(request_string, args)
90
- JSON.parse(response.body)
110
+ HTTP.send(verb.to_s.downcase, request_string(path), args)
111
+ end
112
+
113
+ def get(path:, args: {})
114
+ do_request(verb: 'GET', path: path, args: args)
115
+ end
116
+
117
+ def handle_response(response)
118
+ if response.success?
119
+ JSON.parse(response.body)
120
+ else
121
+ log_error(
122
+ code: response.code,
123
+ message: response.message,
124
+ body: response.body
125
+ )
126
+ raise Eodhd::Error.new(
127
+ code: response.code,
128
+ message: response.message,
129
+ body: response.body
130
+ )
131
+ end
91
132
  end
92
133
  end
93
134
  end
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.7
6
6
 
7
7
  # Changes since 0.12:
8
8
  # -/0: Add logging.
@@ -18,14 +18,31 @@
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().
41
+ # 6/7. Logging for HTTP errors.
42
+ # 26. ~ Eodhd::Client: /log()/log_request()/
43
+ # 27. + Eodhd::Client#log_error
44
+ # 28. ~ Eodhd::Client#do_request: /log/log_request/.
45
+ # 29. ~ Eodhd::Client#handle_response: Use log_error().
29
46
 
30
47
  require_relative 'Eodhd/Client'
31
48
  require_relative 'Eodhd/EodBulkLastDay'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eodhd.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.5
4
+ version: 0.13.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran