Class: ThaiStock

Inherits:
Object
  • Object
show all
Defined in:
lib/thaistock.rb

Class Method Summary collapse

Class Method Details

.check_asset_index(stock) ⇒ Int

check index of asset column

Parameters:

  • object (Nokogiri)

Returns:

  • (Int)

    column of asset



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

generate openable URL for Nokogiri

Parameters:

  • URL's (String)

    postfix

Returns:

  • (String)

    openable URL



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 url with Nokogiri

Parameters:

  • URL (String)

Returns:

  • (Nokogiri)

    object



24
25
26
# File 'lib/thaistock.rb', line 24

def self.open_link(url)
    return Nokogiri::HTML(URI.open("#{url}"))
end

.thai_stockObject

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
            prefix_menu = self.open_link(get_link(prefix_href))
            prefix_menu.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