sorry_yahoo_finance 0.1.2 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f1ba7a0570e5c25d5961d8288f8a027bb2b4c772
4
- data.tar.gz: f15cac31e34bdeb0fd2ca96b8cddbd2551065fb8
3
+ metadata.gz: da52115a085f797fc41e72968b09705d3b77d58e
4
+ data.tar.gz: 9ecd2ce04d9e710eb68338af0d8f953da9f29707
5
5
  SHA512:
6
- metadata.gz: ad8dd65368c5a8697488d11ea093765551a86ee747d50e373729d3e06c2cab71e0eb836413f48b9e116f3499db91bdc6443916b76ac796794e5301afecdaeda2
7
- data.tar.gz: f8f454d55303f66bc855be16963df80eecdbcc3ffa52e00182ebcbd5c9865cfc063da51b95ddcef3a683a03c49fafaaac11e57bb6beb4ad9232b7c34123692c9
6
+ metadata.gz: 2d91efca0b5aa34706f8417cb48feaf4b223ee01e780841c14d7266b67ef5147c750de41fed376e90543d2250c5fb1700113dc9a804f48a99a052df756a358b2
7
+ data.tar.gz: 9bc16e66932e277782a5191e7e8bfef9e74761b1ee587b318892700d8c9763a3bafd1ef68d485210615a94536602b61bb77fee9b6752ef5bca0d15646b17b35c
@@ -0,0 +1,12 @@
1
+ module HashAccessor
2
+ def hash_accessor(hash_name, *key_names)
3
+ key_names.each do |key_name|
4
+ define_method key_name do
5
+ instance_variable_get("@#{hash_name}")[key_name]
6
+ end
7
+ define_method "#{key_name}=" do |value|
8
+ instance_variable_get("@#{hash_name}")[key_name] = value
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,132 @@
1
+ require 'all_stock_codes'
2
+ require 'converter'
3
+ require 'hash_accessor'
4
+
5
+ module SorryYahooFinance
6
+ class Hash
7
+ def map_to_hash &block
8
+ ret = {}
9
+ each { |k,v| ret[k] = block.call(k,v) }
10
+ ret
11
+ end
12
+ end
13
+ class GET
14
+ extend HashAccessor
15
+ hash_accessor :values, :code, :name, :market, :industry, :price, :previousprice, :opening, :high, :low, :turnover, :trading_volume, :price_limit, :margin_buying, :margin_selling, :d_margin_buying, :d_margin_selling, :margin_rate, :chart_image
16
+ class << self
17
+ def get_infos(url)
18
+ html = Converter.do(url)
19
+ previousprice, opening, high, low, turnover, trading_volume, price_limit = html.css('div.innerDate dd').map{|x| x.css('strong').inner_text }
20
+ margin_deal = html.css("div.ymuiDotLine div.yjMS dd.ymuiEditLink strong").map(&:text)
21
+ {
22
+ code: html.css("div#divAddPortfolio + dl dt").text,
23
+ name: html.css('table.stocksTable th.symbol h1').inner_text,
24
+ market: html.css('div.stocksDtlWp dd')[0].content,
25
+ industry: html.css("div.stocksDtl dd.category a").text,
26
+ price: html.css('table.stocksTable td.stoksPrice')[1].content,
27
+ previousprice: previousprice,
28
+ opening: opening,
29
+ high: high,
30
+ low: low,
31
+ turnover: turnover,
32
+ trading_volume: trading_volume,
33
+ price_limit: price_limit,
34
+ margin_buying: margin_deal[0],
35
+ margin_selling: margin_deal[3],
36
+ d_margin_buying: margin_deal[1],
37
+ d_margin_selling: margin_deal[4],
38
+ margin_rate: margin_deal[2],
39
+ chart_image: html.css("div.styleChart img")[0][:src],
40
+ }
41
+ end
42
+
43
+ def get_infos_with_date(url)
44
+ html = Converter.do(url)
45
+ tds = html.xpath("(//div[@id='main']//table)[2]//td")
46
+ opening, high, low, finish, turnover = tds[1..5].map(&:text)
47
+ {
48
+ code: html.css("div#divAddPortfolio + dl dt").text,
49
+ name: html.css('table.stocksTable th.symbol h1').inner_text,
50
+ market: html.css('div.stocksDtlWp dd')[0].content,
51
+ industry: html.css("div.stocksDtl dd.category a").text,
52
+ opening: opening,
53
+ high: high,
54
+ low: low,
55
+ finish: finish
56
+ }
57
+ end
58
+
59
+ def get_by_codes(codes)
60
+ if codes.class == Array
61
+ codes.map{|code| self.new(code)}
62
+ else
63
+ raise "codes #{codes} must be a Array."
64
+ end
65
+ end
66
+
67
+ def get_all
68
+ get_from_codes(AllStockCodes::CODES)
69
+ end
70
+ end
71
+
72
+ # 急場の時はアラート出したい
73
+ def initialize(code, date=nil)
74
+ if code.class == Fixnum && code.to_s.size == 4
75
+ begin
76
+ @values = if date
77
+ year, month, day = date.strftime("%Y,%m,%d").split(",")
78
+ month.delete!("0")
79
+ url = "http://info.finance.yahoo.co.jp/history/?code=#{code}.T&sy=#{year}&sm=#{month}&sd=#{day}&ey=#{year}&em=#{month}&ed=#{day}&tm=d"
80
+ self.class.get_infos_with_date(url)
81
+ else
82
+ self.class.get_infos("http://stocks.finance.yahoo.co.jp/stocks/detail/?code=#{code}")
83
+ end
84
+ rescue => ex
85
+ raise "code #{code} stock dont exist. #{ex}"
86
+ end
87
+ else
88
+ raise "code #{code} must be a four-digit number."
89
+ end
90
+ end
91
+
92
+ def values
93
+ @values
94
+ end
95
+
96
+ def to_range
97
+ # str is like 183〜201
98
+ price_limit = @values[:price_limit]
99
+ price_limit.delete!(",")
100
+ price_limit =~ /(\d+)~(\d+)/
101
+ @values[:price_limit] = Range.new($1.to_i,$2.to_i)
102
+ end
103
+
104
+ def formalize_values
105
+ int_keys = [
106
+ :code,
107
+ :price,
108
+ :previousprice,
109
+ :opening,
110
+ :high,
111
+ :low,
112
+ :turnover,
113
+ :trading_volume,
114
+ :margin_buying,
115
+ :margin_selling,
116
+ :d_margin_buying,
117
+ :d_margin_selling,
118
+ :finish
119
+ ]
120
+ @values = @values.map_to_hash do |k,v|
121
+ if int_keys.include?(k) && v.class == String
122
+ v.delete(",").to_i
123
+ else
124
+ v
125
+ end
126
+ end
127
+ to_range
128
+ @values[:margin_rate] = @values[:margin_rate].to_f
129
+ end
130
+ end
131
+ end
132
+ Stock = SorryYahooFinance
@@ -1,3 +1,3 @@
1
1
  module SorryYahooFinance
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,52 +1,9 @@
1
- require 'all_stock_codes'
2
- require 'converter'
3
1
  require "sorry_yahoo_finance/version"
