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
@!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).
Public Class Methods
# File lib/twelvedata_ruby/client.rb, line 23 def build_requests(requests) Utils.to_a(requests).map(&:build) end
# File lib/twelvedata_ruby/client.rb, line 35 def options origin.merge(timeout) end
# File lib/twelvedata_ruby/client.rb, line 27 def origin @origin ||= {origin: BASE_URL} end
# File lib/twelvedata_ruby/client.rb, line 19 def request(request_objects, opts={}) HTTPX.with(options.merge(opts)).request(build_requests(request_objects)) end
# File lib/twelvedata_ruby/client.rb, line 31 def timeout {timeout: {connect_timeout: instance.connect_timeout}} end
Public Instance Methods
@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
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
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
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
# File lib/twelvedata_ruby/client.rb, line 59 def connect_timeout parse_connect_timeout(options[:connect_timeout]) end
# File lib/twelvedata_ruby/client.rb, line 63 def connect_timeout=(connect_timeout) parse_connect_timeout(connect_timeout) end
@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
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.
# File lib/twelvedata_ruby/client.rb, line 114 def method_missing(endpoint_name, **endpoint_params, &_block) try_fetch(endpoint_name, endpoint_params) || super end
# File lib/twelvedata_ruby/client.rb, line 118 def options @options || @options = {} end
# 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
# File lib/twelvedata_ruby/client.rb, line 130 def build_request(endpoint_name, endpoint_params) Request.new(endpoint_name, **endpoint_params) end
# 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
# File lib/twelvedata_ruby/client.rb, line 144 def parse_connect_timeout(milliseconds) options[:connect_timeout] = Utils.to_d(milliseconds, CONNECT_TIMEOUT) end
# 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