class Nemweb::Client

Constants

CURRENT_REPORTS

Public Instance Methods

dirs(root_uri = CURRENT_REPORTS) click to toggle source

Public: List available NEM data directories

# File lib/nemweb/client.rb, line 53
def dirs(root_uri = CURRENT_REPORTS)
  doc = Nokogiri::HTML.parse(root_uri.read)
  doc.css('a').map { |link| root_uri + link["href"] }
end
fetch(source) { |get_input_stream, name| ... } click to toggle source

Open a data source, and yield a stream.

source - a data file or URI

If the data source is a ZIP file, extract the first entry.

# File lib/nemweb/client.rb, line 64
def fetch(source)
  source_uri = CURRENT_REPORTS + source
  source_uri.open do |stream|
    if source_uri.to_s =~ /zip\Z/i
      Zip::File.open_buffer(stream) do |zipfile|
        zipfile.entries.each do |entry|
          yield entry.get_input_stream, entry.name
        end
      end
    else
      yield stream, source_uri
    end
  end
end
list_files(dir) click to toggle source

Public: Get the files in a NEM data directory.

dir - subdirectory of nemweb.com.au/Reports/Current/

# File lib/nemweb/client.rb, line 43
def list_files(dir)
  dir_uri = CURRENT_REPORTS + dir
  doc = Nokogiri::HTML.parse(dir_uri.read)
  doc.css('a').map { |link| link["href"] }.grep(/\.zip$/).map do |href|
    DataFile.new(dir_uri + href, self)
  end
end
parse(source) { |record| ... } click to toggle source

Public: Load and parse data.

source - a data file or URI

Yields records, as Hash objects.

# File lib/nemweb/client.rb, line 19
def parse(source)
  fetch(source) do |stream|
    headers = nil
    CSV.parse(stream) do |row|
      case row[0]
      when "I"
        headers = row[4..-1]
      when "D"
        record = DataRecord.new(
          row[1], row[2], Integer(row[3]),
          Hash[headers.zip(row[4..-1])]
        )
        yield record
      end
    end
  end
end