Class: Nemweb::Client
- Inherits:
-
Object
- Object
- Nemweb::Client
- Defined in:
- lib/nemweb/client.rb
Constant Summary collapse
- CURRENT_REPORTS =
URI("http://nemweb.com.au/Reports/Current/").freeze
Instance Method Summary collapse
-
#dirs(root_uri = CURRENT_REPORTS) ⇒ Object
Public: List available NEM data directories.
-
#fetch(source) ⇒ Object
Open a data source, and yield a stream.
-
#list_files(dir) ⇒ Object
Public: Get the files in a NEM data directory.
-
#parse(source) ⇒ Object
Public: Load and parse data.
Instance Method Details
#dirs(root_uri = CURRENT_REPORTS) ⇒ Object
Public: List available NEM data directories
53 54 55 56 |
# 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) ⇒ Object
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.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# 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) ⇒ Object
Public: Get the files in a NEM data directory.
dir - subdirectory of nemweb.com.au/Reports/Current/
43 44 45 46 47 48 49 |
# 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) ⇒ Object
Public: Load and parse data.
source - a data file or URI
Yields records, as Hash objects.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# 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 |