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
        dp
        order
        timezone
        start_date
        end_date
        previous_close
      ],
      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 prepost dp], required: %i[symbol]},
    response: {keys: %i[symbol exchange currency datetime close]}
  },
  exchange_rate: {
    parameters: {keys: %i[symbol format precision timezone], required: %i[symbol]},
    response: {keys: %i[symbol rate timestamp]}
  },
  currency_conversion: {
    parameters: {keys: %i[symbol amount format precision timezone], 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.



188
189
190
191
# File 'lib/twelvedata_ruby/endpoint.rb', line 188

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

Instance Attribute Details

#nameObject

Returns the value of attribute name.



186
187
188
# File 'lib/twelvedata_ruby/endpoint.rb', line 186

def name
  @name
end

#query_paramsObject

Returns the value of attribute query_params.



186
187
188
# File 'lib/twelvedata_ruby/endpoint.rb', line 186

def query_params
  @query_params
end

Class Method Details

.default_apikey_paramsObject



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

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

.definitionsObject



157
158
159
160
161
162
163
164
165
166
# File 'lib/twelvedata_ruby/endpoint.rb', line 157

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



168
169
170
# File 'lib/twelvedata_ruby/endpoint.rb', line 168

def names
  @names ||= definitions.keys
end

.valid_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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

Returns:

  • (Boolean)


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

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

Instance Method Details

#definitionObject



193
194
195
# File 'lib/twelvedata_ruby/endpoint.rb', line 193

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

#errorsObject



197
198
199
# File 'lib/twelvedata_ruby/endpoint.rb', line 197

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

#parametersObject



205
206
207
208
209
210
211
# File 'lib/twelvedata_ruby/endpoint.rb', line 205

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



213
214
215
216
217
# File 'lib/twelvedata_ruby/endpoint.rb', line 213

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

#query_params_keysObject



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

def query_params_keys
  query_params.keys
end

#required_parametersObject



232
233
234
# File 'lib/twelvedata_ruby/endpoint.rb', line 232

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

#valid?Boolean

Returns:

  • (Boolean)


236
237
238
# File 'lib/twelvedata_ruby/endpoint.rb', line 236

def valid?
  valid_name? && valid_query_params?
end

#valid_at_attributes?(*attrs) ⇒ Boolean

Returns:

  • (Boolean)


240
241
242
# File 'lib/twelvedata_ruby/endpoint.rb', line 240

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

#valid_name?Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/twelvedata_ruby/endpoint.rb', line 244

def valid_name?
  valid_at_attributes?(:name)
end

#valid_query_params?Boolean

Returns:

  • (Boolean)


248
249
250
# File 'lib/twelvedata_ruby/endpoint.rb', line 248

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