class Medici::Historical

Constants

API_BASE_URL

Attributes

close[R]
date[R]
high[R]
low[R]
open[R]
volume[R]

Public Class Methods

new(attributes) click to toggle source
# File lib/medici/historical.rb, line 43
def initialize(attributes)
  @date = Date.parse(attributes[0])
  @open = attributes[1].to_f
  @high = attributes[2].to_f
  @low = attributes[3].to_f
  @close = attributes[4].to_f
  @volume = attributes[5].to_i
end
quote(symbol, start_date = nil, end_date = nil) click to toggle source
# File lib/medici/historical.rb, line 10
def self.quote(symbol, start_date = nil, end_date = nil)
  request = self.build_request(symbol, start_date, end_date)
  response = Net::HTTP.get_response(URI.parse(request))
  raise HistoricalDataError if response.code != '200'

  self.parse(response.body)
end

Private Class Methods

build_request(symbol, start_date, end_date) click to toggle source
# File lib/medici/historical.rb, line 20
def self.build_request(symbol, start_date, end_date)
  params = {
    q: symbol,
    startdate: start_date,
    enddate: end_date,
    output: 'csv'
  }

  API_BASE_URL + params.each_with_object([]) do |(p, v), acc|
    acc << "#{p}=#{v}" if not v.nil?
  end.join('&')
end
parse(response) click to toggle source
# File lib/medici/historical.rb, line 33
def self.parse(response)
  rows = response.split("\n")
  rows.shift
  rows.map do |row|
    new(row.split(','))
  end
end