class Avantage::Client

Public Class Methods

new(api_key) click to toggle source
# File lib/avantage.rb, line 15
def initialize(api_key)

  @api_key = api_key
end

Public Instance Methods

currency_exchange_rate(from, to=nil) click to toggle source
# File lib/avantage.rb, line 42
def currency_exchange_rate(from, to=nil)

  os =
    if from.is_a?(Hash)
      from.inject({}) { |k, v|
        if k == :from
          h[:from_currency] = v
        elsif k == :to
          h[:to_currency] = v
        else
          h[k] = v
        end
        h }
    else
      { from_currency: from, to_currency: to }
    end

  get('CURRENCY_EXCHANGE_RATE', os)
end
Also aliased as: exchange_rate, forex
exchange_rate(from, to=nil)
forex(from, to=nil)
get(function, parameters) click to toggle source
# File lib/avantage.rb, line 64
    def get(function, parameters)

      func = function.to_s.upcase

      params = parameters.merge(function: func, apikey: @api_key)
      params[:datatype] = 'json' if params.delete(:json)
      params[:datatype] = 'csv' if params.delete(:csv)

      uri = API_ROOT_URI + URI.encode_www_form(params)

      req = Net::HTTP::Get.new(uri)
      req.instance_eval { @header.clear }
      def req.set_header(k, v); @header[k] = [ v ]; end

      req.set_header('User-Agent', USER_AGENT)

      u = URI(uri)

      t0 = monow

      t = Net::HTTP.new(u.host, u.port)
      t.use_ssl = (u.scheme == 'https')
#t.set_debug_output($stdout)

      res = t.request(req)

      r = res.body
      if res.content_type.match(/\Aapplication\/json\b/)
        r = JSON.parse(r)
      end

      class << r; attr_accessor :_response, :_client, :_elapsed; end
        #
      r._response = res
      r._client = self
      r._elapsed = monow - t0

      r
    end
global_quote(symbol) click to toggle source
# File lib/avantage.rb, line 20
def global_quote(symbol)

  os = symbol.is_a?(Hash) ? symbol : { symbol: symbol }

  get('GLOBAL_QUOTE', os)
end
Also aliased as: quote
quote(symbol)
Alias for: global_quote
sector(os={}) click to toggle source
# File lib/avantage.rb, line 36
def sector(os={})

  get('SECTOR', os)
end
Also aliased as: sectors
sectors(os={})
Alias for: sector

Protected Instance Methods

monow() click to toggle source
# File lib/avantage.rb, line 106
def monow; Process.clock_gettime(Process::CLOCK_MONOTONIC); end