tnw_xml_parser 0.0.2 → 0.0.3

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 (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tnw_xml_parser.rb +35 -7
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2953d13535b11d9ac1e4677765b60e8b357c54b1a9f40a1b25b415690110aa41
4
- data.tar.gz: 3ac8ce15587dd02b60a8b601997e0d9f2a59ee9d4600cf1b2fd9271b90c14272
3
+ metadata.gz: b4f1e82b28982fd804c975fa457d157363a40025acc33ece75e1dacb47c8ef51
4
+ data.tar.gz: d0a88e56c3462e379f135766eb6702fc1aa952d9608018112fed76ca0730e3e0
5
5
  SHA512:
6
- metadata.gz: b4526a3174fa0290eee617fa285c00a9020bd5272328ef9a5ba4f056dcd8d1ccb78de24fe111f56e3508a27ec99e8c8845f4802d89de4c2effddc877d9ef755c
7
- data.tar.gz: 3177d4ddaa76018faa44261a3f05bbf785f5bca068c2823ffa1d314b13cf0c5b5ccc2e7e5d7ab2db3cd6cc27513a70bc99754faba40fd445c88b9e22e8027cc8
6
+ metadata.gz: e4b9082c5d1332c80535144ae625924e359bb5ca3e17d11b541e81aea66fc2661309613f6af149fdfb6d39750554639099f0efb4cccd29278b9c9b4fb562d409
7
+ data.tar.gz: ce08bee88b7a1e0202e07ef29e0d34aba41b94bc89bc80848a8e28a6658bab00328f5c7de412731c93e0c0a8096d5b68e3affcc9da9162f9c7ce6e7f3bf4498b
@@ -4,21 +4,49 @@ require "open-uri"
4
4
 
5
5
  class TnwXmlParser
6
6
 
7
- attr_reader :xml_url, :xml_doc
8
-
9
- def self.parse(xml_url)
10
- new(xml_url)
11
- end
7
+ attr_reader :xml_url, :xml_doc, :prefix
12
8
 
13
9
  def initialize(xml_url)
14
10
  validate_xml_url(xml_url)
15
11
 
16
12
  @xml_url = xml_url
17
- @xml_doc = Nokogiri::HTML(open(@xml_url))
13
+ end
14
+
15
+ def read_data
16
+ @xml_doc = Nokogiri::XML(open(@xml_url)) do |config|
17
+ config.strict.noblanks
18
+ end
19
+
20
+ if @xml_doc.css("infoTable").size > 0
21
+ @prefix = ""
22
+ elsif @xml_doc.css("n1:infoTable").size > 0
23
+ @prefix = "n1:"
24
+ else
25
+ raise "infoTable missing from the XML"
26
+ end
27
+
28
+ @xml_doc
18
29
  end
19
30
 
20
31
  def convert_to_hash
21
- # to be implemented
32
+ raise "Please read_data first before converting to json" if xml_doc.nil?
33
+ result = {}
34
+ xml_doc.css("#{prefix}infoTable").each do |info_table|
35
+ cusip = info_table.children.find { |ele| ele.name == "cusip" }.content.upcase
36
+ if result[cusip].nil?
37
+ result[cusip] = {}
38
+ result[cusip]["cusip"] = cusip
39
+ result[cusip]["nameOfIssuer"] = info_table.children.find { |ele| ele.name == "nameOfIssuer" }.content
40
+ result[cusip]["value"] = info_table.children.find { |ele| ele.name == "value" }.content.to_i
41
+ result[cusip]["shrsOrPrnAmt"] = info_table.children.find { |ele| ele.name == "shrsOrPrnAmt" }.children[0].content.to_i
42
+ result[cusip]["shrsOrPrnAmt type"] = info_table.children.find { |ele| ele.name == "shrsOrPrnAmt" }.children[1].content
43
+ result[cusip]["putCall"] = info_table.children.find { |ele| ele.name == "putCall" }&.content
44
+ else
45
+ result[cusip]["value"] += info_table.children.find { |ele| ele.name == "value" }.content.to_i
46
+ result[cusip]["shrsOrPrnAmt"] += info_table.children.find { |ele| ele.name == "shrsOrPrnAmt" }.children[0].content.to_i
47
+ end
48
+ end
49
+ result
22
50
  end
23
51
 
24
52
  private
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tnw_xml_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - T Yang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-13 00:00:00.000000000 Z
11
+ date: 2020-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri