lab3 0.0.1

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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/lab3 +4 -0
  3. data/lib/lab3.rb +98 -0
  4. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 006c3a0197c6745d8830ccddd172eed3b07bc9e7b1b45775ac694a8a73e3669b
4
+ data.tar.gz: a40e2cc48d2303458c647220f8ea4205ba6e87ca5a752d88421ce1dedd7878a3
5
+ SHA512:
6
+ metadata.gz: cb7ad41b8259bea967ae4f2b643d47fb4e29a96636a3759e6bca527d4464ae47ec15ba6e9d57efcc4c51d9ea0aa6e2fa3a65afba5573f02d0c3e8aec9505c950
7
+ data.tar.gz: ab6c2a408fc44aaaef706deaae41f20195a889fe5e01fbf4abc9dc8ab9cc943ae60022c2b10c29a351affa7b50be21fed71d4d6e7051988df290d6d0fb9ddb91
@@ -0,0 +1,4 @@
1
+ #!/usr/bim/env ruby
2
+
3
+ require 'lab3'
4
+ puts Scraper.execute
@@ -0,0 +1,98 @@
1
+ # Sahadporn Charnlertlakha
2
+ # 6210545611
3
+
4
+ require 'httparty'
5
+ require 'Nokogiri'
6
+
7
+ # assignment to write a simple ruby script to read and extract current assets of stock
8
+ # Delta in set.or.th
9
+
10
+ # class Scraper
11
+
12
+ # attr_accessor :parse_page
13
+
14
+ # def initialize
15
+ # url = HTTParty.get('https://www.set.or.th/set/companyhighlight.do?symbol=DELTA')
16
+ # @parse_page ||= Nokogiri::HTML(url.body)
17
+ # end
18
+
19
+ # def get_name
20
+ # parse_page.css("h3").text
21
+ # end
22
+
23
+ # def get_asset
24
+ # parse_page.css("td")[6].text
25
+ # end
26
+
27
+ # def self.execute
28
+ # scraper = Scraper.new
29
+ # names = scraper.get_name
30
+ # assets = scraper.get_asset
31
+
32
+ # puts "#{names} : #{assets}"
33
+ # end
34
+ # end
35
+
36
+
37
+ # Instead of printing Delta's information. Your script looks up at
38
+ # https://www.set.or.th/set/commonslookup.do and loop through all
39
+ # stock data and print their assets in stead.
40
+ class Scraper
41
+
42
+ attr_accessor :parse_page
43
+
44
+ def initialize
45
+ url = HTTParty.get('https://www.set.or.th/set/commonslookup.do')
46
+ @parse_page ||= Nokogiri::HTML(url.body)
47
+ end
48
+
49
+ def get_header_link
50
+ header_link = parse_page.css(".col-xs-12").children.map {|link| link['href']}.compact
51
+ end
52
+
53
+ def get_company_link(header_link)
54
+ uri = []
55
+ header_link.each do |link|
56
+ link = "https://www.set.or.th/" + link
57
+ url = HTTParty.get(link)
58
+ second_parse_page ||= Nokogiri::HTML(url.body)
59
+ sec = second_parse_page.css("tr").css("td a[href]")
60
+ sec.each{|link| uri.push(link['href'])}
61
+ end
62
+ return uri
63
+ end
64
+
65
+ def get_names(company_link)
66
+ names = []
67
+ company_link.each do |link|
68
+ link = "https://www.set.or.th/" + link
69
+ url = HTTParty.get(link)
70
+ third_parse_page ||= Nokogiri::HTML(url.body)
71
+ names.push(third_parse_page.css("h3").text)
72
+ end
73
+ return names
74
+ end
75
+
76
+ def get_assets(company_link)
77
+ assets = []
78
+ company_link.each do |link|
79
+ link = "https://www.set.or.th/set/companyhighlight" + link[19...42] + "5&language=th&country=TH"
80
+ url = HTTParty.get(link)
81
+ third_parse_page ||= Nokogiri::HTML(url.body)
82
+ assets.push(third_parse_page.css("tr")[2].css("td")[-2].text)
83
+ end
84
+ return assets
85
+ end
86
+
87
+ def self.execute
88
+ scraper = Scraper.new
89
+ header_link = scraper.get_header_link[2..-1]
90
+ company_link = scraper.get_company_link(header_link)
91
+ names = scraper.get_names(company_link)
92
+ assets = scraper.get_assets(company_link)
93
+
94
+ arr = names.zip(assets)
95
+ arr.each{|element| puts "#{element[0]} : #{element[1]}"}
96
+ end
97
+
98
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lab3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sahadporn Charnlertlakha
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-01-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: lab 3 assignment for software Specification & Design Lab
14
+ email:
15
+ executables:
16
+ - lab3
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/lab3
21
+ - lib/lab3.rb
22
+ homepage: https://rubygems.org/gems/lab3
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.2.3
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: lab3 assignment
45
+ test_files: []