module StocksExchange

Constants

VERSION

Public Class Methods

listed?(name) click to toggle source

checks whether a market or currency is present

# File lib/stocks_exchange.rb, line 23
def self.listed?(name)
  StocksExchange.const_defined?("#{name}".upcase)
end
refresh() click to toggle source

updates the values of modules and functions

# File lib/stocks_exchange.rb, line 11
def self.refresh
  begin
    setup_currencies
    setup_markets
    return true
  rescue => e
    puts "StocksExchange failed to load required Modules and Methods. Error: #{e.message}"
    return false
  end
end
setup_currencies() click to toggle source

gets all available currencies from stocks.exchange/api2/currencies and creates dynamic modules with dynamic methods as from the API

# File lib/stocks_exchange.rb, line 28
def self.setup_currencies
  setup("/currencies")
end
setup_markets() click to toggle source

gets all available markets from stocks.exchange/api2/currencies and sets their respective modules and methods as self.setup_currencies above the markets are then supplimentented with values from ticker and prices

# File lib/stocks_exchange.rb, line 34
def self.setup_markets
  setup("/markets")
  setup_ticker
  setup_prices
end
setup_prices() click to toggle source

add methods from stocks.exchange/api2/prices to respective markets(Modules) created in self.setup_markets above if market does not exist for some reason, it is created

# File lib/stocks_exchange.rb, line 48
def self.setup_prices
  suppliment("/prices")
end
setup_ticker() click to toggle source

add methods from stocks.exchange/api2/ticker to respective markets(Modules) created in self.setup_markets above if market does not exist for some reason, it is created

# File lib/stocks_exchange.rb, line 42
def self.setup_ticker
  suppliment("/ticker")
end

Private Class Methods

setup(path) click to toggle source

setup_currencies setup_markets

# File lib/stocks_exchange.rb, line 56
def self.setup(path)
  get("#@base_uri#{path}").parsed_response.each do |response|
    case path
    when "/currencies"
      module_name = response['currency'].upcase
    when "/markets"
      module_name = "#{response['currency'].upcase}_#{response['partner']}"
    else
      return false
    end
    StocksExchange.const_set(module_name, Module.new{
      response.map{|key,value| define_singleton_method(key){value}}
      })
  end

  return true
end
suppliment(path) click to toggle source

setup_ticker setup_prices

# File lib/stocks_exchange.rb, line 76
def self.suppliment(path)
  get("#@base_uri#{path}").parsed_response.each do |response|
    if StocksExchange.const_defined?(response['market_name'].upcase)
      current_module=StocksExchange.const_get(response['market_name'].upcase)
      response.map{|key,value| current_module.define_singleton_method(key){value}}
      current_module=nil
    else
      StocksExchange.const_set(response['market_name'].upcase, Module.new(response.map{|key,value| define_singleton_method(key){value}}))
    end
  end
  return true
end