Class: TwelvedataRuby::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/twelvedata_ruby/endpoint.rb

Constant Summary collapse

DEFAULT_FORMAT =
:json
VALID_FORMATS =
[DEFAULT_FORMAT, :csv].freeze
DEFINITIONS =
{
  api_usage: {
    parameters: {keys: %i[format]},
    response: {keys: %i[timestamp current_usage plan_limit]}
  },
  stocks: {
    parameters: {keys: %i[symbol exchange country type format]},
    response: {data_keys: %i[symbol name currency exchange country type], collection: :data}
  },
  forex_pairs: {
    parameters: {keys: %i[symbol currency_base currency_quote format]},
    response: {data_keys: %i[symbol currency_group currency_base currency_quote], collection: :data}
  },
  cryptocurrencies: {
    parameters: {keys: %i[symbol exchange currency_base currency_quote format]},
    response: {data_keys: %i[symbol available_exchanges currency_base currency_quote], collection: :data}
  },
  etf: {
    parameters: {keys: %i[symbol format]},
    response: {data_keys: %i[symbol name currency exchange], collection: :data}
  },
  indices: {
    parameters: {keys: %i[symbol country format]},
    response: {data_keys: %i[symbol name country currency], collection: :data}
  },
  exchanges: {
    parameters: {keys: %i[type name code country format]},
    response: {data_keys: %i[name country code timezone], collection: :data}
  },
  cryptocurrency_exchanges: {
    parameters: {keys: %i[name format]},
    response: {data_keys: %i[name], collection: :data}
  },
  technical_indicators: {
    parameters: {keys: []},
    response: {
      keys: %i[enable full_name description type overlay parameters output_values tinting]
    }
  },
  symbol_search: {
    parameters: {keys: %i[symbol outputsize], required: %i[symbol]},
    response: {
      data_keys: %i[symbol instrument_name exchange exchange_timezone instrument_type country],
      collection: :data
    }
  },
  earliest_timestamp: {
    parameters: {keys: %i[symbol interval exchange]},
    response: {keys: %i[datetime unix_time]}
  },
  time_series: {
    parameters: {
      keys: %i[symbol interval exchange country type outputsize format],
      required: %i[symbol interval]
    },
    response: {
      value_keys: %i[datetime open high low close volume],
      collection: :values,
      meta_keys: %i[symbol interval currency exchange_timezone exchange type]
    }
  },
  quote: {
    parameters: {
      keys: %i[symbol interval exchange country volume_time_period type format],
      required: %i[symbol],
    },
    response: {
      keys: %i[
        symbol
        name
        exchange
        currency
        datetime
        open
        high
        low
        close
        volume
        previous_close
        change
        percent_change
        average_volume
        fifty_two_week
      ]
    }
  },
  price: {
    parameters: {keys: %i[symbol exchange country type format], required: %i[symbol]},
    response: {keys: %i[price]}
  },
  eod: {
    parameters: {keys: %i[symbol exchange country type], required: %i[symbol]},
    response: {keys: %i[symbol exchange currency datetime close]}
  },
  exchange_rate: {
    parameters: {keys: %i[symbol format], required: %i[symbol]},
    response: {keys: %i[symbol rate timestamp]}
  },
  currency_conversion: {
    parameters: {keys: %i[symbol amount format], required: %i[symbol amount]},
    response: {keys: %i[symbol rate amount timestamp]}
  },
  complex_data: {
    parameters: {
      keys: %i[symbols intervals start_date end_date dp order timezone methods name],
      required: %i[symbols intervals start_date end_date]
    },
    response: {keys: %i[data status]},
    http_verb: :post
  },
  earnings: {
    parameters: {keys: %i[symbol exchange country type period outputsize format], required: %i[symbol]},
    response: {keys: %i[date time eps_estimate eps_actual difference surprise_prc]}
  },
  earnings_calendar: {
    parameters: {keys: %i[format]},
    response: {
      keys: %i[
        symbol
        name
        currency
        exchange
        country
        time
        eps_estimate
        eps_estimate
        eps_actual
        difference
        surprise_prc
      ]
    }
  }
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, **query_params) ⇒ Endpoint

Returns a new instance of Endpoint.



174
175
176
177
# File 'lib/twelvedata_ruby/endpoint.rb', line 174

def initialize(name, **query_params)
  self.name = name
  self.query_params = query_params
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



172
173
174
# File 'lib/twelvedata_ruby/endpoint.rb', line 172

def name
  @name
end

#query_paramsObject

Returns the value of attribute query_params.



172
173
174
# File 'lib/twelvedata_ruby/endpoint.rb', line 172

def query_params
  @query_params
end

Class Method Details

.default_apikey_paramsObject



158
159
160
# File 'lib/twelvedata_ruby/endpoint.rb', line 158

def default_apikey_params
  {apikey: Client.instance.apikey}
end

.definitionsObject



143
144
145
146
147
148
149
150
151
152
# File 'lib/twelvedata_ruby/endpoint.rb', line 143

def definitions
  @definitions ||= DEFINITIONS.transform_values {|v|
    v.merge(
      parameters: {
        keys: v[:parameters][:keys].push(:apikey),
        required: (v[:parameters][:required] || []).push(:apikey)
      }
    )
  }.to_h
end

.namesObject



154
155
156
# File 'lib/twelvedata_ruby/endpoint.rb', line 154

def names
  @names ||= definitions.keys
end

.valid_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/twelvedata_ruby/endpoint.rb', line 162

def valid_name?(name)
  names.include?(name.to_sym)
end

.valid_params?(name, **params) ⇒ Boolean Also known as: valid?

Returns:

  • (Boolean)


166
167
168
# File 'lib/twelvedata_ruby/endpoint.rb', line 166

def valid_params?(name, **params)
  new(name, **params).valid?
end

Instance Method Details

#definitionObject



179
180
181
# File 'lib/twelvedata_ruby/endpoint.rb', line 179

def definition
  @definition ||= self.class.definitions[name]
end

#errorsObject



183
184
185
# File 'lib/twelvedata_ruby/endpoint.rb', line 183

def errors
  (@errors || {}).compact
end

#parametersObject



191
192
193
194
195
196
197
# File 'lib/twelvedata_ruby/endpoint.rb', line 191

def parameters
  return @parameters if definition.nil? || @parameters

  params = definition[:parameters]
  params.push(:filename) if params.include?(:format) && query_parameters[:format] == :csv
  params
end

#parameters_keysObject



199
200
201
202
203
# File 'lib/twelvedata_ruby/endpoint.rb', line 199

def parameters_keys
  keys = parameters&.send(:[], :keys)
  keys.push(:filename) if keys && query_params && query_params[:format] == :csv
  keys
end

#query_params_keysObject



205
206
207
# File 'lib/twelvedata_ruby/endpoint.rb', line 205

def query_params_keys
  query_params.keys
end

#required_parametersObject



218
219
220
# File 'lib/twelvedata_ruby/endpoint.rb', line 218

def required_parameters
  parameters&.send(:[], :required)
end

#valid?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/twelvedata_ruby/endpoint.rb', line 222

def valid?
  valid_name? && valid_query_params?
end

#valid_at_attributes?(*attrs) ⇒ Boolean

Returns:

  • (Boolean)


226
227
228
# File 'lib/twelvedata_ruby/endpoint.rb', line 226

def valid_at_attributes?(*attrs)
  errors.values_at(*attrs).compact.empty?
end

#valid_name?Boolean

Returns:

  • (Boolean)


230
231
232
# File 'lib/twelvedata_ruby/endpoint.rb', line 230

def valid_name?
  valid_at_attributes?(:name)
end

#valid_query_params?Boolean

Returns:

  • (Boolean)


234
235
236
# File 'lib/twelvedata_ruby/endpoint.rb', line 234

def valid_query_params?
  valid_at_attributes?(:parameters_keys, :required_parameters)
end