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 =

Returns the exported shell ENV variable name that holds the apikey.

Returns:

  • (String)

    the exported shell ENV variable name that holds the apikey

"TWELVEDATA_API_KEY"
CONNECT_TIMEOUT =

Returns CONNECT_TIMEOUT default connection timeout in milliseconds.

Returns:

  • (Integer)

    CONNECT_TIMEOUT default connection timeout in milliseconds

120
BASE_URL =

Returns valid URI base url string of the API.

Returns:

  • (String)

    valid URI base url string of the API

"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.

Parameters:

  • endpoint_name (String)

    valid API endpoint name to fetch

  • endpoint_params (Hash)

    the optional/required valid query params of the API endpoint. If :apikey key-value pair is present, the pair will override the #apikey of singleton client instance If :format key-value pair is present and is a valid parameter key and value can only be :csv or :json If :filename key-value is present and :format is :csv, then this is will be added to the payload too.

    Otherwise, this will just discarded and will not be part of the payload
    

    If endpoint name and query params used are not valid, EndpointError instances will be returned

    actual API fetch will not happen. @see #fetch for the rest of the documentation
    


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

Returns the options writeonly attribute that may contain values to override the default attribute values. This attribute writer was automatically called in @see TwelvedataRuby.client(**options).

Returns:

  • (Hash)

    the options writeonly attribute that may contain values to override the default attribute values. This attribute writer was automatically called in @see TwelvedataRuby.client(**options).



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

Returns apikey value from the instance options Hash object but if nill use the value from ENV.

Returns:

  • (String)

    apikey value from the instance options Hash object but if nill use the value from ENV



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

Parameters:

  • apikey (String)

Returns:

  • (String)

    apikey value



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

Parameters:

  • apikey_env_var_name (String)

Returns:

  • (String)

    the ENV variable name

See Also:



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, ...

Parameters:

  • request (Request)

    built API request object that holds the endpoint payload

Returns:

  • (NilClass)

    nil if @param request is not truthy

  • (Hash)

    :errors if the request is not valid will hold the endpoint errors details @see Endpoint#errors

  • (Response)

    if request is valid and received an actual response from the API server. The response object's #error may or may not return a kind of ResponseError @see Response#error

  • (ResponseError)

    if the response received did not come from the API server itself.



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

Returns:

  • (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