cstock 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/cstock.gemspec +1 -0
- data/lib/cstock/stock.rb +40 -19
- data/lib/cstock/version.rb +1 -1
- data/spec/cstock_spec.rb +15 -0
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 95101fc84e73758d2e022550e9d91face99c6471
|
4
|
+
data.tar.gz: d867d0340c70fcf95ca40a2dc6273418ee8328d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f56da5d58e739a88d19e3921d54f95d59a61dafd31f3db1aa4aa4536375ae424cb27457316fc32bff09ed9146a8d1965be755ac4c93722a923dd039f81486ad
|
7
|
+
data.tar.gz: ed169c60b6f3d127bbc4ddfd23dc07b35b3bb69e1a7b026eae80fe6f58e95619f9f2c9dfc592b0838fab0a358efaf645b89c42849f98667b805803b552402f33
|
data/README.md
CHANGED
@@ -15,7 +15,7 @@ It crawl infomation from "hq.sinajs.cn/list=", and include the below info:
|
|
15
15
|
|
16
16
|
**as**
|
17
17
|
|
18
|
-
code name open_price yesterday_close_price cur_price high_price low_price
|
18
|
+
code name open_price yesterday_close_price cur_price high_price low_price bid_price ask_price volume turnover
|
19
19
|
|
20
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
21
|
|
data/cstock.gemspec
CHANGED
data/lib/cstock/stock.rb
CHANGED
@@ -20,7 +20,7 @@ module CStock
|
|
20
20
|
|
21
21
|
def initialize(stock_code, data=nil)
|
22
22
|
@code = stock_code
|
23
|
-
data = self.class.quote(stock_code) if data == nil
|
23
|
+
data = self.class.quote(stock_code)[0] if data == nil
|
24
24
|
FIELDS[1..-1].each_with_index do |field, index|
|
25
25
|
instance_variable_set("@#{field}", (data[index].nil? ? nil : data[index]))
|
26
26
|
end
|
@@ -38,24 +38,41 @@ module CStock
|
|
38
38
|
self.class.refresh(self)
|
39
39
|
end
|
40
40
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
41
|
+
##
|
42
|
+
# with the given data to set the stock field values except the fires filed -- code.
|
43
|
+
def set_fields(data)
|
44
|
+
FIELDS[1..-1].each_with_index do |field, index|
|
45
|
+
send("#{field}=".to_sym, (data[index].nil? ? nil : data[index]))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.refresh(stocks)
|
50
|
+
stocks = [stocks] if not stocks.respond_to?(:each)
|
51
|
+
stock_codes = stocks.map(&:code)
|
52
|
+
# one quote return muti stocks data. So it saves time.
|
53
|
+
quote(stock_codes) do |datas|
|
54
|
+
datas.each_with_index do |data, index|
|
55
|
+
stocks[index].set_fields data
|
45
56
|
end
|
46
57
|
end
|
47
58
|
end
|
48
59
|
|
49
60
|
PREFIX_URL = "http://hq.sinajs.cn/list="
|
50
|
-
def self.quote(
|
51
|
-
|
52
|
-
|
61
|
+
def self.quote(stock_codes)
|
62
|
+
parsed_stock_codes_str = ''
|
63
|
+
stock_codes = [stock_codes] if not stock_codes.respond_to?(:each)
|
64
|
+
|
65
|
+
stock_codes.each do |stock_code|
|
66
|
+
parsed_stock_codes_str += "#{parse_stock_code(stock_code)},"
|
67
|
+
end
|
68
|
+
|
69
|
+
url = PREFIX_URL + parsed_stock_codes_str
|
53
70
|
|
54
71
|
RestClient::Request.execute(:url => url, :method => :get) do |response|
|
55
72
|
if response.code == 200
|
56
|
-
|
57
|
-
yield(
|
58
|
-
return
|
73
|
+
datas = parse(response.force_encoding("GBK").encode("UTF-8").strip!)
|
74
|
+
yield(datas) if block_given?
|
75
|
+
return datas
|
59
76
|
else
|
60
77
|
nil
|
61
78
|
end
|
@@ -67,14 +84,18 @@ module CStock
|
|
67
84
|
prefix + stock_code.to_s
|
68
85
|
end
|
69
86
|
|
70
|
-
def self.parse(
|
71
|
-
|
72
|
-
|
73
|
-
return nil
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
87
|
+
def self.parse(datas)
|
88
|
+
return nil if datas.nil?
|
89
|
+
datas = datas.split(';').map do |data|
|
90
|
+
return nil if data.nil?
|
91
|
+
data = data.split('=')
|
92
|
+
if data[1].length < 10
|
93
|
+
nil
|
94
|
+
else
|
95
|
+
data = data[1].split(',')[0..-2]
|
96
|
+
data[0] = data[0][1..-1] # fix name string
|
97
|
+
data
|
98
|
+
end
|
78
99
|
end
|
79
100
|
end
|
80
101
|
end
|
data/lib/cstock/version.rb
CHANGED
data/spec/cstock_spec.rb
CHANGED
@@ -23,6 +23,13 @@ describe CStock::Stock do
|
|
23
23
|
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
|
24
24
|
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
|
25
25
|
date time))
|
26
|
+
|
27
|
+
@s2 = CStock::Stock.new('002385', %w(name open_price yesterday_close_price cur_price high_price low_price bid_price_1 ask_price_1 volume turnover
|
28
|
+
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
|
29
|
+
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
|
30
|
+
date time))
|
31
|
+
|
32
|
+
@stocks = [@s, @s2]
|
26
33
|
end
|
27
34
|
|
28
35
|
it "class method update stock attr" do
|
@@ -36,5 +43,13 @@ describe CStock::Stock do
|
|
36
43
|
@s.refresh
|
37
44
|
expect(@s.name).to eq('浦发银行')
|
38
45
|
end
|
46
|
+
|
47
|
+
it "refresh muti stocks at one quote" do
|
48
|
+
expect(@s.name).to eq("name")
|
49
|
+
expect(@s2.name).to eq("name")
|
50
|
+
CStock::Stock.refresh(@stocks)
|
51
|
+
expect(@s.name).to eq("浦发银行")
|
52
|
+
expect(@s2.name).to eq("大北农")
|
53
|
+
end
|
39
54
|
end
|
40
55
|
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.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aston Fu
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '3.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: byebug
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
description: A ruby gem that get chinese stock infomation.
|
42
56
|
email: im@fuhao.im
|
43
57
|
executables:
|