opml-parser 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.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ Gemfile.lock
2
+ rdoc/
3
+ html/
4
+ pkg/
5
+ .bundle
6
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source :rubygems
2
+
3
+ gem 'nokogiri'
4
+
5
+ group :development, :test do
6
+ gem 'rake'
7
+ end
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ Copyright (c) 2013, Kevin Gillieron <kevin.gillieron@gw-computing.net>
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions
6
+ are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+ 2. Redistributions in binary form must reproduce the above copyright
11
+ notice, this list of conditions and the following disclaimer in the
12
+ documentation and/or other materials provided with the distribution.
13
+ 4. Neither the name of the author nor the names of its contributors
14
+ may be used to endorse or promote products derived from this software
15
+ without specific prior written permission.
16
+
17
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
+ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
+ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
+ SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1 @@
1
+ I will add a description and some examples in this page as soon as possible.
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+ require 'rdoc/task'
5
+
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.libs << 'lib' << 'test'
8
+ test.pattern = 'test/**/*_test.rb'
9
+ test.verbose = true
10
+ end
11
+
12
+ Rake::RDocTask.new(:doc) do |rd|
13
+ rd.main = "README.md"
14
+ rd.rdoc_files.include("README.md", "lib/**/*.rb")
15
+ end
@@ -0,0 +1,9 @@
1
+ module OpmlParser #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 1
4
+ MINOR = 0
5
+ TINY = 0
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,68 @@
1
+ require 'nokogiri'
2
+ require 'opml-parser/version'
3
+
4
+ # OpmlParser Module
5
+ #
6
+ # A very simple and easy to use module for importing and exporting OPML.
7
+ #
8
+ # == Credits
9
+ #
10
+ # Kevin Gillieron <kevin.gillieron@gw-computing.net>
11
+ #
12
+ module OpmlParser
13
+ # Class representing an OPML outline element
14
+ class Outline
15
+ # OPML outline attributes (generally text, title, type, xmlUrl
16
+ # and htmlUrl)
17
+ attr_reader :attributes
18
+
19
+ # Initializes an outline object
20
+ #
21
+ # Arguments:
22
+ # attributes: (Hash) A Hash table that contains the attributes of the
23
+ # outline.
24
+ #
25
+ def initialize(attributes=Hash.new)
26
+ @attributes = attributes
27
+ end
28
+ end
29
+
30
+ # Import an OPML String as an array of Outline object
31
+ #
32
+ # Arguments:
33
+ # contents: (String) A String that contains the OMPL
34
+ def import(contents)
35
+ doc = Nokogiri.XML(contents)
36
+
37
+ doc.xpath("//body//outline").inject(Array.new) do |feeds, outline|
38
+ next if outline.attributes.empty?
39
+ feeds << Outline.new(xml_node_to_hash(outline.attributes))
40
+ end
41
+ end
42
+
43
+ # Import an OPML String as an array of Outline object
44
+ #
45
+ # Arguments:
46
+ # feeds: (Array of Outline) An array of Outline objects
47
+ def export(feeds)
48
+ builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
49
+ xml.opml(version: "1.0") do
50
+ xml.head { xml.title "RReader Exported Feeds" }
51
+ xml.body { feeds.each { |outline| xml.outline(outline.attributes) }}
52
+ end
53
+ end
54
+ builder.to_xml
55
+ end
56
+
57
+ private
58
+ # Convert a Nokogiri XML Node to a Hash table
59
+ def xml_node_to_hash(nokogiri_node)
60
+ attributes = Hash.new
61
+
62
+ nokogiri_node.each do |k,v|
63
+ attributes[k.to_sym] = v.value
64
+ end
65
+
66
+ return attributes
67
+ end
68
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'opml-parser/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "opml-parser"
9
+ spec.version = OpmlParser::VERSION::STRING
10
+ spec.authors = ["Kevin Gillieron"]
11
+ spec.email = ["kevin.gillieron@gw-computing.net"]
12
+ spec.description = %q{A simple OPML parser}
13
+ spec.summary = %q{A simple OPML parser}
14
+ spec.homepage = "http://projects.gw-computing.net/projects/opml-parser"
15
+ spec.license = "3-clause BSD"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "nokogiri"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ end
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <opml version="1.0">
3
+ <head>
4
+ <title>Foobar</title>
5
+ </head>
6
+ <body>
7
+ <outline text="foo" title="bar" type="rss" xmlUrl="http://www.gilliek.ch/feeds" htmlUrl="http://www.gilliek.ch"/>
8
+ </body>
9
+ </opml>
@@ -0,0 +1,47 @@
1
+ require 'test/unit'
2
+ require 'opml-parser'
3
+
4
+ include OpmlParser
5
+
6
+ class OpmlParserTest < Test::Unit::TestCase
7
+ def setup
8
+ fin = File.open('test/data/feeds.xml')
9
+
10
+ @opml = fin.readlines.join("")
11
+ @outline = OpmlParser::Outline.new({
12
+ text: "foo",
13
+ title: "bar",
14
+ type: "rss",
15
+ xmlUrl: "http://www.gilliek.ch/feeds",
16
+ htmlUrl: "http://www.gilliek.ch" })
17
+
18
+ fin.close
19
+ end
20
+
21
+ #import an OMPL String as an array of Outline object
22
+ def test_import
23
+ outlines = OpmlParser::import(@opml)
24
+
25
+ assert_equal(outlines.length, 1)
26
+ assert_equal(outlines[0].attributes[:text] , @outline.attributes[:text])
27
+ assert_equal(outlines[0].attributes[:title] , @outline.attributes[:title])
28
+ assert_equal(outlines[0].attributes[:type] , @outline.attributes[:type])
29
+ assert_equal(outlines[0].attributes[:xmlUrl] , @outline.attributes[:xmlUrl])
30
+ assert_equal(outlines[0].attributes[:htmlUrl], @outline.attributes[:htmlUrl])
31
+ end
32
+
33
+ # export an array of Outline object as an OPML String
34
+ def test_export
35
+ ary = Array.new
36
+ ary << @outline
37
+ opml = OpmlParser::export(ary)
38
+ outlines = OpmlParser::import(opml)
39
+
40
+ assert_equal(outlines.length, 1)
41
+ assert_equal(outlines[0].attributes[:text] , @outline.attributes[:text])
42
+ assert_equal(outlines[0].attributes[:title] , @outline.attributes[:title])
43
+ assert_equal(outlines[0].attributes[:type] , @outline.attributes[:type])
44
+ assert_equal(outlines[0].attributes[:xmlUrl] , @outline.attributes[:xmlUrl])
45
+ assert_equal(outlines[0].attributes[:htmlUrl], @outline.attributes[:htmlUrl])
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opml-parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kevin Gillieron
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: A simple OPML parser
63
+ email:
64
+ - kevin.gillieron@gw-computing.net
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE
72
+ - README.md
73
+ - Rakefile
74
+ - lib/opml-parser.rb
75
+ - lib/opml-parser/version.rb
76
+ - opml-parser.gemspec
77
+ - test/data/feeds.xml
78
+ - test/opml_parser_test.rb
79
+ homepage: http://projects.gw-computing.net/projects/opml-parser
80
+ licenses:
81
+ - 3-clause BSD
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.25
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: A simple OPML parser
104
+ test_files: []