Class: FeedUtils::AtomFeedBuilder

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/feedutils/builder/atom.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(atom_feed) ⇒ AtomFeedBuilder

Returns a new instance of AtomFeedBuilder.



8
9
10
# File 'lib/feedutils/builder/atom.rb', line 8

def initialize( atom_feed )
  @feed = build_feed( atom_feed )
end

Class Method Details

.build(atom_feed) ⇒ Object



16
17
18
19
# File 'lib/feedutils/builder/atom.rb', line 16

def self.build( atom_feed )
  feed = self.new( atom_feed )
  feed.to_feed
end

Instance Method Details

#build_feed(atom_feed) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/feedutils/builder/atom.rb', line 22

def build_feed( atom_feed )
  feed = Feed.new
  ## feed.object = atom_feed  # not use for now
  feed.format = 'atom'

  feed.title  = atom_feed.title.content
  logger.debug "  atom | title.content  >#{atom_feed.title.content}< : #{atom_feed.title.content.class.name}"


  logger.debug "  atom | id.content  >#{atom_feed.id.content}< : #{atom_feed.id.content.class.name}"

  feed.url = nil

  ## note: use links (plural to allow multiple links e.g. self,alternate,etc.)
  atom_feed.links.each_with_index do |link,i|
    logger.debug "  atom | link[#{i+1}] link rel=>#{link.rel}< : #{link.rel.class.name} type=#{link.type} href=#{link.href}"

    ## for now assume alternate is link or no rel specified (assumes alternate)
    ##   note: only set if feed.url is NOT already set (via <id> for example)
    if feed.url.nil? && (link.rel == 'alternate' || link.rel.nil?)
      feed.url = link.href
    end
  end

  ## note: as fallback try id if still no url found
  ##   use url only if starts_with http
  ##     might not be link e.g blogger uses for ids =>
  ##    <id>tag:blogger.com,1999:blog-4704664917418794835</id>
  ##
  ##  note: id might actually be link to feed NOT to site  (remove fallback - why - why not???)
  ##
  ## Note: remove (strip) leading and trailing spaces and newlines

  if feed.url.nil? && atom_feed.id.content.strip.start_with?( 'http' )
    feed.url = atom_feed.id.content.strip
  end


  if atom_feed.updated
    # NOTE: empty updated.content e.g.  used by google groups feed
    #   will return nil : NilClass
    
    ## convert from time to to_datetime  (avoid errors on windows w/ builtin rss lib)
    
    feed.updated =  atom_feed.updated.content.nil?  ? nil : atom_feed.updated.content.to_datetime  #  .utc.strftime( "%Y-%m-%d %H:%M" )
    logger.debug "  atom | updated.content  >#{atom_feed.updated.content}< : #{atom_feed.updated.content.class.name}"
  end

  if atom_feed.generator
    ## Note: remove (strip) leading and trailing spaces and newlines
    feed.generator =  atom_feed.generator.content.strip
    logger.debug "  atom | generator.content  >#{atom_feed.generator.content}< : #{atom_feed.generator.content.class.name}"

    # pp atom_feed.generator
    feed.generator_version = atom_feed.generator.version
    feed.generator_uri     = atom_feed.generator.uri
    logger.debug "  atom | generator.version  >#{atom_feed.generator.version}< : #{atom_feed.generator.version.class.name}"
    logger.debug "  atom | generator.uri      >#{atom_feed.generator.uri}< : #{atom_feed.generator.uri.class.name}"
  end

  if atom_feed.subtitle
    feed.title2 =  atom_feed.subtitle.content
    logger.debug "  atom | subtitle.content  >#{atom_feed.subtitle.content}< : #{atom_feed.subtitle.content.class.name}"
  end


  items = []
  atom_feed.items.each do |atom_item|
    items << build_feed_item( atom_item )
  end
  feed.items = items

  feed # return new feed
end

#build_feed_item(atom_item) ⇒ Object

method build_feed_from_atom



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/feedutils/builder/atom.rb', line 97

def build_feed_item( atom_item )
  item = Item.new   # Item.new
  ## item.object = atom_item  # not used for now

  item.title     = atom_item.title.content
  item.url       = atom_item.link.href

  logger.debug "  atom | item.title.content: >#{atom_item.title.content}< : #{atom_item.title.content.class.name}"
  logger.debug "  atom | item.link.href: >#{atom_item.link.href}< : #{atom_item.link.href.class.name}"


  if atom_item.updated
    ## change time to utc if present? why? why not?
    #  --  .utc.strftime( "%Y-%m-%d %H:%M" )

    ## convert from time to to_datetime  (avoid errors on windows w/ builtin rss lib)

    item.updated    =  atom_item.updated.content.nil? ? nil : atom_item.updated.content.to_datetime
    logger.debug "  atom | item.updated.content  >#{atom_item.updated.content}< : #{atom_item.updated.content.class.name}"
  end

  if atom_item.published
    ## convert from time to to_datetime  (avoid errors on windows w/ builtin rss lib)

    item.published   =  atom_item.published.content.nil? ? nil : atom_item.published.content.to_datetime
    logger.debug "  atom | item.published.content  >#{atom_item.published.content}< : #{atom_item.published.content.class.name}"
  end


  item.guid       =  atom_item.id.content
  logger.debug "  atom | item.id.content: >#{atom_item.id.content}< : #{atom_item.id.content.class.name}"

  if atom_item.content
    item.content = atom_item.content.content
  end

  if atom_item.summary
    item.summary = atom_item.summary.content
  end

  item
end

#to_feedObject



12
13
14
# File 'lib/feedutils/builder/atom.rb', line 12

def to_feed
  @feed
end