class WayToSultan
Public Class Methods
print_asset(source)
click to toggle source
# File lib/way_to_sultan.rb, line 8 def self.print_asset(source) elements = source.split("\n") company, assets = '' elements.each_with_index do |each_element, index| line = each_element.strip if line.include? '<div class="col-xs-12 col-md-12 col-lg-8">' company = elements[index + 1].strip.to_s company.delete_prefix!('<h3>').delete_suffix!('</h3>') end if (line.force_encoding('UTF-8').include? 'Assets') || (line.force_encoding('UTF-8').include? 'สินทรัพย์รวม') assets = elements[index + 4].strip.to_s assets.slice!(assets.index('&')..-1) assets.delete_prefix!('<td >') end end puts "#{company.force_encoding('UTF-8')} : #{assets.force_encoding('UTF-8')}" end
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