Class: TwelvedataRuby::Client
- Inherits:
-
Object
- Object
- TwelvedataRuby::Client
- Includes:
- Singleton
- Defined in:
- lib/twelvedata_ruby/client.rb
Constant Summary collapse
- APIKEY_ENV_NAME =
Default environment variable name for API key
"TWELVEDATA_API_KEY"
- DEFAULT_CONNECT_TIMEOUT =
Default connection timeout in milliseconds
120
- BASE_URL =
Base URL for the Twelve Data API
"https://api.twelvedata.com"
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
Class Method Summary collapse
-
.build_requests(requests) ⇒ Array
Build HTTP requests from Request objects.
-
.http_options ⇒ Hash
Get HTTP client options.
-
.request(request_objects, **options) ⇒ HTTPX::Response+
Make HTTP requests using HTTPX.
Instance Method Summary collapse
-
#apikey ⇒ String?
Get the current API key.
-
#apikey=(apikey) ⇒ String
Set the API key.
-
#apikey_env_var_name ⇒ String
Get the environment variable name for the API key.
-
#apikey_env_var_name=(var_name) ⇒ String
Set the environment variable name for the API key.
-
#configure(**options) ⇒ self
Configure the client with new options.
-
#connect_timeout ⇒ Integer
Get the connection timeout.
-
#connect_timeout=(timeout) ⇒ Integer
Set the connection timeout.
- #define_endpoint_method(endpoint_name) ⇒ Object private
-
#fetch(request) ⇒ Response, ...
Fetch data from an API endpoint.
- #handle_fetch_error(error, request) ⇒ Object private
-
#initialize ⇒ Client
constructor
A new instance of Client.
-
#method_missing(endpoint_name, **endpoint_params, &block) ⇒ Response, ...
Handle method calls for API endpoints.
- #parse_timeout(value) ⇒ Object private
- #reset_configuration ⇒ Object private
-
#respond_to_missing?(endpoint_name, include_all = false) ⇒ Boolean
Check if client responds to endpoint methods.
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
54 55 56 57 58 |
# File 'lib/twelvedata_ruby/client.rb', line 54 def initialize @configuration = {} @endpoint_methods_defined = Set.new reset_configuration end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(endpoint_name, **endpoint_params, &block) ⇒ Response, ...
Handle method calls for API endpoints
141 142 143 144 145 146 147 148 |
# File 'lib/twelvedata_ruby/client.rb', line 141 def method_missing(endpoint_name, **endpoint_params, &block) if Endpoint.valid_name?(endpoint_name) define_endpoint_method(endpoint_name) send(endpoint_name, **endpoint_params) else super end end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
52 53 54 |
# File 'lib/twelvedata_ruby/client.rb', line 52 def configuration @configuration end |
Class Method Details
.build_requests(requests) ⇒ Array
Build HTTP requests from Request objects
37 38 39 |
# File 'lib/twelvedata_ruby/client.rb', line 37 def build_requests(requests) Utils.to_array(requests).map(&:build) end |
.http_options ⇒ Hash
Get HTTP client options
44 45 46 47 48 49 |
# File 'lib/twelvedata_ruby/client.rb', line 44 def { origin: BASE_URL, timeout: { connect_timeout: instance.connect_timeout }, } end |
.request(request_objects, **options) ⇒ HTTPX::Response+
Make HTTP requests using HTTPX
26 27 28 29 30 31 |
# File 'lib/twelvedata_ruby/client.rb', line 26 def request(request_objects, **) requests = build_requests(request_objects) http_client = HTTPX.with(.merge()) http_client.request(requests) end |
Instance Method Details
#apikey ⇒ String?
Get the current API key
75 76 77 |
# File 'lib/twelvedata_ruby/client.rb', line 75 def apikey Utils.empty_to_nil(@configuration[:apikey]) || ENV[apikey_env_var_name] end |
#apikey=(apikey) ⇒ String
Set the API key
83 84 85 |
# File 'lib/twelvedata_ruby/client.rb', line 83 def apikey=(apikey) @configuration[:apikey] = apikey end |
#apikey_env_var_name ⇒ String
Get the environment variable name for the API key
105 106 107 |
# File 'lib/twelvedata_ruby/client.rb', line 105 def apikey_env_var_name (@configuration[:apikey_env_var_name] || APIKEY_ENV_NAME).upcase end |
#apikey_env_var_name=(var_name) ⇒ String
Set the environment variable name for the API key
113 114 115 |
# File 'lib/twelvedata_ruby/client.rb', line 113 def apikey_env_var_name=(var_name) @configuration[:apikey_env_var_name] = var_name.upcase end |
#configure(**options) ⇒ self
Configure the client with new options
67 68 69 70 |
# File 'lib/twelvedata_ruby/client.rb', line 67 def configure(**) @configuration.merge!() self end |
#connect_timeout ⇒ Integer
Get the connection timeout
90 91 92 |
# File 'lib/twelvedata_ruby/client.rb', line 90 def connect_timeout parse_timeout(@configuration[:connect_timeout]) end |
#connect_timeout=(timeout) ⇒ Integer
Set the connection timeout
98 99 100 |
# File 'lib/twelvedata_ruby/client.rb', line 98 def connect_timeout=(timeout) @configuration[:connect_timeout] = parse_timeout(timeout) end |
#define_endpoint_method(endpoint_name) ⇒ Object (private)
187 188 189 190 191 192 193 194 195 |
# File 'lib/twelvedata_ruby/client.rb', line 187 def define_endpoint_method(endpoint_name) return if @endpoint_methods_defined.include?(endpoint_name) define_singleton_method(endpoint_name) do |**params| @endpoint_methods_defined.add(endpoint_name) request = Request.new(endpoint_name, **params) fetch(request) end end |
#fetch(request) ⇒ Response, ...
Fetch data from an API endpoint
121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/twelvedata_ruby/client.rb', line 121 def fetch(request) return nil unless request if request.valid? http_response = self.class.request(request) raise HTTPX::Error, "HTTP request failed" if http_response.error && http_response.response.nil? Response.resolve(http_response, request) else { errors: request.errors } end rescue StandardError => e handle_fetch_error(e, request) end |
#handle_fetch_error(error, request) ⇒ Object (private)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/twelvedata_ruby/client.rb', line 171 def handle_fetch_error(error, request) case error when HTTPX::Error NetworkError.new( message: "Network error occurred: #{error.}", original_error: error, ) else ResponseError.new( message: "Unexpected error: #{error.}", request: request, original_error: error, ) end end |
#parse_timeout(value) ⇒ Object (private)
167 168 169 |
# File 'lib/twelvedata_ruby/client.rb', line 167 def parse_timeout(value) Utils.to_integer(value, DEFAULT_CONNECT_TIMEOUT) end |
#reset_configuration ⇒ Object (private)
161 162 163 164 165 |
# File 'lib/twelvedata_ruby/client.rb', line 161 def reset_configuration @configuration = { connect_timeout: DEFAULT_CONNECT_TIMEOUT, } end |
#respond_to_missing?(endpoint_name, include_all = false) ⇒ Boolean
Check if client responds to endpoint methods
155 156 157 |
# File 'lib/twelvedata_ruby/client.rb', line 155 def respond_to_missing?(endpoint_name, include_all = false) Endpoint.valid_name?(endpoint_name) || super end |