class Coinstack::List

Object for reading and writing our list

Constants

DEFAULT_LOCATION
PAIRS_URI

Attributes

pairs[RW]
user_pairs[RW]

Public Class Methods

new() click to toggle source
# File lib/coinstack/list.rb, line 16
def initialize
  self.pairs = {}
  update_pairs!
  FileUtils.touch(DEFAULT_LOCATION) # Ensures it exists
  self.user_pairs = YAML.load_file(DEFAULT_LOCATION) || {}
end

Public Instance Methods

add(info) click to toggle source
# File lib/coinstack/list.rb, line 32
def add(info)
  data = {}
  data[info[:symbol]] = info[:amount]
  user_pairs.merge!(data)
  save!
end
remove(symbol) click to toggle source
# File lib/coinstack/list.rb, line 39
def remove(symbol)
  user_pairs.delete(symbol)
  save!
end
save!() click to toggle source
# File lib/coinstack/list.rb, line 44
def save!
  File.open(DEFAULT_LOCATION, 'w') { |f| f.write user_pairs.to_yaml }
end
update_pairs!() click to toggle source
# File lib/coinstack/list.rb, line 23
def update_pairs!
  res = Net::HTTP.get_response(PAIRS_URI)
  raise "Could not update pairs! #{res.code}" unless res.is_a?(Net::HTTPSuccess)
  raw_array = JSON.parse(res.body)
  raw_array.each do |data|
    pairs[data['symbol']] = data
  end
end