-
-
Save vrinek/d032b59a16e4369b95ccef5a87beace9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "json" | |
class GDAX::HistoricRate | |
getter time : Time | |
getter low : Float64 | |
getter high : Float64 | |
getter open : Float64 | |
getter close : Float64 | |
getter volume : Float64 | |
def initialize(@time, @low, @high, @open, @close, @volume) | |
end | |
# [ time, low, high, open, close, volume ] | |
def initialize(pull : JSON::PullParser) | |
t = Tuple(Int32, Float64, Float64, Float64, Float64, Float64).new(pull) | |
@time = Time.epoch(t[0]) | |
@low = t[1] | |
@high = t[2] | |
@open = t[3] | |
@close = t[4] | |
@volume = t[5] | |
end | |
# [ time, low, high, open, close, volume ] | |
def to_json(builder : JSON::Builder) | |
{@time.epoch, @low, @high, @open, @close, @volume}.to_json(builder) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "../spec_helper" | |
describe GDAX::HistoricRate do | |
it "renders to JSON" do | |
rate = GDAX::HistoricRate.new( | |
time: Time.new(2016, 2, 15, 10, 20, 30), | |
low: 500.0, | |
high: 600.0, | |
open: 510.0, | |
close: 550.0, | |
volume: 120.0, | |
) | |
rate.to_json.should eq("[1455528030,500.0,600.0,510.0,550.0,120.0]") | |
end | |
it "initializes from JSON" do | |
json = "[1455528030,500.0,600.0,510.0,550.0,120.0]" | |
rate = GDAX::HistoricRate.from_json(json) | |
rate.should be_a(GDAX::HistoricRate) | |
if rate | |
rate.time.should eq(Time.new(2016, 2, 15, 10, 20, 30)) | |
rate.low.should eq(500.0) | |
rate.high.should eq(600.0) | |
rate.open.should eq(510.0) | |
rate.close.should eq(550.0) | |
rate.volume.should eq(120.0) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment