class CStock::Stock

Constants

FIELDS
FIELDS_ZH
PREFIX_URL

Public Class Methods

exists?(stock_code) click to toggle source
# File lib/cstock/stock.rb, line 108
def self.exists?(stock_code)
  quote(stock_code)[0] ? true : false
end
new(stock_code, data=nil) { |self| ... } click to toggle source
# File lib/cstock/stock.rb, line 21
def initialize(stock_code, data=nil)
  @code = stock_code
  data = self.class.quote(stock_code)[0] if data == nil
  FIELDS[1..-1].each_with_index do |field, index|
    instance_variable_set("@#{field}", ((data.nil? or data[index].nil?) ? nil : data[index]))
  end
  yield self if block_given?
end
parse(datas) click to toggle source
# File lib/cstock/stock.rb, line 93
def self.parse(datas)
  return nil if datas.nil?
  datas = datas.split(';').map do |data|
    return nil if data.nil?
    data = data.split('=')
    if data[1].length < 10
      nil
    else
      data = data[1].split(',')[0..-2]
      data[0] = data[0][1..-1]  # fix name string
      data
    end
  end
end
parse_stock_code(stock_code) click to toggle source
# File lib/cstock/stock.rb, line 87
def self.parse_stock_code(stock_code)
  return "sz0" if /^\d{6}$/.match(stock_code).nil?  # so it can continue quote and return nil
  prefix = (stock_code.to_i < 600000) ? "sz" : "sh"
  prefix + stock_code.to_s
end
quote(stock_codes) { |datas| ... } click to toggle source
# File lib/cstock/stock.rb, line 66
def self.quote(stock_codes)
  parsed_stock_codes_str = ''
  stock_codes = [stock_codes] if not stock_codes.respond_to?(:each)

  stock_codes.each do |stock_code|
    parsed_stock_codes_str += "#{parse_stock_code(stock_code)},"
  end

  url = PREFIX_URL + parsed_stock_codes_str

  RestClient::Request.execute(:url => url, :method => :get) do |response|
    if response.code == 200
      datas = parse(response.force_encoding("GBK").encode("UTF-8").strip!)
      yield(datas) if block_given?
      return datas
    else
      nil
    end
  end
end
refresh(stocks) { |stock| ... } click to toggle source

block can yield after each stock refresh

# File lib/cstock/stock.rb, line 51
def self.refresh(stocks)
  stocks = [stocks] if not stocks.respond_to?(:each)
  stock_codes = stocks.map(&:code)
  # one quote return muti stocks data. So it saves time.
  quote(stock_codes) do |datas|
    datas.each_with_index do |data, index|
      stock = stocks[index]
      stock.set_fields data
      yield stock if block_given?
    end
  end
  stocks
end

Public Instance Methods

description() click to toggle source
# File lib/cstock/stock.rb, line 31
def description
  FIELDS_ZH.each do |field|
    puts field + " : " + self.send(field).to_s
  end
end
refresh() click to toggle source
# File lib/cstock/stock.rb, line 37
def refresh
  self.class.refresh(self)
end
set_fields(data) click to toggle source

with the given data to set the stock field values except the fires filed – code.

# File lib/cstock/stock.rb, line 43
def set_fields(data)
  FIELDS[1..-1].each_with_index do |field, index|
    self.send("#{field}=".to_sym, ((data.nil? or data[index].nil?) ? nil : data[index]))
  end
end