cstock 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cc794709608dc917ad3380287b93bdce74aec712
4
- data.tar.gz: d71b2c5bbcd13a1491ac7a4e1d27563f6e5fa10d
3
+ metadata.gz: 50c00672ffb8be8b8471accc764b3b8e228e3cab
4
+ data.tar.gz: fef9b32e38770f86e4ab2c2f4ecc00c62586a91f
5
5
  SHA512:
6
- metadata.gz: 2ec279e4ccb23e359e5859aaf5c8fdb9b42312876141f4ee03c1b7151342a221bc380e9b67dbef542a9abafd4105ecef9531c15552d7984ce8b95fabb82db47d
7
- data.tar.gz: d403dac2e0da301f1c0b9d488e3e236c7c7fd8b610ccdc2e46bb2efd8b6439f645d7c95ef0502074a03e53343f5f04110132336fb3ab62bb3d3d20e017967c9c
6
+ metadata.gz: 9f0eeeab9153902e4e1a7b47ec057aac98791c1182e50b788d761d5b237381db506d5c57c3480c2870a92ac5ef6bc5348eb83b12691c3719b121a4a291146e1b
7
+ data.tar.gz: 59e2854869398db8207dc12f2656e88dbde06f46b78e73785a1234c40bd1ccb90291f54250165f1feac180e1856f456622c0019950940865fc2ad61160f6cf1e
data/README.md CHANGED
@@ -1,4 +1,81 @@
1
1
  cstock
2
2
  ----------------
3
3
 
4
- Query chinese stock infomation.
4
+ Quote chinese stock infomation.
5
+
6
+ It crawl infomation from "hq.sinajs.cn/list=", and include the below info:
7
+
8
+ 股票代码 股票名 开盘价 昨日收盘价 当前价 今日最高 今日最低 买一价 卖一价 成交量 成交额
9
+
10
+ 买一挂单 买一价 买二挂单 买二价 买三挂单 买三价 买四挂单 买四价 买五挂单 买五价
11
+
12
+ 卖一挂单 卖一价 卖二挂单 卖二价 卖三挂单 卖三价 卖四挂单 卖四价 卖五挂单 卖五价
13
+
14
+ 日期 时间
15
+
16
+ **as**
17
+
18
+ code name open_price yesterday_close_price cur_price high_price low_price bid_price_1 ask_price_1 volume turnover
19
+
20
+ 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
21
+
22
+ 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
23
+
24
+ date time
25
+
26
+ # usage
27
+
28
+ ```ruby
29
+ gem install cstock
30
+
31
+ require 'cstock'
32
+
33
+ stock = CStock::Stock.quote('600000')
34
+ stock.name #=> 浦发银行
35
+ stock.股票名 #=> 浦发银行
36
+ stock. open_price #=> 16.00
37
+ ```
38
+
39
+ ## in terminal
40
+
41
+ ```bash
42
+ % cstock 600000
43
+
44
+ 股票代码 : 600000
45
+ 股票名 : 浦发银行
46
+ 开盘价 : 16.00
47
+ 收盘价 : 16.18
48
+ 当前价 : 16.20
49
+ 今日最高 : 16.29
50
+ 今日最低 : 15.93
51
+ 买一价 : 16.20
52
+ 卖一价 : 16.21
53
+ 成交量 : 320361507
54
+ 成交额 : 5171219071
55
+ 买一挂单 : 12400
56
+ 买一价 : 16.20
57
+ 买二挂单 : 532100
58
+ 买二价 : 16.19
59
+ 买三挂单 : 815459
60
+ 买三价 : 16.18
61
+ 买四挂单 : 574500
62
+ 买四价 : 16.17
63
+ 买五挂单 : 358300
64
+ 买五价 : 16.16
65
+ 卖一挂单 : 773560
66
+ 卖一价 : 16.21
67
+ 卖二挂单 : 1226841
68
+ 卖二价 : 16.22
69
+ 卖三挂单 : 1063620
70
+ 卖三价 : 16.23
71
+ 卖四挂单 : 1037567
72
+ 卖四价 : 16.24
73
+ 卖五挂单 : 2292649
74
+ 卖五价 : 16.25
75
+ 日期 : 2015-04-03
76
+ 时间 : 15:04:04
77
+ ```
78
+ # TODO
79
+
80
+ * stock code without 'sh' or 'ss'
81
+ * more indicator
Binary file
@@ -17,5 +17,6 @@ Gem::Specification.new do |s|
17
17
  s.executables = %w(cstock)
18
18
  s.homepage = 'https://github.com/astonfu/cstock'
