Class: TwelvedataRuby::Client

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

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

Parameters:

  • endpoint_name (String, Symbol)

    API endpoint name

  • endpoint_params (Hash)

    Parameters for the endpoint

Returns:



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

#configurationObject (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

Parameters:

Returns:

  • (Array)

    Array of HTTP request specs



37
38
39
# File 'lib/twelvedata_ruby/client.rb', line 37

def build_requests(requests)
  Utils.to_array(requests).map(&:build)
end

.http_optionsHash

Get HTTP client options

Returns:

  • (Hash)

    HTTPX options



44
45
46
47
48
49
# File 'lib/twelvedata_ruby/client.rb', line 44

def http_options
  {
    origin: BASE_URL,
    timeout: { connect_timeout: instance.connect_timeout },
  }
end

.request(request_objects, **options) ⇒ HTTPX::Response+

Make HTTP requests using HTTPX

Parameters:

  • request_objects (Request, Array<Request>)

    Request object(s) to send

  • options (Hash)

    Additional HTTPX options

Returns:

  • (HTTPX::Response, Array<HTTPX::Response>)

    HTTP response(s)



26
27
28
29
30
31
# File 'lib/twelvedata_ruby/client.rb', line 26

def request(request_objects, **options)
  requests = build_requests(request_objects)
  http_client = HTTPX.with(http_options.merge(options))

  http_client.request(requests)
end

Instance Method Details

#apikeyString?

Get the current API key

Returns:

  • (String, nil)

    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

Parameters:

  • apikey (String)

    New API key

Returns:

  • (String)

    The API key that was set



83
84
85
# File 'lib/twelvedata_ruby/client.rb', line 83

def apikey=(apikey)
  @configuration[:apikey] = apikey
end

#apikey_env_var_nameString

Get the environment variable name for the API key

Returns:

  • (String)

    Environment variable name



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

Parameters:

  • var_name (String)

    New environment variable name

Returns:

  • (String)

    The variable name that was set (uppercased)



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

Parameters:

  • options (Hash)

    Configuration options

Options Hash (**options):

  • :apikey (String)

    API key for authentication

  • :connect_timeout (Integer)

    Connection timeout in milliseconds

  • :apikey_env_var_name (String)

    Environment variable name for API key

Returns:

  • (self)

    Returns self for method chaining



67
68
69
70
# File 'lib/twelvedata_ruby/client.rb', line 67

def configure(**options)
  @configuration.merge!(options)
  self
end

#connect_timeoutInteger

Get the connection timeout

Returns:

  • (Integer)

    Connection timeout in milliseconds



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

Parameters:

  • timeout (Integer, String)

    New timeout value

Returns:

  • (Integer)

    The timeout that was set



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

Parameters:

  • request (Request)

    Request object to send

Returns:



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.message}",
      original_error: error,
    )
  else
    ResponseError.new(
      message: "Unexpected error: #{error.message}",
      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_configurationObject (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

Parameters:

  • endpoint_name (String, Symbol)

    Method name to check

  • include_all (Boolean) (defaults to: false)

    Include private methods in check

Returns:

  • (Boolean)

    True if client responds to the method



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