sbi-security 0.0.3 → 0.0.4

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: d73397285cf7f9d4161679ef56dd8560f7e54d7e
4
- data.tar.gz: 1ec53a44798377bf9a5cf3ccc8f609860e8609b0
3
+ metadata.gz: 0c1e960daaccd91b38d3c574cba635d29e393737
4
+ data.tar.gz: 0c146bc14278e453047aaa327b3a35b7944364d6
5
5
  SHA512:
6
- metadata.gz: 56e12fdcf31161f47678fd62a6019b241eadd83acce0676264053ab6d8082a35ec336e23422b6ad1b87c9ddd1ecbef1eee953186200d773becc5e19d9bb88cfc
7
- data.tar.gz: 3344d204d76a698457e975d5a18935046b9e4a4e1ba2cd2507a0c38ba54aa1d0275fd13842126a3c276325176c07e075abdf3fcf79eb7d53319851af55515cd4
6
+ metadata.gz: a62fb8304e2319b90f4856fb8e9d7a86afdbd0ebaf5074776a8b2fc58d3de258d58ee9cf4ef875ee1daec9cdc409e1177e786fb7707ae6050757c34dfd6803c9
7
+ data.tar.gz: cefa0c2a8db7d326752c02ef95e5e38ea19f80d058e44e3cf8ff3c2baf23f641f9ac13920c8b5468d9a0c459b0fa3bb91154b1a895d39b1873d2da64de3e345b
data/README.md CHANGED
@@ -26,7 +26,7 @@ Note: You need to set environment variables `SBI_SECURITY_USER_ID` and `SBI_SECU
26
26
 
27
27
  ### Show portfolio
28
28
 
29
- ```bash
29
+ ```
30
30
  $ sbisec portfolio
31
31
 
32
32
  +--------+--------------+------+----------+--------+--------+---------+---------+----------+-----------+
@@ -36,6 +36,18 @@ $ sbisec portfolio
36
36
  +--------+--------------+------+----------+--------+--------+---------+---------+----------+-----------+
37
37
  ```
38
38
 
39
+ ### Show stock
40
+
41
+ $ sbisec stock 3633
42
+
43
+ ```
44
+ +--------+--------------+--------+--------+---------+-------+-------+-------+----------+--------+-----------+
45
+ | コード | 銘柄 | 現在値 | 前日比 | 前日(%) | 始値 | 高値 | 安値 | 前日終値 | 出来高 | 売買代金 |
46
+ +--------+--------------+--------+--------+---------+-------+-------+-------+----------+--------+-----------+
47
+ | 3633 | GMOペパボ | 3,390 | -20 | -0.59 | 3,405 | 3,405 | 3,380 | 3,410 | 500 | 1,696,000 |
48
+ +--------+--------------+--------+--------+---------+-------+-------+-------+----------+--------+-----------+
49
+ ```
50
+
39
51
  ## TODO
40
52
 
41
53
  - Implement `Sbi::Security::Client#buy`
data/bin/sbisec CHANGED
@@ -16,6 +16,14 @@ Commander.configure do
16
16
  puts Sbi::Security::CLI.new(ENV["SBI_SECURITY_USER_ID"], ENV["SBI_SECURITY_PASSWORD"]).portfolio
17
17
  end
18
18
  end
19
+
20
+ command :stock do |c|
21
+ c.syntax = 'sbisec stock [code]'
22
+ c.description = 'Display stock information'
23
+ c.action do |args, options|
24
+ puts Sbi::Security::CLI.new(ENV["SBI_SECURITY_USER_ID"], ENV["SBI_SECURITY_PASSWORD"]).stock(args.first)
25
+ end
26
+ end
19
27
  end
20
28
 
21
29
  exit 0
data/lib/sbi/security.rb CHANGED
@@ -16,7 +16,9 @@ require "sbi/security/crawler"
16
16
  require "sbi/security/formatter"
17
17
  require "sbi/security/decorator"
18
18
  require "sbi/security/decorator/portfolio_decorator"
19
+ require "sbi/security/decorator/portfolio_stock_decorator"
19
20
  require "sbi/security/decorator/stock_decorator"
20
21
  require "sbi/security/portfolio"
22
+ require "sbi/security/portfolio_stock"
21
23
  require "sbi/security/stock"
22
24
  require "sbi/security/version"
@@ -16,5 +16,16 @@ module Sbi::Security
16
16
 
17
17
  Terminal::Table.new(rows: rows)
18
18
  end
