rulethu_stock_exchange 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a0dc271fa578eb41d6b0dd67f8cdc82eddc52a9168e0b8f366e66793ed4304af
4
- data.tar.gz: '09806e50ef484e84efc7f05044844dc260c82e69368a24f8689e838ec909e884'
3
+ metadata.gz: 8e2e09b5387087ac9abfb3fea2dda21cb983328fcefe63233543ba22797ff244
4
+ data.tar.gz: 0a51aca05580cac8ff1f555b0b31de12ad3c8f1526b147a6f66d01530a8fe36b
5
5
  SHA512:
6
- metadata.gz: 66394403e7cb1219331e9231c7060f0b9733a1c16f76d4f11c19d58c44f1e95570227836dd9ed168089a66892633c4e2afccb008d43f0c5ab616a32313c9689e
7
- data.tar.gz: d8c01ffb3e7ebebeb91e31dd36dfd21e93ae29d7fe875f6fb5734a2a9674417558c0861f99fbbc57744969904194e84518be0baa01395937d9df54ff7a427fc0
6
+ metadata.gz: cc72e015dd2e3ca7450be928e323d12e48ded2807767fdbf4ed63ac6036834e6998e5bef1dbc1228d048cb5b3c1175a803101adb29b65bbc7b03e20ed5cae3a6
7
+ data.tar.gz: b8ea28ad59be15ccd1f864e0d6f49b25ec57650a37a5eee2ea08babf110b47d573e2226785b69a16d6c5d30023706c2b374d1d9eb7d2ca95023dee699e80a212
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # RulethuStockExchange
2
2
 
3
3
  This gem allows you to scrape stock exchange data from the following exchanges:
4
- * Zimbabwe Stock Exchange (ZSE)
5
- * Johannesburg Stock Exchange (JSE)
6
- * London Stock Exchange (LSE)
7
4
 
5
+ - Zimbabwe Stock Exchange (ZSE)
6
+ - Johannesburg Stock Exchange (JSE)
7
+ - London Stock Exchange (LSE)
8
8
 
9
9
  ## Installation
10
10
 
@@ -18,8 +18,12 @@ If bundler is not being used to manage dependencies, install the gem by executin
18
18
 
19
19
  ## Usage
20
20
 
21
- ### Example 1: Scraping Data From London Stock Exchange
21
+ ** Make sure you have firefox browser installed **
22
+
23
+ ### Example 1: Scraping Data From London Stock Exchange
24
+
22
25
  Require the gem
26
+
23
27
  ```ruby
24
28
  require 'rulethu_stock_exchange'
25
29
  ```
