Class: CStock::Stock
- Inherits:
-
Object
- Object
- CStock::Stock
- Defined in:
- lib/cstock/stock.rb
Constant Summary collapse
- FIELDS =
%w(code name open_price yesterday_close_price current_price high_price low_price bid_price ask_price volume turnover bid_volume_1 bid_price_1 bid_volume_2 bid_price_2 bid_volume_3 bid_price_3 bid_volume_4 bid_price_4 bid_volume_5 bid_price_5 ask_volume_1 ask_price_1 ask_volume_2 ask_price_2 ask_volume_3 ask_price_3 ask_volume_4 ask_price_4 ask_volume_5 ask_price_5 date time)
- FIELDS_ZH =
%w(代码 股票名 开盘价 昨日收盘价 当前价 今日最高 今日最低 买一价 卖一价 成交量 成交额 买一挂单 买一价 买二挂单 买二价 买三挂单 买三价 买四挂单 买四价 买五挂单 买五价 卖一挂单 卖一价 卖二挂单 卖二价 卖三挂单 卖三价 卖四挂单 卖四价 卖五挂单 卖五价 日期 时间)
- PREFIX_URL =
"http://hq.sinajs.cn/list="
Class Method Summary collapse
- .exists?(stock_code) ⇒ Boolean
- .parse(datas) ⇒ Object
- .parse_stock_code(stock_code) ⇒ Object
- .quote(stock_codes) ⇒ Object
-
.refresh(stocks) ⇒ Object
block can yield after each stock refresh.
Instance Method Summary collapse
- #description ⇒ Object
-
#initialize(stock_code, data = nil) {|_self| ... } ⇒ Stock
constructor
A new instance of Stock.
- #refresh ⇒ Object
-
#set_fields(data) ⇒ Object
with the given data to set the stock field values except the fires filed – code.
Constructor Details
#initialize(stock_code, data = nil) {|_self| ... } ⇒ Stock
Returns a new instance of Stock.
21 22 23 24 25 26 27 28 |
# 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 |
Class Method Details
.exists?(stock_code) ⇒ Boolean
108 109 110 |
# File 'lib/cstock/stock.rb', line 108 def self.exists?(stock_code) quote(stock_code)[0] ? true : false end |
.parse(datas) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# 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) ⇒ Object
87 88 89 90 91 |
# 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) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# 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) ⇒ Object
block can yield after each stock refresh
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# 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 |
Instance Method Details
#description ⇒ Object
31 32 33 34 35 |
# File 'lib/cstock/stock.rb', line 31 def description FIELDS_ZH.each do |field| puts field + " : " + self.send(field).to_s end end |
#refresh ⇒ Object
37 38 39 |
# File 'lib/cstock/stock.rb', line 37 def refresh self.class.refresh(self) end |
#set_fields(data) ⇒ Object
with the given data to set the stock field values except the fires filed – code.
43 44 45 46 47 |
# 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 |