Class: Bwkfanboy::Fetch

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

Class Method Summary collapse

Class Method Details

.cat(uri) ⇒ Object

If no block given, return contents of fetch’ed URI. Otherwise, execute the block with 1 parameter–stream.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bwkfanboy/fetch.rb', line 10

def self.cat(uri)
  uri.chomp!

  Bwkfanboy::Utils.veputs(1, "fetching #{uri}\n")

  begin
    open(uri, "User-Agent" => Bwkfanboy::Meta::USER_AGENT) {|f|
      if defined?(f.meta) && f.status[0] != '200' then
        Bwkfanboy::Utils.errx(1, "cannot fetch #{uri} : HTTP responce: #{f.status[0]}")
      end
      Bwkfanboy::Utils.veputs(1, "charset = #{f.content_type_parse[1][1]}\n") if defined?(f.meta)
      if block_given?
        yield f
      else
        return f.read
      end
    }
  rescue
    # typically Errno::ENOENT
    Bwkfanboy::Utils.errx(1, "cannot fetch: #{$!}");
  end

  return ""
end