Created
May 24, 2013 21:54
-
-
Save nmarley/5646759 to your computer and use it in GitHub Desktop.
Example of how Btce::TradeAPI could be initialized
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
#<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"> |
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
#! /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