19
+
20
+ def stock(code)
21
+ stock = @client.stock(code)
22
+
23
+ rows = []
24
+ rows << stock.decorate.header
25
+ rows << :separator
26
+ rows << stock.decorate.format
27
+
28
+ Terminal::Table.new(rows: rows)
29
+ end
19
30
  end
20
31
  end
@@ -8,6 +8,10 @@ module Sbi::Security
8
8
  crawler.portfolio
9
9
  end
10
10
 
11
+ def stock(code)
12
+ crawler.stock(code)
13
+ end
14
+
11
15
  private
12
16
 
13
17
  def crawler
@@ -19,30 +19,57 @@ module Sbi::Security
19
19
  def portfolio
20
20
  find("img[title='ポートフォリオ']").click
21
21
 
22
- stocks = page.all(:xpath, '//table[@width="100%"]/tbody/tr[@align="center"]').each_with_index.map do |tr, i|
23
- # Ignore title row
24
- next if i == 0
25
-
22
+ stocks = page.all(:xpath, '//table[@width="100%"]/tbody/tr[@align="center"]').drop(1).each_with_index.map do |tr, i|
26
23
  _, code_and_name, _, count, value, price, price_ratio, price_ratio_percentage, profit, profit_percentage,
27
- total_value = tr.all("td").map(&:text)
24
+ total_value = tr.all("td").map { |td| td.text.gsub(/,/, "") }
28
25
 
29
- Stock.new(
26
+ PortfolioStock.new(
30
27
  code: code_and_name.split(" ").first,
31
28
  name: code_and_name.split(" ").last,
32
- count: count.gsub(/,/, ""),
33
- value: value.gsub(/,/, ""),
34
- price: price.gsub(/,/, ""),
35
- price_ratio: (price_ratio == "--" ? nil : price_ratio.gsub(/,/, "").to_i),
36
- price_ratio_percentage: (price_ratio_percentage == "--" ? nil : price_ratio_percentage.gsub(/,/, "").to_f),
37
- profit: profit.gsub(/,/, ""),
38
- profit_percentage: profit_percentage.gsub(/,/, ""),
39
- total_value: total_value.gsub(/,/, "")
29
+ count: count,
30
+ value: value,
31
+ price: price,
32
+ price_ratio: empty_string_to_num(price_ratio).to_i,
33
+ price_ratio_percentage: empty_string_to_num(price_ratio_percentage).to_f,
34
+ profit: profit,
35
+ profit_percentage: profit_percentage,
36
+ total_value: total_value
40
37
  )
41
- end.compact
38
+ end
42
39
 
43
40
  Portfolio.new(stocks)
44
41
  end
45
42
 
43
+ def stock(code)
44
+ fill_in :i_stock_sec, with: code
45
+ find("img[title='株価検索']").click
46
+
47
+ # SBI security has XHR for fetching information. Need to wait until page finish to emulate JavaScript.
48
+ loop do
49
+ if page.find(:xpath, "//td[@id='MTB0_0']/p/em/span[@class='fxx01']").text != "--"
50
+ break
51
+ end
52
+ sleep 1
53
+ end
54
+
55
+ price_ratio, price_ratio_percentage = page.all(:xpath, "//td[@id='MTB0_1']/p/span").map { |td| td.text.gsub(/,/, "") }
56
+ start_price, end_price, highest_price, total_stock, lowest_price, total_price = page.all(:xpath, "//table[@class='tbl690']/tbody/tr/td/p/span[@class='fm01']").map { |td| td.text.gsub(/,/, "") }
57
+
58
+ Stock.new(
59
+ code: code,
60
+ name: page.find(:xpath, "//h3/span[@class='fxx01']").text,
61
+ price: page.find(:xpath, "//td[@id='MTB0_0']/p/em/span[@class='fxx01']").text.gsub(/,/, ""),
62
+ price_ratio: empty_string_to_num(price_ratio).to_i,
63
+ price_ratio_percentage: empty_string_to_num(price_ratio_percentage).to_f,
64
+ start_price: start_price.to_i,
65
+ end_price: end_price,
66
+ highest_price: highest_price.to_i,
67
+ total_stock: total_stock.to_i,
68
+ lowest_price: lowest_price.to_i,
69
+ total_price: total_price.to_i * 1000
70
+ )
71
+ end
72
+
46
73
  private
47
74
 
48
75
  def login(user_id, password)
@@ -51,5 +78,9 @@ module Sbi::Security
51
78
  fill_in :user_password, with: password
52
79
  find_button(class: "ov").click
53
80
  end
81
+
82
+ def empty_string_to_num(string)
83
+ string == "--" ? nil : string
84
+ end
54
85
  end
55
86
  end
@@ -0,0 +1,20 @@
1
+ module Sbi::Security
2
+ class PortfolioStockDecorator < SimpleDelegator
3
+ include Sbi::Security::Formatter
4
+
5
+ def format
6
+ [
7
+ code,
8
+ name,
9
+ currency(count),
10
+ currency(value),
11
+ currency(price),
12
+ sign(price_ratio),
13
+ sign(price_ratio_percentage),
14
+ sign(profit),
15
+ sign(profit_percentage),
16
+ currency(total_value)
17
+ ]
18
+ end
19
+ end
20
+ end
@@ -2,18 +2,23 @@ module Sbi::Security
2
2
  class StockDecorator < SimpleDelegator
3
3
  include Sbi::Security::Formatter
4
4
 
5
+ def header
6
+ %w(コード 銘柄 現在値 前日比 前日(%) 始値 高値 安値 前日終値 出来高 売買代金)
7
+ end
8
+
5
9
  def format
6
10
  [
7
11
  code,
8
12
  name,
9
- currency(count),
10
- currency(value),
11
13
  currency(price),
12
14
  sign(price_ratio),
13
15
  sign(price_ratio_percentage),
14
- sign(profit),
15
- sign(profit_percentage),
16
- currency(total_value)
16
+ currency(start_price),
17
+ currency(highest_price),
18
+ currency(lowest_price),
19
+ currency(end_price),
20
+ currency(total_stock),
21
+ currency(total_price)
17
22
  ]
18
23
  end
19
24
  end
@@ -5,10 +5,10 @@ module Sbi::Security
5
5
  end
6
6
 
7
7
  def sign(number)
8
- if number.to_i == 0
9
- number.to_s
10
- elsif number < 0
8
+ if number < 0
11
9
  currency(number).colorize(:blue)
10
+ elsif number.to_i == 0
11
+ number.to_s
12
12
  else
13
13
  "+#{currency(number)}".colorize(:red)
14
14
  end
@@ -0,0 +1,21 @@
1
+ module Sbi::Security
2
+ class PortfolioStock
3
+ include Decorator
4
+ include Virtus.model(strict: true)
5
+
6
+ attribute :code, Integer
7
+ attribute :name, String
8
+ attribute :count, Integer
9
+ attribute :value, Float
10
+ attribute :price, Integer
11
+
12
+ # Basically, price_ratio is Integer and price_ratio_percentage is Float.
13
+ # Those are nil after 9:00 JST so we can't define type.
14
+ attribute :price_ratio
15
+ attribute :price_ratio_percentage
16
+
17
+ attribute :profit, Integer
18
+ attribute :profit_percentage, Float
19
+ attribute :total_value, Integer
20
+ end
21
+ end
@@ -5,17 +5,17 @@ module Sbi::Security
5
5
 
6
6
  attribute :code, Integer
7
7
  attribute :name, String
8
- attribute :count, Integer
9
- attribute :value, Float
10
8
  attribute :price, Integer
11
9
 
12
10
  # Basically, price_ratio is Integer and price_ratio_percentage is Float.
13
11
  # Those are nil after 9:00 JST so we can't define type.
14
12
  attribute :price_ratio
15
13
  attribute :price_ratio_percentage
16
-
17
- attribute :profit, Integer
18
- attribute :profit_percentage, Float
19
- attribute :total_value, Integer
14
+ attribute :start_price
15
+ attribute :end_price, Integer
16
+ attribute :highest_price
17
+ attribute :total_stock
18
+ attribute :lowest_price
19
+ attribute :total_price
20
20
  end
21
21
  end
@@ -1,5 +1,5 @@
1
1
  module Sbi
2
2
  module Security
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sbi-security
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - camelmasa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-12 00:00:00.000000000 Z
11
+ date: 2017-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capybara
@@ -157,9 +157,11 @@ files:
157
157
  - lib/sbi/security/crawler.rb
158
158
  - lib/sbi/security/decorator.rb
159
159
  - lib/sbi/security/decorator/portfolio_decorator.rb
160
+ - lib/sbi/security/decorator/portfolio_stock_decorator.rb
160
161
  - lib/sbi/security/decorator/stock_decorator.rb
161
162
  - lib/sbi/security/formatter.rb
162
163
  - lib/sbi/security/portfolio.rb
164
+ - lib/sbi/security/portfolio_stock.rb
163
165
  - lib/sbi/security/stock.rb
164
166
  - lib/sbi/security/version.rb
165
167
  - sbi-security.gemspec