class WayToSultan

Public Class Methods

print_asset(source) click to toggle source
stock() click to toggle source
# File lib/way_to_sultan.rb, line 29
def self.stock
  url = 'https://www.set.or.th/set/commonslookup.do?'

  base_url = 'https://www.set.or.th'

  source = Net::HTTP.get(URI.parse(url))

  # 0-9, A, B, C, ..., Y, Z
  index_menu = source.split("\n")

  # find index menu
  i = 0
  index_menu.each_with_index do |line, index|
    if line.include? 'class="col-xs-12 padding-top-10 text-center capital-letter"'
      index_menu[index+1..-1].each do |word|
        w = word.strip
        if w != ""
          index += 1
          i = index
          break
        end
        index += 1
      end
    end
  end

  c = 25
  while c >= 0
    # get suffix link
    index_page = index_menu[i+1].strip.to_s.split('"')[1]
    # go to alphabetic page
    url = base_url + index_page
    source = Net::HTTP.get(URI.parse(url))
    start_at_line = 0
    source = source.split("\n")
    source.each_with_index do |each_line, ind|
      if each_line.include? '<div class="text-right">'
        start_at_line = ind + 1
      end
    end
    source[start_at_line..-1].each_with_index do |each_line, index|
      if each_line.include? '<tr valign="top">'
        w = source[start_at_line + index + 1].strip
        if w == ""
          increment = 2
          while w == ""
            w = source[start_at_line + index + increment].strip
            increment += 1
          end
        end
        abb_company_name = w.split('target="">')[-1].delete_suffix('</a></td>')

        company_page = "https://www.set.or.th/set/companyhighlight.do?symbol=#{abb_company_name}&ssoPageId=5&language=th&country=TH"
        info = Net::HTTP.get(URI.parse(company_page))
        print_asset(info)
      end
    end
    i += 1
    c -= 1
  end
end