2
+ require "sorry_yahoo_finance/get"
4
3
  module SorryYahooFinance
5
- def get_from_code(code)
6
- if code.class == Fixnum && code.to_s.size == 4
7
- begin
8
- html = Converter.do("http://stocks.finance.yahoo.co.jp/stocks/detail/?code=#{code}")
9
- previousprice, opening, high, low, turnover, trading_volume, price_limit = html.css('div.innerDate dd').map{|x| x.css('strong').inner_text }
10
- margin_deal = html.css("div.ymuiDotLine div.yjMS dd.ymuiEditLink strong").map(&:text)
11
- {
12
- code: code,
13
- name: html.css('table.stocksTable th.symbol h1').inner_text,
14
- market: html.css('div.stocksDtlWp dd')[0].content,
15
- industry: html.css("div.stocksDtl dd.category a").text,
16
- price: html.css('table.stocksTable td.stoksPrice')[1].content,
17
- previousprice: previousprice,
18
- opening: opening,
19
- high: high,
20
- low: low,
21
- turnover: turnover,
22
- trading_volume: trading_volume,
23
- price_limit: price_limit,
24
- margin_buying: margin_deal[0],
25
- margin_selling: margin_deal[3],
26
- d_margin_buying: margin_deal[1],
27
- d_margin_selling: margin_deal[4],
28
- margin_rate: margin_deal[2],
29
- chart_image: html.css("div.styleChart img")[0][:src],
30
- }
31
- rescue
32
- raise "code #{code} stock dont exist."
33
- end
34
- else
35
- raise "code #{code} must be a four-digit number."
4
+ class << self
5
+ def GET(code, date=nil)
6
+ SorryYahooFinance::GET.new(code, date)
36
7
  end
37
8
  end
38
-
39
- def get_from_codes(codes)
40
- if codes.class == Array
41
- codes.map{|code| get_from_code(code)}
42
- else
43
- raise "codes #{codes} must be a Array."
44
- end
45
- end
46
-
47
- def get_all
48
- get_from_codes(AllStockCodes::CODES)
49
- end
50
-
51
- module_function :get_from_code, :get_from_codes, :get_all
52
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sorry_yahoo_finance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - gogotanaka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-14 00:00:00.000000000 Z
11
+ date: 2014-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -113,7 +113,9 @@ files:
113
113
  - bin/sorry_yahoo_finance
114
114
  - lib/all_stock_codes.rb
115
115
  - lib/converter.rb
116
+ - lib/hash_accessor.rb
116
117
  - lib/sorry_yahoo_finance.rb
118
+ - lib/sorry_yahoo_finance/get.rb
117
119
  - lib/sorry_yahoo_finance/version.rb
118
120
  - sorry_yahoo_finance.gemspec
119
121
  - spec/sorry_yahoo_finance_spec.rb