Skip to content

Commit

Permalink
Parsing feeds and sniff type
Browse files Browse the repository at this point in the history
  • Loading branch information
zetaben committed Aug 8, 2010
1 parent c9d7964 commit d3619ab
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/opds.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
require 'opds/opds'
require 'opds/support/browser.rb'
require 'opds/support/logging'
require 'opds/support/browser'
require 'opds/parser'
3 changes: 3 additions & 0 deletions lib/opds/opds.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
module OPDS
def self.access(feed)
Feed.parse_url(feed)
end
end
38 changes: 38 additions & 0 deletions lib/opds/parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require "nokogiri"
module OPDS
class OPDSParser
include Logging
attr_accessor :options
attr_reader :sniffed_type
def initialize(opts={})
@sniffed_type=nil
self.options=opts.merge({})
end

def parse(content)
@ret=Nokogiri::XML(content)
@sniffed_type=sniff(@ret)
@ret
end
protected
def sniff(doc)
return :entry if doc.root.name=='entry'
entries = doc.xpath('/xmlns:feed/xmlns:entry',doc.root.namespaces)
if entries.size > 0
return :acquisition if entries.all? do |entry|
entry.xpath('xmlns:link').any? do |link|
l=link.attributes['rel']
unless l.nil?
l.value.include?('http://opds-spec.org/acquisition')
else
false
end
end
end
return :navigation
end
return nil
end

end
end
5 changes: 3 additions & 2 deletions lib/opds/support/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module OPDS
module Support
class Browser
include Logging
def go_to(uri)
url=URI.parse(uri)
@last_response=nil
Expand All @@ -13,7 +14,7 @@ def go_to(uri)
@last_response = http.request(req)
}
if status/10==30 && headers['location']
STDERR.puts("Following redirection (code: #{status}) to #{headers['location']}")
log("Following redirection (code: #{status}) to #{headers['location']}")
go_to(headers['location'].first)
end
end
Expand All @@ -32,7 +33,7 @@ def headers
end

def body
@last_response.body
@last_response.body if @last_response
end

end
Expand Down
11 changes: 11 additions & 0 deletions lib/opds/support/logging.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module OPDS
module Logging
def log(txt)
STDERR.puts("LOGGING : #{txt}")
end

def self.log(txt)
STDERR.puts("LOGGING : #{txt}")
end
end
end

0 comments on commit d3619ab

Please sign in to comment.