adammck-pants 0.2

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 (4) hide show
  1. data/bin/pants +5 -0
  2. data/lib/pants.rb +130 -0
  3. data/pants.gemspec +23 -0
  4. metadata +74 -0
data/bin/pants ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ # vim: noet
3
+
4
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/pants.rb")
5
+ Pants.new(ARGV, STDIN).run
data/lib/pants.rb ADDED
@@ -0,0 +1,130 @@
1
+ #!/usr/bin/env ruby
2
+ # vim: noet
3
+
4
+
5
+ require "rubygems"
6
+ require "open-uri"
7
+ require "simple-rss"
8
+ require "json"
9
+ require "erb"
10
+
11
+
12
+ class Pants
13
+ DATE_TAGS = [:pubDate, :published, :updated, :expirationDate, :modified]
14
+
15
+ CONTENT_TYPES = {
16
+ "application/json" => :json,
17
+ "application/rss+xml" => :rss,
18
+ "application/atom+xml" => :atom
19
+ }
20
+
21
+ # parse one or two command-line
22
+ # arguments, or quit with usage
23
+ def initialize(args, stdin)
24
+ if args.length == 3
25
+ @type = args[2]
26
+ @tmpl = IO.read(args[1])
27
+ @uri = args[0]
28
+
29
+ elsif args.length == 2
30
+ @tmpl = IO.read(args[1])
31
+ @uri = args[0]
32
+
33
+ elsif args.length == 1
34
+ @uri = args[0]
35
+ @tmpl = TMPL
36
+
37
+ else
38
+ puts "Usage: pants.rb FEED [TEMPLATE] [FORCE-TYPE]"
39
+ puts ""
40
+ puts " FEED: Remote URI of source data"
41
+ puts " TEMPLATE: Local path of ERB template to build"
42
+ puts " FORCE-TYPE: Content type to override mime type of fetched document"
43
+ puts " (Available types are: #{CONTENT_TYPES.values.join(", ")})"
44
+ exit 1
45
+ end
46
+ end
47
+
48
+ # fetch the feed over http, parse it,
49
+ # and generate the output with erb. if
50
+ # anything at all goes wrong, abort
51
+ def run
52
+ catch(:abort) do
53
+ begin
54
+ open(@uri) do |s|
55
+
56
+ # if we're not forcing a particular parser type,
57
+ # check that the actual content type is supported
58
+ unless @type
59
+ ct = s.content_type
60
+ fail("unsupported content type", ct) unless\
61
+ @type = CONTENT_TYPES[ct]
62
+ end
63
+
64
+ # get the raw data
65
+ @src = s.read
66
+ end
67
+
68
+ # parse the document (with whichever parser type was
69
+ # forced or fetched) and render the results with ERB
70
+ @data = send(parser = "parse_#{@type}")
71
+ puts ERB.new(@tmpl).result(binding)
72
+
73
+ rescue OpenURI::HTTPError => err
74
+ fail "fetching uri", err
75
+
76
+ rescue SimpleRSSError => err
77
+ fail "parsing rss", err
78
+
79
+ rescue StandardError => err
80
+ fail "rendering", err.message
81
+ end
82
+ end
83
+ end
84
+
85
+ private
86
+
87
+ def parse_json
88
+ JSON.parse(@src)
89
+ end
90
+
91
+ def parse_rss
92
+ SimpleRSS.parse(@src)
93
+ end
94
+
95
+ def parse_atom
96
+ SimpleRSS.parse(@src)
97
+ end
98
+
99
+ # returns the first date tag present in _item_,
100
+ # in the priority of DATE_TAGS (since in our default
101
+ # template, we're only (currently) using one of them)
102
+ def the_date(item)
103
+ DATE_TAGS.each do |tag|
104
+ return item[tag] if\
105
+ item.has_key?(tag)
106
+ end
107
+ end
108
+
109
+ # whether or not something goes wrong, the output of this program will
110
+ # probably be piped into a tmp file to be included in an HTML document;
111
+ # so include some HTML in the error, to hide (or highlight) it with CSS
112
+ def fail(doing, text)
113
+ puts "<div class='feed-error'>Error while #{doing}: <span>#{text}</span></div>"
114
+ throw(:abort)
115
+ end
116
+ end
117
+
118
+
119
+ Pants::TMPL = <<EOT
120
+ <div class="feed">
121
+ <h2><%= @data.channel.title %></h2>
122
+ <ul><% @data.items.each do |item| %>
123
+ <li>
124
+ <h3><a href="<%= item.link %>"><%= item.title %></a></h3>
125
+ <div class="date"><%= the_date(item) %></div>
126
+ <div class="desc"><%= item.description %></div>
127
+ </li><% end %>
128
+ </ul>
129
+ </div>
130
+ EOT
data/pants.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "pants"
3
+ s.version = "0.2"
4
+ s.date = "2009-02-18"
5
+ s.summary = "Command-line Ruby application to transform an RSS or Atom feed (or JSON document!) into a chunk of HTML via ERB"
6
+ s.email = "adam.mckaig@gmail.com"
7
+ s.homepage = "http://github.com/adammck/pants"
8
+ s.authors = ["Adam Mckaig"]
9
+ s.has_rdoc = true
10
+
11
+ s.files = [
12
+ "pants.gemspec",
13
+ "lib/pants.rb",
14
+ "bin/pants"
15
+ ]
16
+
17
+ s.executables = [
18
+ "pants"
19
+ ]
20
+
21
+ s.add_dependency("simple-rss")
22
+ s.add_dependency("json")
23
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adammck-pants
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.2"
5
+ platform: ruby
6
+ authors:
7
+ - Adam Mckaig
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-18 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: simple-rss
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description:
36
+ email: adam.mckaig@gmail.com
37
+ executables:
38
+ - pants
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - pants.gemspec
45
+ - lib/pants.rb
46
+ - bin/pants
47
+ has_rdoc: true
48
+ homepage: http://github.com/adammck/pants
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: Command-line Ruby application to transform an RSS or Atom feed (or JSON document!) into a chunk of HTML via ERB
73
+ test_files: []
74
+