Class: EOD::API

Inherits:
APICake::Base
  • Object
show all
Defined in:
lib/eod/api.rb

Overview

Provides access to all the EOD API endpoints with dynamic methods anc caching.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_token, use_cache: true, cache_dir: nil, cache_life: nil) ⇒ API

Returns a new instance of API.



11
12
13
14
15
16
# File 'lib/eod/api.rb', line 11

def initialize(api_token, use_cache: true, cache_dir: nil, cache_life: nil)
  @api_token = api_token
  cache.disable unless use_cache
  cache.dir = cache_dir if cache_dir
  cache.life = cache_life if cache_life
end

Instance Attribute Details

#api_tokenObject (readonly)

Returns the value of attribute api_token.



9
10
11
# File 'lib/eod/api.rb', line 9

def api_token
  @api_token
end

Instance Method Details

#default_queryObject



18
19
20
# File 'lib/eod/api.rb', line 18

def default_query
  { api_token: api_token, fmt: :json }
end

#get_csv(*args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/eod/api.rb', line 22

def get_csv(*args)
  payload = get!(*args)

  if payload.response.code != '200'
    raise BadResponse, "#{payload.response.code} #{payload.response.msg}"
  end

  data = payload.parsed_response
  data = csv_node data if data.is_a? Hash

  header = data.first.keys

  CSV.generate do |csv|
    csv << header
    data.each { |row| csv << row.values }
  end
end