Class: FeedParser::JsonFeedBuilder

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ JsonFeedBuilder

Returns a new instance of JsonFeedBuilder.



15
16
17
# File 'lib/feedparser/builder/json.rb', line 15

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

Class Method Details

.build(hash) ⇒ Object



10
11
12
13
# File 'lib/feedparser/builder/json.rb', line 10

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

Instance Method Details

#build_author(h) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/feedparser/builder/json.rb', line 48

def build_author( h )
  author = Author.new

  author.name     = h['name']
  author.url      = h['url']
  author.avatar   = h['avatar']

  author
end

#build_feed(h) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/feedparser/builder/json.rb', line 25

def build_feed( h )
  feed = Feed.new
  feed.format = 'json'

  feed.title    = h['title']
  feed.url      = h['home_page_url']
  feed.feed_url = h['feed_url']
  feed.summary  = h['description']


  if h['author']
    feed.authors << build_author( h['author'] )
  end


  h['items'].each do |hash_item|
    feed.items << build_item( hash_item )
  end

  feed # return new feed
end

#build_item(h) ⇒ Object



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
96
97
# File 'lib/feedparser/builder/json.rb', line 60

def build_item( h )
  item = Item.new   # Item.new

  item.guid         = h['id']
  item.title        = h['title']
  item.url          = h['url']
  item.external_url = h['external_url']

  ## convert date if present (from string to date type)
  date_published_str = h['date_published']
  if date_published_str
    item.published_local  = DateTime.iso8601( date_published_str )
    item.published        = item.published_local.utc
  end

  date_modified_str = h['date_modified']
  if date_modified_str
    item.updated_local  = DateTime.iso8601( date_modified_str )
    item.updated        = item.updated_local.utc
  end


  item.content_html = h['content_html']
  item.content_text = h['content_text']
  item.summary      = h['summary']

  if h['author']
    item.authors << build_author( h['author'] )
  end

  if h['tags']
    h['tags'].each do |json_tag|
      item.tags << build_tag( json_tag )
    end
  end

  item
end

#build_tag(json_tag) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/feedparser/builder/json.rb', line 100

def build_tag( json_tag )
  ## pp rss_cat
  tag = Tag.new

  tag.name = json_tag

  tag
end

#to_feedObject



19
20
21
# File 'lib/feedparser/builder/json.rb', line 19

def to_feed
  @feed
end