rulethu_stock_exchange 0.1.6 → 0.1.8

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a0dc271fa578eb41d6b0dd67f8cdc82eddc52a9168e0b8f366e66793ed4304af
4
- data.tar.gz: '09806e50ef484e84efc7f05044844dc260c82e69368a24f8689e838ec909e884'
3
+ metadata.gz: 997032ba38a153377cf0576dce101b69ec777e48f58c42e5ec06b7b860222d5b
4
+ data.tar.gz: b1dc52bc0af36edb912a4af09892023c652c47461de0163d3c9c22544ae6fb59
5
5
  SHA512:
6
- metadata.gz: 66394403e7cb1219331e9231c7060f0b9733a1c16f76d4f11c19d58c44f1e95570227836dd9ed168089a66892633c4e2afccb008d43f0c5ab616a32313c9689e
7
- data.tar.gz: d8c01ffb3e7ebebeb91e31dd36dfd21e93ae29d7fe875f6fb5734a2a9674417558c0861f99fbbc57744969904194e84518be0baa01395937d9df54ff7a427fc0
6
+ metadata.gz: 7b91fed4ad022fbfb94b08992a4a62b9a80ba492b22fcee7345d87ca977c4f6def7c124c1799c06f0b956c101ed96591cc0b14e13ef7bcbb63315c77fa5c5360
7
+ data.tar.gz: 8ccb363b6bdec459d6125f6c3c8be366c5ee5ad0a233e5ddad9599b00291fdf615498697382eb3f7b7e1c3e03442f4cb6aac8f7378b3aa4324b0eeb32bfb5572
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,43 @@ 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
17
+ text = row.find_elements(:tag_name, "td").first.text.strip
18
+ unless text&.upcase == "EQUITIES"
19
+ data_rows << row unless text.empty?
21
20
  end
22
21
  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
22
+
23
+ header_data = []
24
+ data_rows.first.find_elements(:tag_name, "td").each do |td|
25
+ text = td.text.strip.gsub("\n", " ") || "#"
26
+ text = "#" if text.empty?
27
+ header_data << text
28
+ end
29
+
30
+ data_rows = data_rows.slice(1, data_rows.length)
31
+
32
+ data = []
33
+ data_rows.each do |row|
34
+ row_data = {}
35
+ cells = row.find_elements :tag_name, "td"
36
+ cells.each_with_index do |cell, index|
37
+ key = header_data[index] || "#"
38
+ row_data[key] = cell.text.strip
34
39
  end
35
- output << result
36
- print "."
40
+ data << row_data
37
41
  end
38
- output
42
+ data
39
43
  end
40
44
 
41
45
  def parse
42
46
  header_data = []
43
47
  begin
44
48
  thead = table.find_element(:tag_name, "thead")
45
- cells = thead.find_element(:tag_name, "tr").find_elements(:tag_name, 'th')
49
+ cells = thead.find_element(:tag_name, "tr").find_elements(:tag_name, "th")
46
50
  puts "Writing header data"
47
51
  cells.each do |cell|
48
52
  header_data << cell.text
@@ -52,12 +56,12 @@ module RulethuStockExchange
52
56
  # Handle missing header gracefully (e.g., log the error)
53
57
  end
54
58
  tbody = table.find_element(:tag_name, "tbody")
55
- rows = tbody.find_elements(:tag_name, 'tr')
59
+ rows = tbody.find_elements(:tag_name, "tr")
56
60
  body_data = []
57
61
  puts "\nWriting body data"
58
62
  rows.each do |row|
59
63
  data = {}
60
- cells = row.find_elements(:tag_name, 'td')
64
+ cells = row.find_elements(:tag_name, "td")
61
65
  if header_data.empty?
62
66
  header_data = cells.map(&:text)
63
67
  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.8"
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.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blessed Sibanda
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-11 00:00:00.000000000 Z
11
+ date: 2024-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: selenium-webdriver
@@ -52,7 +52,7 @@ metadata:
52
52
  homepage_uri: https://github.com/rulethu/rulethu_stock_exchange.git
53
53
  source_code_uri: https://github.com/rulethu/rulethu_stock_exchange.git
54
54
  changelog_uri: https://github.com/rulethu/rulethu_stock_exchange/blob/main/CHANGELOG.md
55
- post_install_message:
55
+ post_install_message:
56
56
  rdoc_options: []
57
57
  require_paths:
58
58
  - lib
@@ -67,8 +67,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  requirements: []
70
- rubygems_version: 3.5.7
71
- signing_key:
70
+ rubygems_version: 3.5.20
71
+ signing_key:
72
72
  specification_version: 4
73
73
  summary: Get Stock Exchange Data
74
74
  test_files: []