class TwelvedataRuby::Client

Responsible of the actual communication – sending a valid request

and receiving the response  -- of the API web server

Constants

APIKEY_ENV_NAME

@return [String] the exported shell ENV variable name that holds the apikey

BASE_URL

@return [String] valid URI base url string of the API

CONNECT_TIMEOUT

@return [Integer] CONNECT_TIMEOUT default connection timeout in milliseconds

Attributes

options[W]

@!attribute options

@return [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).

@see TwelvedataRuby.client

Public Class Methods

build_requests(requests) click to toggle source
# File lib/twelvedata_ruby/client.rb, line 23
def build_requests(requests)
  Utils.to_a(requests).map(&:build)
end
options() click to toggle source
# File lib/twelvedata_ruby/client.rb, line 35
def options
  origin.merge(timeout)
end
origin() click to toggle source
# File lib/twelvedata_ruby/client.rb, line 27
def origin
  @origin ||= {origin: BASE_URL}
end
request(request_objects, opts={}) click to toggle source
# File lib/twelvedata_ruby/client.rb, line 19
def request(request_objects, opts={})
  HTTPX.with(options.merge(opts)).request(build_requests(request_objects))
end
timeout() click to toggle source
# File lib/twelvedata_ruby/client.rb, line 31
def timeout
  {timeout: {connect_timeout: instance.connect_timeout}}
end

Public Instance Methods

apikey() click to toggle source

@return [String] apikey value from the instance options Hash object

but if nill use the value from +ENV[APIKEY_ENV_NAME]+
# File lib/twelvedata_ruby/client.rb, line 48
def apikey
  Utils.empty_to_nil(options[:apikey]) || ENV[apikey_env_var_name]
end
apikey=(apikey) click to toggle source

The writer method that can be used to pass manually the value of the apikey @param [String] apikey @return [String] apikey value

# File lib/twelvedata_ruby/client.rb, line 55
def apikey=(apikey)
  options[:apikey] = apikey
end
apikey_env_var_name() click to toggle source

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

# 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) click to toggle source

A setter helper method to configure the ENV variable name of the API key @param [String] apikey_env_var_name @return [String] the ENV variable name @see apikey_env_var_name

# 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_timeout() click to toggle source
# File lib/twelvedata_ruby/client.rb, line 59
def connect_timeout
  parse_connect_timeout(options[:connect_timeout])
end
connect_timeout=(connect_timeout) click to toggle source
# File lib/twelvedata_ruby/client.rb, line 63
def connect_timeout=(connect_timeout)
  parse_connect_timeout(connect_timeout)
end
fetch(request) click to toggle source

@param [Request] request built API request object that holds the endpoint payload

@return [NilClass] nil if @param request is not truthy @return [Hash] :errors if the request is not valid will hold the endpoint errors details

@see Endpoint#errors

@return [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

@return [ResponseError] if the response received did not come from the API server itself.

# 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
method_missing(endpoint_name, **endpoint_params, &_block) click to toggle source

The entry point in dynamically defining instance methods based on the called the valid endpoint names. @param [String] endpoint_name valid API endpoint name to fetch @param [Hash] endpoint_params 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

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

Calls superclass method
# File lib/twelvedata_ruby/client.rb, line 114
def method_missing(endpoint_name, **endpoint_params, &_block)
  try_fetch(endpoint_name, endpoint_params) || super
end
options() click to toggle source
# File lib/twelvedata_ruby/client.rb, line 118
def options
  @options || @options = {}
end
respond_to_missing?(endpoint_name, _include_all=false) click to toggle source
Calls superclass method
# 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

Private Instance Methods

build_request(endpoint_name, endpoint_params) click to toggle source
# File lib/twelvedata_ruby/client.rb, line 130
def build_request(endpoint_name, endpoint_params)
  Request.new(endpoint_name, **endpoint_params)
end
define_endpoint_method(endpoint_name) click to toggle source
# File lib/twelvedata_ruby/client.rb, line 138
def define_endpoint_method(endpoint_name)
  self.class.define_method(endpoint_name) do |**qparams|
    fetch(build_request(__method__, qparams))
  end
end
parse_connect_timeout(milliseconds) click to toggle source
# File lib/twelvedata_ruby/client.rb, line 144
def parse_connect_timeout(milliseconds)
  options[:connect_timeout] = Utils.to_d(milliseconds, CONNECT_TIMEOUT)
end
try_fetch(endpoint_name, endpoint_params) click to toggle source
# File lib/twelvedata_ruby/client.rb, line 134
def try_fetch(endpoint_name, endpoint_params)
  respond_to?(endpoint_name) ? fetch(build_request(endpoint_name, endpoint_params)) : nil
end