Class: TwelvedataRuby::Client
- Inherits:
-
Object
- Object
- TwelvedataRuby::Client
- 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.
"TWELVEDATA_API_KEY"
- CONNECT_TIMEOUT =
Returns CONNECT_TIMEOUT default connection timeout in milliseconds.
120
- BASE_URL =
Returns valid URI base url string of the API.
"https://api.twelvedata.com"
Instance Attribute Summary collapse
-
#options ⇒ Hash
The options writeonly attribute that may contain values to override the default attribute values.
Class Method Summary collapse
- .build_requests(requests) ⇒ Object
- .options ⇒ Object
- .origin ⇒ Object
- .request(request_objects, opts = {}) ⇒ Object
- .timeout ⇒ Object
Instance Method Summary collapse
-
#apikey ⇒ String
Apikey value from the instance options Hash object but if nill use the value from
ENV[APIKEY_ENV_NAME]
. -
#apikey=(apikey) ⇒ String
The writer method that can be used to pass manually the value of the
apikey
. -
#apikey_env_var_name ⇒ Object
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.
-
#apikey_env_var_name=(apikey_env_var_name) ⇒ String
A setter helper method to configure the ENV variable name of the API key.
- #connect_timeout ⇒ Object
- #connect_timeout=(connect_timeout) ⇒ Object
- #fetch(request) ⇒ NilClass, ...
-
#method_missing(endpoint_name, **endpoint_params, &_block) ⇒ Object
The entry point in dynamically defining instance methods based on the called the valid endpoint names.
- #respond_to_missing?(endpoint_name, _include_all = false) ⇒ Boolean
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(endpoint_name, **endpoint_params, &_block) ⇒ Object
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
#options ⇒ Hash
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).
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 |
.options ⇒ Object
35 36 37 |
# File 'lib/twelvedata_ruby/client.rb', line 35 def origin.merge(timeout) end |
.origin ⇒ Object
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(.merge(opts)).request(build_requests(request_objects)) end |
.timeout ⇒ Object
31 32 33 |
# File 'lib/twelvedata_ruby/client.rb', line 31 def timeout {timeout: {connect_timeout: instance.connect_timeout}} end |
Instance Method Details
#apikey ⇒ String
Returns apikey value from the instance options Hash object but if nill use the value from ENV[APIKEY_ENV_NAME]
.
48 49 50 |
# File 'lib/twelvedata_ruby/client.rb', line 48 def apikey Utils.empty_to_nil([: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) [:apikey] = apikey end |
#apikey_env_var_name ⇒ Object
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 ([: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) [:apikey_env_var_name] = apikey_env_var_name end |
#connect_timeout ⇒ Object
59 60 61 |
# File 'lib/twelvedata_ruby/client.rb', line 59 def connect_timeout parse_connect_timeout([: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 |