Class: ThaiStock
- Inherits:
-
Object
- Object
- ThaiStock
- Defined in:
- lib/thaistock.rb
Class Method Summary collapse
-
.check_asset_index(stock) ⇒ Int
check index of asset column.
-
.get_link(url) ⇒ String
generate openable URL for Nokogiri.
-
.open_link(url) ⇒ Nokogiri
open url with Nokogiri.
-
.thai_stock ⇒ Object
Excucute the program.
Class Method Details
.check_asset_index(stock) ⇒ Int
check index of asset column
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/thaistock.rb', line 33 def self.check_asset_index(stock) index = 1 begin while true stock_detail = stock.css("th[#{index}]").first.text if stock_detail.to_s.include? "ไตรมาส" break else index += 1 end end rescue NoMethodError index = 10 end return index end |
.get_link(url) ⇒ String
generate openable URL for Nokogiri
14 15 16 17 |
# File 'lib/thaistock.rb', line 14 def self.get_link(url) prefix = "https://www.set.or.th" return prefix + url.values[0] end |
.open_link(url) ⇒ Nokogiri
open url with Nokogiri
24 25 26 |
# File 'lib/thaistock.rb', line 24 def self.open_link(url) return Nokogiri::HTML(URI.open("#{url}")) end |
.thai_stock ⇒ Object
Excucute the program
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/thaistock.rb', line 52 def self.thai_stock url = "https://www.set.or.th/set/commonslookup.do" source = self.open_link(url) source.search("a").map{ |prefix_href| if prefix_href.to_s.include? "prefix=" # open each catagories = self.open_link(get_link(prefix_href)) .search("td a").map{ |stock| # open each stock in the following catagories stock_info = self.open_link(get_link(stock)) # print company's name company_name = stock_info.css("h3").text print company_name print " : " stock_info.search("a").map{ |stock_asset| if stock_asset.to_s.include? "companyhighlight" # open each stock detail current_stock_asset = self.open_link(get_link(stock_asset)) index = self.check_asset_index(current_stock_asset) begin # print company asset company_asset = current_stock_asset.css("td[#{index}]").first.text puts company_asset rescue NoMethodError puts "NO DATA FOUND" end end } } end } end |