fed 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) Tom Preston-Werner
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,69 @@
1
+ RakeGem
2
+ =======
3
+
4
+ # DESCRIPTION
5
+
6
+ Ever wanted to manage your RubyGem in a sane way without having to resort to
7
+ external dependencies like Jeweler or Hoe? Ever thought that Rake and a hand
8
+ crafted gemspec should be enough to deal with these problems? If so, then
9
+ RakeGem is here to make your life awesome!
10
+
11
+ RakeGem is not a library. It is just a few simple file templates that you can
12
+ copy into your project and easily customize to match your specific needs. It
13
+ ships with a few Rake tasks to help you keep your gemspec up-to-date, build
14
+ a gem, and release your library and gem to the world.
15
+
16
+ RakeGem assumes you are using Git. This makes the Rake tasks easy to write. If
17
+ you are using something else, you should be able to get RakeGem up and running
18
+ with your system without too much editing.
19
+
20
+ The RakeGem tasks were inspired by the
21
+ [Sinatra](http://github.com/sinatra/sinatra) project.
22
+
23
+ # INSTALLATION
24
+
25
+ Take a look at `Rakefile` and `NAME.gemspec`. For new projects, you can start
26
+ with these files and edit a few lines to make them fit into your library. If
27
+ you have an existing project, you'll probably want to take the RakeGem
28
+ versions and copy any custom stuff from your existing Rakefile and gemspec
29
+ into them. As long as you're careful, the rake tasks should keep working.
30
+
31
+ # ASSUMPTIONS
32
+
33
+ RakeGem makes a few assumptions. You will either need to satisfy these
34
+ assumptions or modify the rake tasks to work with your setup.
35
+
36
+ You should have a file named `lib/NAME.rb` (where NAME is the name of your
37
+ library) that contains a version line. It should look something like this:
38
+
39
+ module NAME
40
+ VERSION = '0.1.0'
41
+ end
42
+
43
+ It is important that you use the constant `VERSION` and that it appear on a
44
+ line by itself.
45
+
46
+ # UPDATING THE VERSION
47
+
48
+ In order to make a new release, you'll want to update the version. With
49
+ RakeGem, you only need to do that in the `lib/NAME.rb` file. Everything else
50
+ will use this find the canonical version of the library.
51
+
52
+ # TASKS
53
+
54
+ RakeGem provides three rake tasks:
55
+
56
+ `rake gemspec` will update your gemspec with the latest version (taken from
57
+ the `lib/NAME.rb` file) and file list (as reported by `git ls-files`).
58
+
59
+ `rake build` will update your gemspec, build your gemspec into a gem, and
60
+ place it in the `pkg` directory.
61
+
62
+ `rake release` will update your gemspec, build your gem, make a commit with
63
+ the message `Release 0.1.0` (with the correct version, obviously), tag the
64
+ commit with `v0.1.0` (again with the correct version), and push the `master`
65
+ branch and new tag to `origin`.
66
+
67
+ Keep in mind that these are just simple Rake tasks and you can edit them
68
+ however you please. Don't want to auto-commit or auto-push? Just delete those
69
+ lines. You can bend RakeGem to your own needs. That's the whole point!
@@ -0,0 +1,150 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def version
16
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
17
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
18
+ end
19
+
20
+ def date
21
+ Date.today.to_s
22
+ end
23
+
24
+ def rubyforge_project
25
+ name
26
+ end
27
+
28
+ def gemspec_file
29
+ "#{name}.gemspec"
30
+ end
31
+
32
+ def gem_file
33
+ "#{name}-#{version}.gem"
34
+ end
35
+
36
+ def replace_header(head, header_name)
37
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
38
+ end
39
+
40
+ #############################################################################
41
+ #
42
+ # Standard tasks
43
+ #
44
+ #############################################################################
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/testtask'
49
+ Rake::TestTask.new(:test) do |test|
50
+ test.libs << 'lib' << 'test'
51
+ test.pattern = 'test/**/test_*.rb'
52
+ test.verbose = true
53
+ end
54
+
55
+ desc "Generate RCov test coverage and open in your browser"
56
+ task :coverage do
57
+ require 'rcov'
58
+ sh "rm -fr coverage"
59
+ sh "rcov test/test_*.rb"
60
+ sh "open coverage/index.html"
61
+ end
62
+
63
+ require 'rdoc/task'
64
+ Rake::RDocTask.new do |rdoc|
65
+ rdoc.rdoc_dir = 'rdoc'
66
+ rdoc.title = "#{name} #{version}"
67
+ rdoc.rdoc_files.include('README*')
68
+ rdoc.rdoc_files.include('lib/**/*.rb')
69
+ end
70
+
71
+ desc "Open an irb session preloaded with this library"
72
+ task :console do
73
+ sh "irb -rubygems -r ./lib/#{name}.rb"
74
+ end
75
+
76
+ #############################################################################
77
+ #
78
+ # Custom tasks (add your own tasks here)
79
+ #
80
+ #############################################################################
81
+
82
+
83
+
84
+ #############################################################################
85
+ #
86
+ # Packaging tasks
87
+ #
88
+ #############################################################################
89
+
90
+ desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
91
+ task :release => :build do
92
+ unless `git branch` =~ /^\* master$/
93
+ puts "You must be on the master branch to release!"
94
+ exit!
95
+ end
96
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
97
+ sh "git tag v#{version}"
98
+ sh "git push origin master"
99
+ sh "git push origin v#{version}"
100
+ sh "gem push pkg/#{name}-#{version}.gem"
101
+ end
102
+
103
+ desc "Build #{gem_file} into the pkg directory"
104
+ task :build => :gemspec do
105
+ sh "mkdir -p pkg"
106
+ sh "gem build #{gemspec_file}"
107
+ sh "mv #{gem_file} pkg"
108
+ end
109
+
110
+ desc "Generate #{gemspec_file}"
111
+ task :gemspec => :validate do
112
+ # read spec file and split out manifest section
113
+ spec = File.read(gemspec_file)
114
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
115
+
116
+ # replace name version and date
117
+ replace_header(head, :name)
118
+ replace_header(head, :version)
119
+ replace_header(head, :date)
120
+ #comment this out if your rubyforge_project has a different name
121
+ replace_header(head, :rubyforge_project)
122
+
123
+ # determine file list from git ls-files
124
+ files = `git ls-files`.
125
+ split("\n").
126
+ sort.
127
+ reject { |file| file =~ /^\./ }.
128
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
129
+ map { |file| " #{file}" }.
130
+ join("\n")
131
+
132
+ # piece file back together and write
133
+ manifest = " s.files = %w[\n#{files}\n ]\n"
134
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
135
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
136
+ puts "Updated #{gemspec_file}"
137
+ end
138
+
139
+ desc "Validate #{gemspec_file}"
140
+ task :validate do
141
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
142
+ unless libfiles.empty?
143
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
144
+ exit!
145
+ end
146
+ unless Dir['VERSION*'].empty?
147
+ puts "A `VERSION` file at root level violates Gem best practices."
148
+ exit!
149
+ end
150
+ end
Binary file
@@ -0,0 +1,73 @@
1
+ ## This is the rakegem gemspec template. Make sure you read and understand
2
+ ## all of the comments. Some sections require modification, and others can
3
+ ## be deleted if you don't need them. Once you understand the contents of
4
+ ## this file, feel free to delete any comments that begin with two hash marks.
5
+ ## You can find comprehensive Gem::Specification documentation, at
6
+ ## http://docs.rubygems.org/read/chapter/20
7
+ Gem::Specification.new do |s|
8
+ s.specification_version = 2 if s.respond_to? :specification_version=
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.rubygems_version = '1.3.5'
11
+
12
+ ## Leave these as is they will be modified for you by the rake gemspec task.
13
+ ## If your rubyforge_project name is different, then edit it and comment out
14
+ ## the sub! line in the Rakefile
15
+ s.name = 'fed'
16
+ s.version = '0.0.1'
17
+ s.date = '2013-04-25'
18
+
19
+ ## Make sure your summary is short. The description may be as long
20
+ ## as you like.
21
+ s.summary = "A feed parser that actually works."
22
+ s.description = "A feed parser that actually works and doesn't suck."
23
+
24
+ ## List the primary authors. If there are a bunch of authors, it's probably
25
+ ## better to set the email to an email list or something. If you don't have
26
+ ## a custom homepage, consider using your GitHub URL or the like.
27
+ s.authors = ["Jeremy McAnally"]
28
+ s.email = 'jeremy@github.com'
29
+ s.homepage = 'http://github.com/jm/fed'
30
+
31
+ ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
32
+ ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
33
+ s.require_paths = %w[lib]
34
+
35
+ ## Specify any RDoc options here. You'll want to add your README and
36
+ ## LICENSE files to the extra_rdoc_files list.
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.extra_rdoc_files = %w[README.md LICENSE]
39
+
40
+ ## List your runtime dependencies here. Runtime dependencies are those
41
+ ## that are needed for an end user to actually USE your code.
42
+ s.add_dependency('nokogiri')
43
+ s.add_dependency('curb')
44
+
45
+ ## List your development dependencies here. Development dependencies are
46
+ ## those that are only needed during development
47
+ s.add_development_dependency('mocha')
48
+ s.add_development_dependency('contest')
49
+
50
+ ## Leave this section as-is. It will be automatically generated from the
51
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
52
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
53
+ # = MANIFEST =
54
+ s.files = %w[
55
+ LICENSE
56
+ README.md
57
+ Rakefile
58
+ fed-0.0.gem
59
+ fed.gemspec
60
+ lib/fed.rb
61
+ lib/fed/feed/atom.rb
62
+ lib/fed/feed/base.rb
63
+ lib/fed/feed/entry.rb
64
+ lib/fed/feed/rss.rb
65
+ lib/fed/http.rb
66
+ lib/fed/http/curb.rb
67
+ ]
68
+ # = MANIFEST =
69
+
70
+ ## Test files will be grabbed from the file list. Make sure the path glob
71
+ ## matches what you actually use.
72
+ s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
73
+ end
@@ -0,0 +1,63 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'curb'
4
+ require 'nokogiri'
5
+ require 'time'
6
+
7
+ require 'fed/http'
8
+ require 'fed/http/curb'
9
+ require 'fed/feed/base'
10
+ require 'fed/feed/atom'
11
+ require 'fed/feed/rss'
12
+ require 'fed/feed/entry'
13
+
14
+ module Fed
15
+ VERSION = '0.0.1'
16
+
17
+ class <<self
18
+
19
+ def fetch_and_parse(feed_url)
20
+ parse fetch(feed_url)
21
+ end
22
+
23
+ def fetch(feed_url)
24
+ Fed::Http.client.get(feed_url)
25
+ end
26
+
27
+ def parse(xml)
28
+ document = Nokogiri::XML(xml)
29
+ parser_for(document).parse
30
+ end
31
+
32
+ def parser_for(doc)
33
+ if is_atom?(doc)
34
+ Fed::Feed::Atom.new(doc)
35
+ elsif is_rss?(doc)
36
+ Fed::Feed::Rss.new(doc)
37
+ elsif (new_url = find_link_in_html(doc))
38
+ parser_for(fetch(new_url))
39
+ else
40
+ raise "Bad feed."
41
+ end
42
+ end
43
+
44
+ def is_atom?(document)
45
+ document.css('feed entry').any?
46
+ end
47
+
48
+ def is_rss?(document)
49
+ document.css('rss channel').any?
50
+ end
51
+
52
+ def find_link_in_html(document)
53
+ elems = document.css("link[type='application/atom+xml']")
54
+
55
+ if elems.any?
56
+ elems.first.attributes['href'].value
57
+ else
58
+ nil
59
+ end
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,34 @@
1
+ module Fed
2
+ module Feed
3
+ class Atom < Base
4
+ def parse
5
+ feed = @document.css('feed').first
6
+
7
+ @title = feed.css('/title').text
8
+ @description = feed.css('/subtitle').text
9
+ @link = feed.css('/link').first.attributes['href'].value
10
+ @updated = DateTime.parse(feed.css('/updated').text) rescue nil
11
+
12
+ @entries = feed.css('entry').map do |entry|
13
+ entry_title = entry.css('/title').text
14
+ entry_summary = entry.css('/summary').text
15
+ entry_content = entry.css('/content').text
16
+ entry_published = DateTime.parse(entry.css('/updated').text) rescue nil
17
+ entry_guid = entry.css('/id').text
18
+ entry_author = entry.css('/author name').map {|a| a.text}.join(', ')
19
+
20
+ link_elem = entry.css("link[rel='alternate']").first
21
+ entry_link = if (link_elem && (attribute = link_elem.attributes['href']))
22
+ attribute.value
23
+ else
24
+ ""
25
+ end
26
+
27
+ Entry.new(entry_title, entry_link, entry_guid, entry_published, entry_author, entry_summary, entry_content)
28
+ end
29
+
30
+ self
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,11 @@
1
+ module Fed
2
+ module Feed
3
+ class Base
4
+ attr_reader :document, :title, :description, :link, :guid, :updated, :entries
5
+
6
+ def initialize(doc)
7
+ @document = doc
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ module Fed
2
+ module Feed
3
+ class Entry < Struct.new(:title, :link, :guid, :published, :author, :summary, :content)
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,28 @@
1
+ module Fed
2
+ module Feed
3
+ class Rss < Base
4
+ def parse
5
+ channel = @document.css('rss channel').first
6
+
7
+ @title = channel.css('/title').text
8
+ @description = channel.css('/description').text
9
+ @link = channel.css('/link').text
10
+ @updated = DateTime.parse(channel.css('/pubDate').text) rescue nil
11
+
12
+ @entries = channel.css('item').map do |item|
13
+ item_title = item.css('/title').text
14
+ item_summary = item.css('/description').text
15
+ item_content = item.css('/description').text
16
+ item_link = item.css('/link').text
17
+ item_published = DateTime.parse(item.css('/pubDate').text) rescue nil
18
+ item_guid = item.css('/guid').text
19
+ item_author = item.css('/author').text
20
+
21
+ Entry.new(item_title, item_link, item_guid, item_published, item_author, item_summary, item_content)
22
+ end
23
+
24
+ self
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ module Fed
2
+ module Http
3
+ class <<self
4
+ attr_writer :options
5
+
6
+ def options
7
+ @options ||= {
8
+ :user_agent => "fed (Ruby feed parser) v.#{Fed::VERSION}"
9
+ }
10
+ end
11
+
12
+ attr_writer :client
13
+
14
+ def client
15
+ @client ||= Fed::Http::Curb
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+
2
+ module Fed
3
+ module Http
4
+ class Curb
5
+ def self.get(url)
6
+ Curl.get(url) do|http|
7
+ http.headers['User-Agent'] = Fed::Http.options[:user_agent]
8
+ end.body_str
9
+ end
10
+ end
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fed
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeremy McAnally
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-25 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: curb
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: mocha
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
+ - !ruby/object:Gem::Dependency
63
+ name: contest
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: A feed parser that actually works and doesn't suck.
79
+ email: jeremy@github.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files:
83
+ - README.md
84
+ - LICENSE
85
+ files:
86
+ - LICENSE
87
+ - README.md
88
+ - Rakefile
89
+ - fed-0.0.gem
90
+ - fed.gemspec
91
+ - lib/fed.rb
92
+ - lib/fed/feed/atom.rb
93
+ - lib/fed/feed/base.rb
94
+ - lib/fed/feed/entry.rb
95
+ - lib/fed/feed/rss.rb
96
+ - lib/fed/http.rb
97
+ - lib/fed/http/curb.rb
98
+ homepage: http://github.com/jm/fed
99
+ licenses: []
100
+ post_install_message:
101
+ rdoc_options:
102
+ - --charset=UTF-8
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 1.8.23
120
+ signing_key:
121
+ specification_version: 2
122
+ summary: A feed parser that actually works.
123
+ test_files: []