Class: TwelvedataRuby::Client

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/twelvedata_ruby/client.rb

Overview

Responsible of the actual communication – sending a valid request

and receiving the response  -- of the API web server

Constant Summary collapse

APIKEY_ENV_NAME =
"TWELVEDATA_API_KEY"
CONNECT_TIMEOUT =
120
BASE_URL =
"https://api.twelvedata.com"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(endpoint_name, **endpoint_params, &_block) ⇒ Object

TODO:

define all the method signatures of the endpoint methods that will meta-programatically defined at runtime.

The entry point in dynamically defining instance methods based on the called the valid endpoint names.



114
115
116
# File 'lib/twelvedata_ruby/client.rb', line 114

def method_missing(endpoint_name, **endpoint_params, &_block)
  try_fetch(endpoint_name, endpoint_params) || super
end

Instance Attribute Details

#optionsHash



44
# File 'lib/twelvedata_ruby/client.rb', line 44

attr_writer :options

Class Method Details

.build_requests(requests) ⇒ Object



23
24
25
# File 'lib/twelvedata_ruby/client.rb', line 23

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

.optionsObject



35
36
37
# File 'lib/twelvedata_ruby/client.rb', line 35

def options
  origin.merge(timeout)
end

.originObject



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

def origin
  @origin ||= {origin: BASE_URL}
end

.request(request_objects, opts = {}) ⇒ Object



19
20
21
# File 'lib/twelvedata_ruby/client.rb', line 19

def request(request_objects, opts={})
  HTTPX.with(options.merge(opts)).request(build_requests(request_objects))
end

.timeoutObject



31
32
33
# File 'lib/twelvedata_ruby/client.rb', line 31

def timeout
  {timeout: {connect_timeout: instance.connect_timeout}}
end

Instance Method Details

#apikeyString



48
49
50
# File 'lib/twelvedata_ruby/client.rb', line 48

def apikey
  Utils.empty_to_nil(options[:apikey]) || ENV[apikey_env_var_name]
end

#apikey=(apikey) ⇒ String

The writer method that can be used to pass manually the value of the apikey



55
56
57
# File 'lib/twelvedata_ruby/client.rb', line 55

def apikey=(apikey)
  options[:apikey] = apikey
end

#apikey_env_var_nameObject

The name of the ENVIRONMENT variable that may hold the value of the Twelve Data API key # @return [String] the ENV variable that will be used to fetch from ENV the value of the API key



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

def apikey_env_var_name
  (options[:apikey_env_var_name] || APIKEY_ENV_NAME).upcase
end

#apikey_env_var_name=(apikey_env_var_name) ⇒ String

A setter helper method to configure the ENV variable name of the API key



77
78
79
# File 'lib/twelvedata_ruby/client.rb', line 77

def apikey_env_var_name=(apikey_env_var_name)
  options[:apikey_env_var_name] = apikey_env_var_name
end

#connect_timeoutObject



59
60
61
# File 'lib/twelvedata_ruby/client.rb', line 59

def connect_timeout
  parse_connect_timeout(options[:connect_timeout])
end

#connect_timeout=(connect_timeout) ⇒ Object



63
64
65
# File 'lib/twelvedata_ruby/client.rb', line 63

def connect_timeout=(connect_timeout)
  parse_connect_timeout(connect_timeout)
end

#fetch(request) ⇒ NilClass, ...



97
98
99
100
101
# File 'lib/twelvedata_ruby/client.rb', line 97

def fetch(request)
  return nil unless request

  request.valid? ? Response.resolve(self.class.request(request), request) : {errors: request.errors}
end

#respond_to_missing?(endpoint_name, _include_all = false) ⇒ Boolean



122
123
124
125
126
# File 'lib/twelvedata_ruby/client.rb', line 122

def respond_to_missing?(endpoint_name, _include_all=false)
  Utils.return_nil_unless_true(Endpoint.valid_name?(endpoint_name)) {
    define_endpoint_method(endpoint_name)
  } || super
end