Skip to content

Instantly share code, notes, and snippets.

@nmarley
Created May 24, 2013
Embed
What would you like to do?
Example of how Btce::TradeAPI could be initialized
#<TradeAPI:0x007fd9143fc6c8 @key="YOUR-API-KEY", @secret="YOUR-SECRET-KEY">
#<TradeAPI:0x007fd9144134e0 @key="MY-OTHER-API-KEY", @secret="MY-OTHER-SECRET-KEY">
#<TradeAPI:0x007fd9144129a0 @key="Master key!", @secret="Super S33krit">
#! /usr/bin/env ruby
require 'yaml'
require 'pp'
class TradeAPI < Object
# default to btce-api-key.yml as the key file argument
def initialize(options = {:key_file => 'btce-api-key.yml'})
# process yml file first
data = {}
if options.has_key?(:key_file) && File.exists?(options[:key_file])
data = YAML::load(
File.open(options[:key_file])
)
end
@key = data.fetch('key', nil)
@secret = data.fetch('secret', nil)
# if :key and/or :secret are explicitly specified, those arguments take
# precedence
@key = options.fetch(:key, @key)
@secret = options.fetch(:secret, @secret)
unless @key && @secret
# throw exception, key and secret must be set, either as args or in yml
# file
raise ArgumentError, "Both :key and :secret must be set."
end
puts "#{self.inspect}"
end
end
t = TradeAPI.new
t = TradeAPI.new :key_file => 'other-btce-api-key.yml'
t = TradeAPI.new :key => "Master key!", :secret => "Super S33krit"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment