cub 0.0.1 → 0.0.2

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.
data/README.md CHANGED
@@ -21,6 +21,9 @@ Or install it yourself as:
21
21
  Cub.company(7203)
22
22
  => "トヨタ自動車"
23
23
 
24
+ Cub.price(7203)
25
+ => 3275
26
+
24
27
  ## Contributing
25
28
 
26
29
  1. Fork it
data/cub.gemspec CHANGED
@@ -15,5 +15,6 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Cub::VERSION
17
17
 
18
+ gem.add_dependency "nokogiri"
18
19
  gem.add_development_dependency "rspec"
19
20
  end
data/lib/cub.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require File.expand_path(File.dirname(__FILE__) + '/cub/version')
3
3
  require 'yaml'
4
+ require 'open-uri'
5
+ require 'nokogiri'
4
6
 
5
7
  module Cub
6
8
  @@companies = YAML.load_file(File.dirname(__FILE__) + '/../companies.yml')
@@ -16,6 +18,14 @@ module Cub
16
18
  end
17
19
  end
18
20
 
21
+ def self.price(code)
22
+ if valid_code?(code)
23
+ fetch_price(code)
24
+ else
25
+ raise CompanyException, "無効な証券コード"
26
+ end
27
+ end
28
+
19
29
  private
20
30
  def self.valid_code?(code)
21
31
  /^\d{4}$/ =~ code.to_s
@@ -31,4 +41,11 @@ module Cub
31
41
  end
32
42
  end
33
43
 
44
+ def self.fetch_price(code)
45
+ url = "http://stocks.finance.yahoo.co.jp/stocks/detail/?code=#{code}"
46
+ user_agent = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)'
47
+ doc = Nokogiri::HTML(open(url, 'User-Agent' => user_agent).read)
48
+ doc.css('td[class=stoksPrice]').text.gsub(/(\d{0,3}),(\d{3})/, '\1\2').to_i
49
+ end
50
+
34
51
  end
data/lib/cub/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Cub
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/spec/cub_spec.rb CHANGED
@@ -1,26 +1,38 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
3
  require 'cub'
4
- require 'yaml'
5
4
 
6
5
  describe Cub do
7
6
  context '証券コードに対応する会社が存在する場合' do
8
7
  it '会社名を表示' do
9
8
  Cub.company(7203).should eq('トヨタ自動車')
10
9
  end
10
+
11
+ it '株価を表示' do
12
+ Cub.price(7203).should be_within(1000).of(3000)
13
+ end
11
14
  end
12
15
 
13
16
  context '証券コードに対応する会社が存在しない場合' do
14
17
  it '例外を返す' do
15
18
  lambda{ Cub.company(0000) }.should raise_error(Cub::CompanyException)
19
+ lambda{ Cub.price(0000) }.should raise_error(Cub::CompanyException)
16
20
  end
17
21
  end
18
22
 
19
23
  context '証券コードが無効な場合' do
20
- it '例外を返す' do
21
- lambda{ Cub.company(333) }.should raise_error(Cub::CompanyException)
22
- lambda{ Cub.company(55555) }.should raise_error(Cub::CompanyException)
23
- lambda{ Cub.company('abcd') }.should raise_error(Cub::CompanyException)
24
+ describe '例外を返す' do
25
+ it "#company" do
26
+ lambda{ Cub.company(333) }.should raise_error(Cub::CompanyException)
27
+ lambda{ Cub.company(55555) }.should raise_error(Cub::CompanyException)
28
+ lambda{ Cub.company('abcd') }.should raise_error(Cub::CompanyException)
29
+ end
30
+
31
+ it "#price" do
32
+ lambda{ Cub.price(333) }.should raise_error(Cub::CompanyException)
33
+ lambda{ Cub.price(55555) }.should raise_error(Cub::CompanyException)
34
+ lambda{ Cub.price('abcd') }.should raise_error(Cub::CompanyException)
35
+ end
24
36
  end
25
37
  end
26
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-12 00:00:00.000000000 Z
12
+ date: 2012-08-20 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  - !ruby/object:Gem::Dependency
15
31
  name: rspec
16
32
  requirement: !ruby/object:Gem::Requirement
@@ -65,10 +81,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
81
  version: '0'
66
82
  requirements: []
67
83
  rubyforge_project:
68
- rubygems_version: 1.8.24
84
+ rubygems_version: 1.8.23
69
85
  signing_key:
70
86
  specification_version: 3
71
87
  summary: Fetch stock information
72
88
  test_files:
73
89
  - spec/cub_spec.rb
74
90
  - spec/spec_helper.rb
91
+ has_rdoc: