opml-handler 0.1.0 → 1.0.0

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
  SHA1:
3
- metadata.gz: e42cdb96f2c6fcce36f177689e2ce4190b95b01d
4
- data.tar.gz: 707675f61f3bb341c9dc983e820ca3799e893c6c
3
+ metadata.gz: 7a893fe56d0bf2920440dcf6f52014d7bc0f754b
4
+ data.tar.gz: a16826bdedb97c7805770b003f4148b4bd453e45
5
5
  SHA512:
6
- metadata.gz: b560c48dd0eb523f616ad42116d2a1a1d5236209cdeee69cf89639b17c1182af86b7594d35db153781e4e1531d42088d7df04e818180040b17eed070bb4a7656
7
- data.tar.gz: 4c2a4dcaa75b9e3fa52b346197d79b634469c4cec83563aa7670847229050ce7ede3da71ef160aa388d4ac752a00d33bbb9cbcfa3501fbf4ea3636ee6430c4b9
6
+ metadata.gz: a1253344d7ce2aa0f28d3bd929c96726296ca269bb6b38a54d8d7ff0ba7ac3027458bfcb6d07d84db5cb2cb27a17fb623d3f5a889cda724c040ebb13a33aebc8
7
+ data.tar.gz: bb921818be9a5cdd27d51312074d0b287215c6559c8e94052d1f49134996fc45841fc2d9f8558cd14b1ab9916dbb1297aeb3490535aa9c8423f7087b9191bb9d
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Opml::Handler
2
2
 
3
- Handle opml(xml) in your ruby application
3
+ Handle opml(xml) in your ruby application, both import and export and support children outlines
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,10 +20,10 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- Export ruby hash to opml
24
-
23
+ You can convert ruby hash to opml string
25
24
  ```ruby
26
25
  require 'opml-handler'
26
+
27
27
  data = [{text: 'First level',
28
28
  children: [{text: 'Second Level',
29
29
  children: [{text: 'Item 1', _note: "Some notes", children: []},
@@ -31,11 +31,38 @@ data = [{text: 'First level',
31
31
  }
32
32
  ]
33
33
  }]
34
- opml = OpmlHandler::Opml.new('', "title")
34
+
35
+ opml = OpmlHandler::Opml.new(outlines_attributes: data, title: "title")
35
36
 
36
37
  puts opml.to_xml
37
38
  ```
38
39
 
40
+ Or convert opml string to ruby hash
41
+
42
+ ```ruby
43
+ content = '<?xml version="1.0" encoding="UTF-8"?>
44
+ <opml version="1.0">
45
+ <head>
46
+ <title>Title</title>
47
+ </head>
48
+ <body>
49
+ <outline text="First level">
50
+ <outline text="Second Level">
51
+ <outline text="Item 1" _note="Some notes"/>
52
+ <outline text="Item 2"/>
53
+ </outline>
54
+ </outline>
55
+ </body>
56
+ </opml>'
57
+
58
+ opml_object = OpmlHandler::Opml.new(content: content)
59
+
60
+ opml_object.to_hash
61
+
62
+ puts opml_object.title
63
+ puts opml_object.outlines_attributes
64
+ ```
65
+
39
66
  ## Development
40
67
 
41
68
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,3 +1,3 @@
1
1
  module OpmlHandler
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/opml-handler.rb CHANGED
@@ -3,25 +3,55 @@ require 'nokogiri'
3
3
  module OpmlHandler
4
4
 
5
5
  class Opml
6
- attr_reader :title
6
+ attr_reader :title, :content, :outlines_attributes
7
7
 
8
- def initialize(outline_data=[], title='')
9
- @outline_data = outline_data
10
- @title = title
8
+ def initialize(params={})
9
+ @content = params[:content] || ""
10
+ @outlines_attributes = params[:outlines_attributes] || []
11
+ @title = params[:title] || "no title"
11
12
  end
12
13
 
13
14
  def outlines
14
- @outline_data.map { |data| OpmlHandler::Outline.new(data) }
15
+ outlines_attributes.map { |data| OpmlHandler::Outline.new(data) }
15
16
  end
16
17
 
17
18
  def to_xml
18
- builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
19
- xml.opml(version: "1.0") do
20
- xml.head { xml.title title }
21
- xml.body { outlines.map { |outline| outline.to_xml(xml) } }
19
+ @content = begin
20
+ builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
21
+ xml.opml(version: "1.0") do
22
+ xml.head { xml.title title }
23
+ xml.body { outlines.map { |outline| outline.to_xml(xml) } }
24
+ end
25
+ end
26
+ builder.to_xml
27
+ end
28
+ end
29
+
30
+ def to_hash
31
+ doc = Nokogiri.XML(content)
32
+ @title = doc.xpath("//head//title").children.first.text
33
+ @outlines_attributes = nodes_to_hash(doc.xpath("//body//outline"))
34
+ end
35
+
36
+ def nodes_to_hash(nodes)
37
+ [].tap do |outlines_attributes|
38
+ nodes.each do |node|
39
+ outline_attributes = node_to_hash(node)
40
+ if outline_attributes && node.children
41
+ outline_attributes[:children] = nodes_to_hash(node.children)
42
+ end
43
+ outlines_attributes << outline_attributes
44
+ end
45
+ end.compact
46
+ end
47
+
48
+ def node_to_hash(node)
49
+ return nil if node.attributes.empty?
50
+ {}.tap do |attributes|
51
+ node.attributes.each do |k, v|
52
+ attributes[k.to_sym] = v.value
22
53
  end
23
54
  end
24
- builder.to_xml
25
55
  end
26
56
  end
27
57
 
@@ -39,4 +69,4 @@ module OpmlHandler
39
69
  end
40
70
  end
41
71
 
42
- end
72
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opml-handler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - hang