Class: Feed2Email::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/feed2email/cli.rb

Instance Method Summary collapse

Instance Method Details

#add(uri) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/feed2email/cli.rb', line 12

def add(uri)
  uri = autodiscover_feeds(uri)

  if feed = Feed[uri: uri]
    abort "Feed already exists: #{feed}"
  end

  feed = Feed.new(uri: uri)

  if feed.save(raise_on_failure: false)
    puts "Added feed: #{feed}"
  else
    abort 'Failed to add feed'
  end
end

#backendObject



29
30
31
# File 'lib/feed2email/cli.rb', line 29

def backend
  exec('sqlite3', Feed2Email.database_path)
end

#configObject



34
35
36
37
38
39
40
# File 'lib/feed2email/cli.rb', line 34

def config
  if ENV['EDITOR']
    exec(ENV['EDITOR'], Feed2Email.config_path)
  else
    abort 'EDITOR environmental variable not set'
  end
end

#export(path) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/feed2email/cli.rb', line 43

def export(path)
  if Feed.empty?
    abort 'No feeds to export'
  end

  unless File.exist?(path)
    puts 'This may take a while. Please wait...'

    if n = OPMLExporter.export(path)
      puts "Exported #{'feed subscription'.pluralize(n)} to #{path}"
    else
      abort 'Failed to export feed subscriptions'
    end
  else
    abort 'File already exists'
  end
end

#import(path) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/feed2email/cli.rb', line 64

def import(path)
  if File.exist?(path)
    puts 'Importing...'

    if n = OPMLImporter.import(path, options[:remove])
      if n > 0
        puts "Imported #{'feed subscription'.pluralize(n)} from #{path}"
      end
    else
      abort 'Failed to import feed subscriptions'
    end
  else
    abort 'File does not exist'
  end
end

#listObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/feed2email/cli.rb', line 81

def list
  if Feed.any?
    puts Feed.by_smallest_id.to_a
    print "\nSubscribed to #{'feed'.pluralize(Feed.count)}"

    if Feed.where(enabled: false).count > 0
      print " (#{Feed.enabled.count} enabled)"
    end

    puts
  else
    puts 'No feeds'
  end
end

#processObject



97
98
99
# File 'lib/feed2email/cli.rb', line 97

def process
  Feed.enabled.by_smallest_id.each(&:process)
end

#remove(id) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/feed2email/cli.rb', line 102

def remove(id)
  feed = Feed[id]

  if feed
    puts "Remove feed: #{feed}"

    if ask('Are you sure?', limited_to: %w{y n}) == 'y'
      if feed.delete
        puts 'Removed'
      else
        abort 'Failed to remove feed'
      end
    else
      puts 'Not removed'
    end
  else
    abort "Feed not found. Is #{id} a valid id?"
  end
end

#toggle(id) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/feed2email/cli.rb', line 123

def toggle(id)
  feed = Feed[id]

  if feed && feed.toggle
    puts "Toggled feed: #{feed}"
  else
    abort "Failed to toggle feed. Is #{id} a valid id?"
  end
end

#uncache(id) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/feed2email/cli.rb', line 134

def uncache(id)
  feed = Feed[id]

  if feed && feed.uncache
    puts "Uncached feed: #{feed}"
  else
    abort "Failed to uncache feed. Is #{id} a valid id?"
  end
end

#versionObject



145
146
147
# File 'lib/feed2email/cli.rb', line 145

def version
  puts "feed2email #{Feed2Email::VERSION}"
end