Class: Fastreader

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

Constant Summary collapse

VERSION =
'1.0.8'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Fastreader

Returns a new instance of Fastreader.



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
# File 'lib/fastreader.rb', line 49

def initialize(options={})
  @curses = options[:curses] # If true it is in curses mode
  @debug = false # change if there is an option
  @environment = options[:environment] 

  if ['test', 'development'].include?(@environment)
    config = File.open(File.dirname(__FILE__) + '/../config/database.yml')
    dbconfig = YAML::load(config)[@environment]
  else
    dbconfig = {"timeout"=>5000, "adapter"=>"sqlite3"}.merge({'database' => options[:database]})
    database_path = options[:database] || ENV['HOME'] + '/fastreader.sqlite3'
    setup_database(database_path)
  end

  ActiveRecord::Base.establish_connection(dbconfig)

  # establish logging if in development mode
  if @debug
    log_file_path = File.dirname(__FILE__) + "/../log/#{@environment}.log"
    log_file = File.open(log_file_path, 'a') 
    log_file.sync = true
  else
    log_file = STDOUT
  end
  ActiveRecord::Base.logger = Logger.new(log_file)
  ActiveRecord::Base.logger.level = Logger::INFO


  # get Display object
  @display = Display.new(options)
end

Instance Attribute Details

#database_pathObject

Returns the value of attribute database_path.



38
39
40
# File 'lib/fastreader.rb', line 38

def database_path
  @database_path
end

Instance Method Details

#auto_discover_and_subscribe(url) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fastreader.rb', line 85

def auto_discover_and_subscribe(url)
  uri = URI.parse(url)
  feed_url = Autodiscovery.new(fetch(url)).discover
  if feed_url
    feed_url = uri.merge(feed_url).to_s
    puts "Found feed: #{feed_url}" 
    return feed_url
  else
    puts "Can't find feed for #{url}" 
    return nil
  end
end

#delete_allObject



188
189
190
# File 'lib/fastreader.rb', line 188

def delete_all
  Feed.delete_all
end

#fetch(url) ⇒ Object

a simple wrapper over open-uri call. Easier to mock in testing.



209
210
211
212
213
214
215
216
217
# File 'lib/fastreader.rb', line 209

def fetch(url)
  begin
    open(url).read
  rescue Timeout::Error 
    puts "-> attempt to fetch #{url} timed out"
  rescue Exception => e
    puts "-> error trying to fetch #{url}: #{$!}"
  end
end

#get_bindingObject



219
220
221
# File 'lib/fastreader.rb', line 219

def get_binding
  return binding()
end

#import_opml(opml) ⇒ Object



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

def import_opml(opml)
  importer = OPMLImporter.new(opml)
  feeds = importer.feed_urls.each do | url |
    subscribe(url)
  end
end

#listObject Also known as: ls



200
201
202
203
204
# File 'lib/fastreader.rb', line 200

def list
  # Add virtual feeds here
  feeds = Feed.feeds_list
  @display.list_feeds( feeds )
end

#most_recent(number = 10) ⇒ Object

Shows the number most recent posts across all feeds



193
194
195
196
197
198
# File 'lib/fastreader.rb', line 193

def most_recent(number=10)
  entries = Entry.find(:all,
                       :order => "last_updated desc",
                       :limit => number)
  @display.display_entries(entries)
end

#parse(command) ⇒ Object



81
82
83
# File 'lib/fastreader.rb', line 81

def parse(command)
  self.instance_eval(command)
end

#puts(string) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/fastreader.rb', line 98

def puts(string)
  if @output_block
    @output_block.call(string)
  else
    STDOUT.puts( string )
  end
end

#setup_database(database_path) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/fastreader.rb', line 40

def setup_database(database_path)
  unless File.exist?(database_path)
    # copy the stock starter feed database (which just contains a few feed
    # subscriptions) to the database_path
    default_db = File.dirname(__FILE__) + "/../db/default.sqlite3"
    cp(default_db, database_path)
  end
end

#subscribe(feed_url, &block) ⇒ Object



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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/fastreader.rb', line 113

def subscribe(feed_url, &block)

  if @output_block.nil? && (block_given? || block)
    @output_block = block
  end
  
  # try to repair the URL if possible
  unless feed_url =~ /^http:\/\//
    feed_url = "http://" + feed_url
  end

  puts "Subscribing to #{feed_url}"
  begin
    xml = fetch(feed_url)
  rescue SocketError
    puts "Error trying to load page at #{feed_url}"
    return
  end
  if xml.nil?
    puts "Can't find any resource at #{feed_url}"
    return
  end

  LOGGER.debug( "xml length: %s, feed_url: %s, block: %s" % [xml.length, feed_url, block.class])
  feed = Feed.create_feed( xml, feed_url.strip, &block )
  LOGGER.debug(feed.class)

  if feed.nil?
  
    puts "Can't find feed at #{feed_url}"
    puts "Attempting autodiscovery..."
 
    feed_url = auto_discover_and_subscribe(feed_url)
    if feed_url
      puts "Subscribing to #{feed_url}"
      xml = fetch(feed_url)

      feed = Feed.create_feed( xml, feed_url.strip, &block )
    end
  end
  feed
end

#update(options = {}, &block) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/fastreader.rb', line 156

def update(options = {}, &block)
  if @output_block.nil? && (block_given? || block)
    @output_block = block
  end

  num = 0 
  if feed_id = options[:feed_id]

    f = Feed.find(feed_id)

    puts "Updating from #{f.feed_url}"

    result =  f.update_self( fetch(f.feed_url), options[:force], &block ) 
    num += result || 0

  else
    Feed.find(:all).each {|f| 
      
      begin 
        puts f.feed_url
        result = f.update_self( fetch(f.feed_url) )  
        num += result || 0
      rescue
        puts "Error trying to update from #{f.feed_url}! Skipping for now."
      end
     
    }
  end
  # Return the number updated 
  return num
end