cstock 0.3.1 → 0.4.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: 1b6ab2e641015052e199ecac1030fbe8784631f1
4
- data.tar.gz: 5be8428f3ac1ec0e319f4efbaddf56272a533759
3
+ metadata.gz: 95101fc84e73758d2e022550e9d91face99c6471
4
+ data.tar.gz: d867d0340c70fcf95ca40a2dc6273418ee8328d8
5
5
  SHA512:
6
- metadata.gz: 3096f87115d89891979da514e5bd3b18af55a2c723961c1808ea2cb5e72458fdb5811d900f23686708559209e49adf1700efef4551df6b107a82ff176722d93a
7
- data.tar.gz: 355bc287cce8c801c7c9f11fc6d6e9f40ae82205a0419b432763f9d2d2df2948c65e956942c3b1e91bce339ba7f9a626d8e3caa410fca5096314035f8249fa30
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 bid_price_1 ask_price_1 volume turnover
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
@@ -19,4 +19,5 @@ Gem::Specification.new do |s|
19
19
 
20
20
  s.add_runtime_dependency 'rest-client', '~> 1.6'
21
21
  s.add_development_dependency 'rspec', '~> 3.2'
22
+ s.add_development_dependency 'byebug'
22
23
  end
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
- def self.refresh(stock)
42
- quote(stock.code) do |data|
43
- FIELDS[1..-1].each_with_index do |field, index|
44
- stock.send("#{field}=".to_sym, (data[index].nil? ? nil : data[index]))
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(stock_code)
51
- parsed_stock_code = parse_stock_code(stock_code)
52
- url = PREFIX_URL + parsed_stock_code
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
- data = parse(response.force_encoding("GBK").encode("UTF-8"))
57
- yield(data) if block_given?
58
- return data
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(data)
71
- data = data.split('=')
72
- if data[1].length < 10
73
- return nil
74
- else
75
- data = data[1].split(',')[0..-2]
76
- data[0] = data[0][1..-1]
77
- return data
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
@@ -1,4 +1,4 @@
1
1
  # =>CStock::VERSION
2
2
  module CStock
3
- VERSION = '0.3.1'
3
+ VERSION = '0.4.0'
4
4
  end
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.3.1
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: