Module: Atom::Tools

Defined in:
lib/atom/tools.rb

Overview

methods to make writing commandline Atom tools more convenient

Instance Method Summary collapse

Instance Method Details

#atom_options(opts, options) ⇒ Object

set up some common OptionParser settings



120
121
122
123
124
125
126
127
128
# File 'lib/atom/tools.rb', line 120

def atom_options opts, options
  opts.on('-u', '--user NAME', 'username for HTTP auth') { |u| options[:user] = u }

  opts.on_tail('-h', '--help', 'show this usage statement') { |h| puts opts; exit }

  opts.on_tail('-p', '--password [PASSWORD]', 'password for HTTP auth') do |p|
    options[:pass] = p
  end
end

#dir_to_entries(path) ⇒ Object

parse a directory of entries

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
# File 'lib/atom/tools.rb', line 20

def dir_to_entries path
  raise ArgumentError, "#{path} is not a directory" unless File.directory? path

  Dir[path+'/*.atom'].map do |e|
    Atom::Entry.parse(File.read(e))
  end
end

#entries_to_dir(entries, path) ⇒ Object

saves an Array of Atom::Entrys to a directory



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/atom/tools.rb', line 41

def entries_to_dir entries, path
  if File.exists? path
    raise "directory #{path} already exists"
  else
    Dir.mkdir path
  end

  entries.each do |entry|
    e = entry.to_s

    new_filename = path + '/0x' + MD5.new(e).hexdigest[0,8] + '.atom'

    File.open(new_filename, 'w') { |f| f.write e }
  end
end

#entries_to_http(entries, url, http = Atom::HTTP.new) ⇒ Object

POSTs an Array of Atom::Entrys to an Atom Collection



34
35
36
37
38
# File 'lib/atom/tools.rb', line 34

def entries_to_http entries, url, http = Atom::HTTP.new
  coll = Atom::Collection.new url, http

  entries.each { |entry| coll.post! entry }
end

#entries_to_stdout(entries) ⇒ Object

dumps an Array of Atom::Entrys into a Feed on stdout



58
59
60
61
62
63
64
65
66
67
# File 'lib/atom/tools.rb', line 58

def entries_to_stdout entries
  feed = Atom::Feed.new

  entries.each do |entry|
    puts entry.inspect
    feed.entries << entry
  end

  puts feed.to_s
end

#http_to_entries(url, complete_feed = false, http = Atom::HTTP.new) ⇒ Object

fetch and parse a Feed URL, returning the entries found



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/atom/tools.rb', line 7

def http_to_entries url, complete_feed = false, http = Atom::HTTP.new
  feed = Atom::Feed.new url, http

  if complete_feed
    feed.get_everything!
  else
    feed.update!
  end

  feed.entries
end

#obtain_passwordObject

obtain a password from the TTY, hiding the user’s input this will fail if you don’t have the program ‘stty’



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/atom/tools.rb', line 133

def obtain_password
  i = o = File.open('/dev/tty', 'w+')

  o.print 'Password: '

  # store original settings
  state = `stty -F /dev/tty -g`

  # don't echo input
  system "stty -F /dev/tty -echo"

  p = i.gets.chomp

  # restore original settings
  system "stty -F /dev/tty #{state}"

  p
end

#parse_input(source, options) ⇒ Object

turns a collection of Atom Entries into an Array of Atom::Entrys

source: a URL, a directory or “-” for an Atom Feed on stdin options:

:complete - whether to fetch the complete logical feed
:user - username to use for HTTP requests (if required)
:pass - password to use for HTTP requests (if required)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/atom/tools.rb', line 76

def parse_input source, options
  entries = if source.match /^http/
           http = Atom::HTTP.new

           setup_http http, options

           http_to_entries source, options[:complete], http
         elsif source == '-'
           stdin_to_entries
         else
           dir_to_entries source
         end

  if options[:verbose]
    entries.each do |entry|
      puts "got #{entry.title}"
    end
  end

  entries
end

#setup_http(http, options) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/atom/tools.rb', line 152

def setup_http http, options
  if options[:user]
    http.user = options[:user]

    unless options[:pass]
      options[:pass] = obtain_password
    end

    http.pass = options[:pass]
  end
end

#stdin_to_entriesObject

parse a Feed on stdin



29
30
31
# File 'lib/atom/tools.rb', line 29

def stdin_to_entries
  Atom::Feed.parse($stdin).entries
end

#write_output(entries, dest, options) ⇒ Object

turns an Array of Atom::Entrys into a collection of Atom Entries

entries: an Array of Atom::Entrys pairs dest: a URL, a directory or “-” for an Atom Feed on stdout options:

:user - username to use for HTTP requests (if required)
:pass - password to use for HTTP requests (if required)


105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/atom/tools.rb', line 105

def write_output entries, dest, options
  if dest.match /^http/
    http = Atom::HTTP.new

    setup_http http, options

    entries_to_http entries, dest, http
  elsif dest == '-'
    entries_to_stdout entries
  else
    entries_to_dir entries, dest
  end
end