|
3 | 3 | require "httpx" |
4 | 4 | require "singleton" |
5 | 5 |
|
6 | | -module TwelvedataRuby |
7 | | - # HTTP client for making requests to the Twelve Data API |
8 | | - class Client |
9 | | - include Singleton |
10 | | - |
11 | | - # Default environment variable name for API key |
12 | | - APIKEY_ENV_NAME = "TWELVEDATA_API_KEY" |
13 | | - |
14 | | - # Default connection timeout in milliseconds |
15 | | - DEFAULT_CONNECT_TIMEOUT = 120 |
16 | | - |
17 | | - # Base URL for the Twelve Data API |
18 | | - BASE_URL = "https://api.twelvedata.com" |
19 | | - |
20 | | - class << self |
21 | | - # Make HTTP requests using HTTPX |
22 | | - # |
23 | | - # @param request_objects [Request, Array<Request>] Request object(s) to send |
24 | | - # @param options [Hash] Additional HTTPX options |
25 | | - # @return [HTTPX::Response, Array<HTTPX::Response>] HTTP response(s) |
26 | | - def request(request_objects, **options) |
27 | | - requests = build_requests(request_objects) |
28 | | - http_client = HTTPX.with(http_options.merge(options)) |
29 | | - |
30 | | - http_client.request(requests) |
31 | | - end |
32 | | - |
33 | | - # Build HTTP requests from Request objects |
34 | | - # |
35 | | - # @param requests [Request, Array<Request>] Request object(s) |
36 | | - # @return [Array] Array of HTTP request specs |
37 | | - def build_requests(requests) |
38 | | - Utils.to_array(requests).map(&:build) |
39 | | - end |
40 | | - |
41 | | - # Get HTTP client options |
42 | | - # |
43 | | - # @return [Hash] HTTPX options |
44 | | - def http_options |
45 | | - { |
46 | | - origin: BASE_URL, |
47 | | - timeout: { connect_timeout: instance.connect_timeout } |
48 | | - } |
49 | | - end |
50 | | - end |
| 6 | +# HTTP client for making requests to the Twelve Data API |
| 7 | +class TwelvedataRuby::Client |
| 8 | + include Singleton |
51 | 9 |
|
52 | | - attr_reader :configuration |
| 10 | + # Default environment variable name for API key |
| 11 | + APIKEY_ENV_NAME = "TWELVEDATA_API_KEY" |
53 | 12 |
|
54 | | - def initialize |
55 | | - @configuration = {} |
56 | | - @endpoint_methods_defined = Set.new |
57 | | - reset_configuration |
58 | | - end |
| 13 | + # Default connection timeout in milliseconds |
| 14 | + DEFAULT_CONNECT_TIMEOUT = 120 |
59 | 15 |
|
60 | | - # Configure the client with new options |
61 | | - # |
62 | | - # @param options [Hash] Configuration options |
63 | | - # @option options [String] :apikey API key for authentication |
64 | | - # @option options [Integer] :connect_timeout Connection timeout in milliseconds |
65 | | - # @option options [String] :apikey_env_var_name Environment variable name for API key |
66 | | - # @return [self] Returns self for method chaining |
67 | | - def configure(**options) |
68 | | - @configuration.merge!(options) |
69 | | - self |
70 | | - end |
| 16 | + # Base URL for the Twelve Data API |
| 17 | + BASE_URL = "https://api.twelvedata.com" |
71 | 18 |
|
72 | | - # Get the current API key |
| 19 | + class << self |
| 20 | + # Make HTTP requests using HTTPX |
73 | 21 | # |
74 | | - # @return [String, nil] Current API key |
75 | | - def apikey |
76 | | - Utils.empty_to_nil(@configuration[:apikey]) || ENV[apikey_env_var_name] |
77 | | - end |
| 22 | + # @param request_objects [Request, Array<Request>] Request object(s) to send |
| 23 | + # @param options [Hash] Additional HTTPX options |
| 24 | + # @return [HTTPX::Response, Array<HTTPX::Response>] HTTP response(s) |
| 25 | + def request(request_objects, **options) |
| 26 | + requests = build_requests(request_objects) |
| 27 | + http_client = HTTPX.with(http_options.merge(options)) |
78 | 28 |
|
79 | | - # Set the API key |
80 | | - # |
81 | | - # @param apikey [String] New API key |
82 | | - # @return [String] The API key that was set |
83 | | - def apikey=(apikey) |
84 | | - @configuration[:apikey] = apikey |
| 29 | + http_client.request(requests) |
85 | 30 | end |
86 | 31 |
|
87 | | - # Get the connection timeout |
| 32 | + # Build HTTP requests from Request objects |
88 | 33 | # |
89 | | - # @return [Integer] Connection timeout in milliseconds |
90 | | - def connect_timeout |
91 | | - parse_timeout(@configuration[:connect_timeout]) |
| 34 | + # @param requests [Request, Array<Request>] Request object(s) |
| 35 | + # @return [Array] Array of HTTP request specs |
| 36 | + def build_requests(requests) |
| 37 | + Utils.to_array(requests).map(&:build) |
92 | 38 | end |
93 | 39 |
|
94 | | - # Set the connection timeout |
| 40 | + # Get HTTP client options |
95 | 41 | # |
96 | | - # @param timeout [Integer, String] New timeout value |
97 | | - # @return [Integer] The timeout that was set |
98 | | - def connect_timeout=(timeout) |
99 | | - @configuration[:connect_timeout] = parse_timeout(timeout) |
| 42 | + # @return [Hash] HTTPX options |
| 43 | + def http_options |
| 44 | + { |
| 45 | + origin: BASE_URL, |
| 46 | + timeout: { connect_timeout: instance.connect_timeout }, |
| 47 | + } |
100 | 48 | end |
| 49 | + end |
101 | 50 |
|
102 | | - # Get the environment variable name for the API key |
103 | | - # |
104 | | - # @return [String] Environment variable name |
105 | | - def apikey_env_var_name |
106 | | - (@configuration[:apikey_env_var_name] || APIKEY_ENV_NAME).upcase |
107 | | - end |
| 51 | + attr_reader :configuration |
108 | 52 |
|
109 | | - # Set the environment variable name for the API key |
110 | | - # |
111 | | - # @param var_name [String] New environment variable name |
112 | | - # @return [String] The variable name that was set (uppercased) |
113 | | - def apikey_env_var_name=(var_name) |
114 | | - @configuration[:apikey_env_var_name] = var_name.upcase |
115 | | - end |
| 53 | + def initialize |
| 54 | + @configuration = {} |
| 55 | + @endpoint_methods_defined = Set.new |
| 56 | + reset_configuration |
| 57 | + end |
116 | 58 |
|
117 | | - # Fetch data from an API endpoint |
118 | | - # |
119 | | - # @param request [Request] Request object to send |
120 | | - # @return [Response, Hash, ResponseError] Response or error information |
121 | | - def fetch(request) |
122 | | - return nil unless request |
123 | | - |
124 | | - if request.valid? |
125 | | - http_response = self.class.request(request) |
126 | | - raise HTTPX::Error, "HTTP request failed" if http_response.error && http_response.response.nil? |
127 | | - |
128 | | - Response.resolve(http_response, request) |
129 | | - else |
130 | | - { errors: request.errors } |
131 | | - end |
132 | | - rescue StandardError => e |
133 | | - handle_fetch_error(e, request) |
134 | | - end |
| 59 | + # Configure the client with new options |
| 60 | + # |
| 61 | + # @param options [Hash] Configuration options |
| 62 | + # @option options [String] :apikey API key for authentication |
| 63 | + # @option options [Integer] :connect_timeout Connection timeout in milliseconds |
| 64 | + # @option options [String] :apikey_env_var_name Environment variable name for API key |
| 65 | + # @return [self] Returns self for method chaining |
| 66 | + def configure(**options) |
| 67 | + @configuration.merge!(options) |
| 68 | + self |
| 69 | + end |
135 | 70 |
|
136 | | - # Handle method calls for API endpoints |
137 | | - # |
138 | | - # @param endpoint_name [String, Symbol] API endpoint name |
139 | | - # @param endpoint_params [Hash] Parameters for the endpoint |
140 | | - # @return [Response, Hash, ResponseError] API response or error |
141 | | - def method_missing(endpoint_name, **endpoint_params, &block) |
142 | | - if Endpoint.valid_name?(endpoint_name) |
143 | | - define_endpoint_method(endpoint_name) |
144 | | - send(endpoint_name, **endpoint_params) |
145 | | - else |
146 | | - super |
147 | | - end |
148 | | - end |
| 71 | + # Get the current API key |
| 72 | + # |
| 73 | + # @return [String, nil] Current API key |
| 74 | + def apikey |
| 75 | + Utils.empty_to_nil(@configuration[:apikey]) || ENV[apikey_env_var_name] |
| 76 | + end |
149 | 77 |
|
150 | | - # Check if client responds to endpoint methods |
151 | | - # |
152 | | - # @param endpoint_name [String, Symbol] Method name to check |
153 | | - # @param include_all [Boolean] Include private methods in check |
154 | | - # @return [Boolean] True if client responds to the method |
155 | | - def respond_to_missing?(endpoint_name, include_all = false) |
156 | | - Endpoint.valid_name?(endpoint_name) || super |
157 | | - end |
| 78 | + # Set the API key |
| 79 | + # |
| 80 | + # @param apikey [String] New API key |
| 81 | + # @return [String] The API key that was set |
| 82 | + def apikey=(apikey) |
| 83 | + @configuration[:apikey] = apikey |
| 84 | + end |
158 | 85 |
|
159 | | - private |
| 86 | + # Get the connection timeout |
| 87 | + # |
| 88 | + # @return [Integer] Connection timeout in milliseconds |
| 89 | + def connect_timeout |
| 90 | + parse_timeout(@configuration[:connect_timeout]) |
| 91 | + end |
160 | 92 |
|
161 | | - def reset_configuration |
162 | | - @configuration = { |
163 | | - connect_timeout: DEFAULT_CONNECT_TIMEOUT |
164 | | - } |
| 93 | + # Set the connection timeout |
| 94 | + # |
| 95 | + # @param timeout [Integer, String] New timeout value |
| 96 | + # @return [Integer] The timeout that was set |
| 97 | + def connect_timeout=(timeout) |
| 98 | + @configuration[:connect_timeout] = parse_timeout(timeout) |
| 99 | + end |
| 100 | + |
| 101 | + # Get the environment variable name for the API key |
| 102 | + # |
| 103 | + # @return [String] Environment variable name |
| 104 | + def apikey_env_var_name |
| 105 | + (@configuration[:apikey_env_var_name] || APIKEY_ENV_NAME).upcase |
| 106 | + end |
| 107 | + |
| 108 | + # Set the environment variable name for the API key |
| 109 | + # |
| 110 | + # @param var_name [String] New environment variable name |
| 111 | + # @return [String] The variable name that was set (uppercased) |
| 112 | + def apikey_env_var_name=(var_name) |
| 113 | + @configuration[:apikey_env_var_name] = var_name.upcase |
| 114 | + end |
| 115 | + |
| 116 | + # Fetch data from an API endpoint |
| 117 | + # |
| 118 | + # @param request [Request] Request object to send |
| 119 | + # @return [Response, Hash, ResponseError] Response or error information |
| 120 | + def fetch(request) |
| 121 | + return nil unless request |
| 122 | + |
| 123 | + if request.valid? |
| 124 | + http_response = self.class.request(request) |
| 125 | + raise HTTPX::Error, "HTTP request failed" if http_response.error && http_response.response.nil? |
| 126 | + |
| 127 | + Response.resolve(http_response, request) |
| 128 | + else |
| 129 | + { errors: request.errors } |
165 | 130 | end |
| 131 | + rescue StandardError => e |
| 132 | + handle_fetch_error(e, request) |
| 133 | + end |
166 | 134 |
|
167 | | - def parse_timeout(value) |
168 | | - Utils.to_integer(value, DEFAULT_CONNECT_TIMEOUT) |
| 135 | + # Handle method calls for API endpoints |
| 136 | + # |
| 137 | + # @param endpoint_name [String, Symbol] API endpoint name |
| 138 | + # @param endpoint_params [Hash] Parameters for the endpoint |
| 139 | + # @return [Response, Hash, ResponseError] API response or error |
| 140 | + def method_missing(endpoint_name, **endpoint_params, &block) |
| 141 | + if Endpoint.valid_name?(endpoint_name) |
| 142 | + define_endpoint_method(endpoint_name) |
| 143 | + send(endpoint_name, **endpoint_params) |
| 144 | + else |
| 145 | + super |
169 | 146 | end |
| 147 | + end |
| 148 | + |
| 149 | + # Check if client responds to endpoint methods |
| 150 | + # |
| 151 | + # @param endpoint_name [String, Symbol] Method name to check |
| 152 | + # @param include_all [Boolean] Include private methods in check |
| 153 | + # @return [Boolean] True if client responds to the method |
| 154 | + def respond_to_missing?(endpoint_name, include_all = false) |
| 155 | + Endpoint.valid_name?(endpoint_name) || super |
| 156 | + end |
| 157 | + |
| 158 | + private |
170 | 159 |
|
171 | | - def handle_fetch_error(error, request) |
172 | | - case error |
173 | | - when HTTPX::Error |
174 | | - NetworkError.new( |
175 | | - message: "Network error occurred: #{error.message}", |
176 | | - original_error: error |
177 | | - ) |
178 | | - else |
179 | | - ResponseError.new( |
180 | | - message: "Unexpected error: #{error.message}", |
181 | | - request: request, |
182 | | - original_error: error |
183 | | - ) |
184 | | - end |
| 160 | + def reset_configuration |
| 161 | + @configuration = { |
| 162 | + connect_timeout: DEFAULT_CONNECT_TIMEOUT, |
| 163 | + } |
| 164 | + end |
| 165 | + |
| 166 | + def parse_timeout(value) |
| 167 | + Utils.to_integer(value, DEFAULT_CONNECT_TIMEOUT) |
| 168 | + end |
| 169 | + |
| 170 | + def handle_fetch_error(error, request) |
| 171 | + case error |
| 172 | + when HTTPX::Error |
| 173 | + NetworkError.new( |
| 174 | + message: "Network error occurred: #{error.message}", |
| 175 | + original_error: error, |
| 176 | + ) |
| 177 | + else |
| 178 | + ResponseError.new( |
| 179 | + message: "Unexpected error: #{error.message}", |
| 180 | + request: request, |
| 181 | + original_error: error, |
| 182 | + ) |
185 | 183 | end |
| 184 | + end |
186 | 185 |
|
187 | | - def define_endpoint_method(endpoint_name) |
188 | | - return if @endpoint_methods_defined.include?(endpoint_name) |
| 186 | + def define_endpoint_method(endpoint_name) |
| 187 | + return if @endpoint_methods_defined.include?(endpoint_name) |
189 | 188 |
|
190 | | - define_singleton_method(endpoint_name) do |**params| |
191 | | - @endpoint_methods_defined.add(endpoint_name) |
192 | | - request = Request.new(endpoint_name, **params) |
193 | | - fetch(request) |
194 | | - end |
| 189 | + define_singleton_method(endpoint_name) do |**params| |
| 190 | + @endpoint_methods_defined.add(endpoint_name) |
| 191 | + request = Request.new(endpoint_name, **params) |
| 192 | + fetch(request) |
195 | 193 | end |
196 | 194 | end |
197 | 195 | end |
0 commit comments