Class: RSSManager

Inherits:
Object
  • Object
show all
Defined in:
lib/core.rb

Overview

RSS Manager: Core Channel Manager Class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRSSManager

Returns a new instance of RSSManager.



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/core.rb', line 56

def initialize
  config_file = File.join(ENV['HOME'], 'subscribe-rss.list')
  unless File.exist?(config_file)
    File.open(config_file, 'w') do |f|
      f << 'https://www.ruby-lang.org/en/feeds/news.rss'
    end
  end
  @channel_config_path = config_file
  @channel_urls = []
  @channels = []

  init_pipeline
end

Instance Attribute Details

#channelsObject

Returns the value of attribute channels.



54
55
56
# File 'lib/core.rb', line 54

def channels
  @channels
end

#current_channel_indexObject

Returns the value of attribute current_channel_index.



54
55
56
# File 'lib/core.rb', line 54

def current_channel_index
  @current_channel_index
end

Instance Method Details

#async_fetchObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/core.rb', line 96

def async_fetch
  # use: 2.854694
  Async do |task|
    @channels.each do |ch|
      task.async do
        ch.fetch_channel
      end
    end
  end
end

#channel_by_index(channel_index) ⇒ Object



126
127
128
# File 'lib/core.rb', line 126

def channel_by_index(channel_index)
  @channels[channel_index]
end

#channel_items(channel_index = 0) ⇒ Object



130
131
132
# File 'lib/core.rb', line 130

def channel_items(channel_index = 0)
  @channels[channel_index].items
end

#channel_title_by_index(channel_index) ⇒ Object



121
122
123
124
# File 'lib/core.rb', line 121

def channel_title_by_index(channel_index)
  channel = @channels[channel_index]
  channel.feed.channel.title
end

#channel_titlesObject



113
114
115
116
117
118
119
# File 'lib/core.rb', line 113

def channel_titles
  titles = []
  @channels.each_with_index do |x, index|
    titles.push "#{"[#{index}]".green} #{x.feed.channel.title.to_s.blue} (#{x.feed.channel.items.length}) \n"
  end
  titles
end

#init_channelsObject



78
79
80
81
82
# File 'lib/core.rb', line 78

def init_channels
  @channel_urls.each do |channel_url|
    @channels.push(RSSChannel.new(channel_url))
  end
end

#init_pipelineObject



107
108
109
110
111
# File 'lib/core.rb', line 107

def init_pipeline
  load_channel_urls
  init_channels
  update_channels
end

#load_channel_urlsObject



70
71
72
73
74
75
76
# File 'lib/core.rb', line 70

def load_channel_urls
  channel_lines = File.open(@channel_config_path).readlines.map(&:strip)
  @channel_urls = channel_lines.filter do |x|
    x != '' && x.index('#') != 0
  end
  @channel_urls
end

#sync_fetchObject



91
92
93
94
# File 'lib/core.rb', line 91

def sync_fetch
  # use: 6.022412
  @channels.each(&:fetch_channel)
end

#update_channelsObject



84
85
86
87
88
89
# File 'lib/core.rb', line 84

def update_channels
  time_start = Time.now
  async_fetch
  time_end = Time.now
  puts "use: #{time_end - time_start}"
end