19
19
 
20
+ s.add_runtime_dependency 'rest-client'
20
21
  s.add_development_dependency 'rspec'
21
22
  end
@@ -1,13 +1,14 @@
1
- require 'net/http'
1
+ require 'rubygems'
2
+ require 'rest-client'
2
3
 
3
4
  module CStock
4
5
  class Stock
5
- FIELDS = %w(name open_price close_price cur_price high_price low_price bid_price_1 ask_price_1 volume turnover
6
+ FIELDS = %w(code name open_price yesterday_close_price cur_price high_price low_price bid_price_1 ask_price_1 volume turnover
6
7
  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
7
8
  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
8
9
  date time)
9
10
 
10
- FIELDS_ZH = %w(股票名 开盘价 收盘价 当前价 今日最高 今日最低 买一价 卖一价 成交量 成交额
11
+ FIELDS_ZH = %w(代码 股票名 开盘价 昨日收盘价 当前价 今日最高 今日最低 买一价 卖一价 成交量 成交额
11
12
  买一挂单 买一价 买二挂单 买二价 买三挂单 买三价 买四挂单 买四价 买五挂单 买五价
12
13
  卖一挂单 卖一价 卖二挂单 卖二价 卖三挂单 卖三价 卖四挂单 卖四价 卖五挂单 卖五价
13
14
  日期 时间)
@@ -17,9 +18,10 @@ module CStock
17
18
  __send__(:alias_method, FIELDS_ZH[index].to_sym, field.to_sym)
18
19
  end
19
20
 
20
- def initialize(data)
21
+ def initialize(stock_code, data)
22
+ @code = stock_code
21
23
  return nil if data == nil
22
- FIELDS.each_with_index do |field, index|
24
+ FIELDS[1..-1].each_with_index do |field, index|
23
25
  instance_variable_set("@#{field}", (data[index].nil? ? nil : data[index]))
24
26
  end
25
27
  end
@@ -30,11 +32,25 @@ module CStock
30
32
  end
31
33
  end
32
34
 
33
- BASE_HOST = "hq.sinajs.cn"
34
- CURRENT_INFO = "/list="
35
+ PREFIX_URL = "http://hq.sinajs.cn/list="
35
36
  def self.quote(stock_code)
36
- data = Net::HTTP.get(BASE_HOST, CURRENT_INFO + stock_code)
37
- Stock.new(parse(data)) if data != nil
37
+ parsed_stock_code = parse_stock_code(stock_code)
38
+ url = PREFIX_URL + parsed_stock_code
39
+
40
+ RestClient::Request.execute(:url => url, :method => :get) do |response|
41
+ if response.code == 200
42
+ s = Stock.new(stock_code, parse(response.force_encoding("GBK").encode("UTF-8")))
43
+ yield(s) if block_given?
44
+ return s
45
+ else
46
+ nil
47
+ end
48
+ end
49
+ end
50
+
51
+ def self.parse_stock_code(stock_code)
52
+ prefix = (stock_code.to_i < 600000) ? "sz" : "sh"
53
+ prefix + stock_code.to_s
38
54
  end
39
55
 
40
56
  def self.parse(data)
@@ -43,7 +59,7 @@ module CStock
43
59
  return nil
44
60
  else
45
61
  data = data[1].split(',')[0..-2]
46
- data[0] = data[0][1..-1].force_encoding("GBK").encode("UTF-8")
62
+ data[0] = data[0][1..-1]
47
63
  return data
48
64
  end
49
65
  end
@@ -1,4 +1,4 @@
1
1
  # =>CStock::VERSION
2
2
  module CStock
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
@@ -3,9 +3,17 @@ require 'spec_helper'
3
3
 
4
4
  describe CStock::Stock do
5
5
  describe 'quote' do
6
+ stock = CStock::Stock.quote('600000') do |s|
7
+ puts "***" + s.name
8
+ end
9
+ stock.description
10
+
6
11
  it "get stock info" do
7
- stock = CStock::Stock.quote('sh600000')
8
12
  expect(stock.name).to eq("浦发银行")
9
13
  end
14
+
15
+ it "set stock code" do
16
+ expect(stock.code).to eq("600000")
17
+ end
10
18
  end
11
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cstock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aston Fu
@@ -10,6 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2015-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rspec
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -35,6 +49,7 @@ files:
35
49
  - LICENSE
36
50
  - README.md
37
51
  - bin/cstock
52
+ - cstock-0.1.0.gem
38
53
  - cstock.gemspec
39
54
  - lib/cstock.rb
40
55
  - lib/cstock/stock.rb