@@ -29,32 +33,39 @@ Get the stock exchange. (Currently available stock exchanges are ZSE, JSE and LS
29
33
  ```ruby
30
34
  exchange = RulethuStockExchange::Exchanges::LSE
31
35
  ```
36
+
32
37
  Create a Scraper passing in the url for the exchangew
38
+
33
39
  ```ruby
34
40
  scraper = RulethuStockExchange::Scraper.new exchange[:url]
35
41
  ```
36
42
 
37
43
  Scrape the data passing in a selector
44
+
38
45
  ```ruby
39
46
  html = scraper.scrape exchange[:selector]
40
47
  ```
41
48
 
42
49
  Create a parser to parse the data
50
+
43
51
  ```ruby
44
- parser = RulethuStockExchange::Parser.new html
52
+ parser = RulethuStockExchange::Parser.new html
45
53
  ```
46
54
 
47
55
  Parse the data
56
+
48
57
  ```ruby
49
- data = parser.parse()
58
+ data = parser.parse()
50
59
  ```
51
60
 
52
61
  Write the data to JSON file
62
+
53
63
  ```ruby
54
64
  filename = RulethuStockExchange::IO.write_to_json_file data, 'lse'
55
65
  ```
56
66
 
57
67
  Convert the JSON file to CSV
68
+
58
69
  ```ruby
59
70
  RulethuStockExchange::IO.json_to_csv filename
60
71
  ```
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
- require 'json'
3
- require 'csv'
4
- require 'time'
2
+ require "json"
3
+ require "csv"
4
+ require "time"
5
5
 
6
6
  module RulethuStockExchange
7
7
  class IO
@@ -10,8 +10,8 @@ module RulethuStockExchange
10
10
  date = Time.now.strftime("%d-%m-%Y")
11
11
  data_dir = Pathname.new("data") / Pathname.new(prefix)
12
12
  data_dir.mkpath
13
- filename = "#{data_dir}/#{date}-data.json"
14
- File.open(filename, 'w') do |f|
13
+ filename = "#{data_dir}/#{prefix}-#{date}-data.json"
14
+ File.open(filename, "w") do |f|
15
15
  f.write(JSON.dump(data))
16
16
  end
17
17
  filename
@@ -20,13 +20,13 @@ module RulethuStockExchange
20
20
  def self.json_to_csv(json_file)
21
21
  puts "\nConverting #{json_file} to CSV"
22
22
  data = Object.new
23
- File.open(json_file, 'r') do |f|
23
+ File.open(json_file, "r") do |f|
24
24
  data = JSON.parse(f.read)
25
25
  end
26
26
 
27
- filename = json_file.sub(/\.json$/, '.csv')
27
+ filename = json_file.sub(/\.json$/, ".csv")
28
28
 
29
- CSV.open(filename, 'w') do |csv|
29
+ CSV.open(filename, "w") do |csv|
30
30
  csv << data.first.keys
31
31
  data.each do |row|
32
32
  csv << row.values
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- require 'selenium-webdriver'
2
+ require "selenium-webdriver"
3
3
 
4
4
  module RulethuStockExchange
5
5
  class Parser
@@ -10,39 +10,39 @@ module RulethuStockExchange
10
10
  end
11
11
 
12
12
  def parse_zse
13
- tbody = table.find_element(:tag_name, 'tbody')
14
- rows = tbody.find_elements(:tag_name, 'tr')
15
- body_data = []
16
- puts "\nWriting body data"
13
+ tbody = table.find_element(:tag_name, "tbody")
14
+ rows = tbody.find_elements(:tag_name, "tr")
15
+ data_rows = []
17
16
  rows.each do |row|
18
- cells = row.find_elements(:tag_name, 'td')
19
- cells.each do |cell|
20
- body_data << cell.text.strip
21
- end
17
+ text = row.find_elements(:tag_name, "td").first.text.strip
18
+ data_rows << row unless text.empty?
22
19
  end
23
- data = body_data.reject(&:empty?)
24
- header_data = data[1..4].map { |d| d.gsub("\n", " ") }
25
- remaining_data = data[5..]
26
- grouped_data = remaining_data.each_slice(4).to_a
27
- data = grouped_data.reject { |row| row.first.match?(/^[-+]?\d+(\.\d+)?$/) }
28
- data = data[0...-1]
29
- output = []
30
- data.each do |row|
31
- result = {}
32
- row.each_with_index do |value, index|
33
- result[header_data[index]] = value
20
+
21
+ header_data = []
22
+ rows[0].find_elements(:tag_name, "td").each do |td|
23
+ text = td.text.strip.gsub("\n", " ") || "#"
24
+ text = "#" if text.empty?
25
+ header_data << text
26
+ end
27
+
28
+ data = []
29
+ data_rows.each do |row|
30
+ row_data = {}
31
+ cells = row.find_elements :tag_name, "td"
32
+ cells.each_with_index do |cell, index|
33
+ key = header_data[index] || "#"
34
+ row_data[key] = cell.text.strip
34
35
  end
35
- output << result
36
- print "."
36
+ data << row_data
37
37
  end
38
- output
38
+ data
39
39
  end
40
40
 
41
41
  def parse
42
42
  header_data = []
43
43
  begin
44
44
  thead = table.find_element(:tag_name, "thead")
45
- cells = thead.find_element(:tag_name, "tr").find_elements(:tag_name, 'th')
45
+ cells = thead.find_element(:tag_name, "tr").find_elements(:tag_name, "th")
46
46
  puts "Writing header data"
47
47
  cells.each do |cell|
48
48
  header_data << cell.text
@@ -52,12 +52,12 @@ module RulethuStockExchange
52
52
  # Handle missing header gracefully (e.g., log the error)
53
53
  end
54
54
  tbody = table.find_element(:tag_name, "tbody")
55
- rows = tbody.find_elements(:tag_name, 'tr')
55
+ rows = tbody.find_elements(:tag_name, "tr")
56
56
  body_data = []
57
57
  puts "\nWriting body data"
58
58
  rows.each do |row|
59
59
  data = {}
60
- cells = row.find_elements(:tag_name, 'td')
60
+ cells = row.find_elements(:tag_name, "td")
61
61
  if header_data.empty?
62
62
  header_data = cells.map(&:text)
63
63
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RulethuStockExchange
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.7"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rulethu_stock_exchange
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blessed Sibanda
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-11 00:00:00.000000000 Z
11
+ date: 2024-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: selenium-webdriver