nemweb 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 365f80b7362e4ef860617c2901c610f2b73480e798f9aecc70f4f97bb7fdb755
4
+ data.tar.gz: 25941839fbd9b5bda7a163d101b60ca36a5ac5040899918f43a081b777c741d9
5
+ SHA512:
6
+ metadata.gz: c445e7f378714cd116569814f4341f66b59566a5ff5ca28d240e74a190c59a888db8fec12d15ecf856d83ce212ce151aa03cbf39f0f71cb5087b9348c0e5a13b
7
+ data.tar.gz: 10c2c08f7ffad64e142f565084484920e834a2480ff31126a0650680883b22ace76fa87b9ef0708bef4b46c1d0a9a0a23b4bf0afad0903b03fc447ef80cc2bd1
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in nemweb.gemspec
4
+ gemspec
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nemweb (0.2.0)
5
+ clamp
6
+ nokogiri
7
+ rubyzip
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ clamp (1.3.1)
13
+ diff-lcs (1.3)
14
+ mini_portile2 (2.4.0)
15
+ nokogiri (1.10.3)
16
+ mini_portile2 (~> 2.4.0)
17
+ rake (10.5.0)
18
+ rspec (3.8.0)
19
+ rspec-core (~> 3.8.0)
20
+ rspec-expectations (~> 3.8.0)
21
+ rspec-mocks (~> 3.8.0)
22
+ rspec-core (3.8.0)
23
+ rspec-support (~> 3.8.0)
24
+ rspec-expectations (3.8.2)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.8.0)
27
+ rspec-mocks (3.8.0)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.8.0)
30
+ rspec-support (3.8.0)
31
+ rubyzip (1.2.3)
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ bundler (~> 2.0)
38
+ nemweb!
39
+ rake (~> 10.0)
40
+ rspec (~> 3.0)
41
+
42
+ BUNDLED WITH
43
+ 2.0.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Mike Williams
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # Nemweb
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/nemweb`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'nemweb'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install nemweb
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/nemweb.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "nemweb"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,145 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'clamp'
4
+ require 'json'
5
+ require 'set'
6
+ require 'time'
7
+
8
+ $stdout.sync = true
9
+ $stderr.sync = true
10
+
11
+ $LOAD_PATH << File.expand_path('../lib', __dir__)
12
+
13
+ require 'nemweb/client'
14
+
15
+ Clamp do
16
+
17
+ subcommand "dirs", "List NEMWEB data directories" do
18
+
19
+ def execute
20
+ nemweb.dirs.each do |dir|
21
+ puts dir.path.split("/").last
22
+ end
23
+ end
24
+
25
+ end
26
+
27
+ subcommand "ls", "List a NEMWEB data directory" do
28
+
29
+ option %w(-l --long), :flag, "print details"
30
+
31
+ parameter "DIR", "directory to list"
32
+
33
+ def execute
34
+ lags = []
35
+ nemweb.list_files(dir).each do |data_file|
36
+ line = data_file.uri.to_s
37
+ if long?
38
+ line += " lag=#{get_lag(data_file)}s"
39
+ end
40
+ puts line
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def get_lag(data_file)
47
+ return [] unless long?
48
+ period_start = Time.parse("#{data_file.period} +1000")
49
+ last_modified = Time.parse(http_head(data_file.uri).fetch("last-modified").first)
50
+ (last_modified - period_start).to_i
51
+ end
52
+
53
+ def http_head(uri)
54
+ uri = URI(uri)
55
+ Net::HTTP.start(uri.host, uri.port) do |http|
56
+ http.head(uri.path).to_hash
57
+ end
58
+ end
59
+
60
+ end
61
+
62
+ subcommand "fetch", "Fetch a NEMWEB data file" do
63
+
64
+ option %w(--name -n), :flag, "display data file name"
65
+ option %w(--name-only -N), :flag, "display data file name, only"
66
+
67
+ parameter "SOURCE", "data file URI or path"
68
+
69
+ def execute
70
+ nemweb.fetch(source) do |io, name|
71
+ puts "file: #{name}" if name? || name_only?
72
+ IO.copy_stream(io, $stdout) unless name_only?
73
+ end
74
+ end
75
+
76
+ end
77
+
78
+ subcommand "parse", "Parse a NEMWEB data file" do
79
+
80
+ parameter "SOURCE", "data file URI or path"
81
+
82
+ def execute
83
+ nemweb.parse(source) do |record|
84
+ puts JSON.dump(record.to_h)
85
+ end
86
+ end
87
+
88
+ end
89
+
90
+ module DirectoryMonitoring
91
+
92
+ def self.included(target)
93
+ target.option ["-i", "--poll-interval"], "SECONDS", "polling interval", default: 10, &method(:Integer)
94
+ target.option ["-a", "--after"], "PERIOD", "last period processed"
95
+ end
96
+
97
+ def on_new_data_file(directories)
98
+ while true do
99
+ begin
100
+ directories.each do |dir|
101
+ files = nemweb.list_files(dir)
102
+ new_files = if cursors[dir].nil?
103
+ [files.last]
104
+ else
105
+ files.select { |data_file| data_file.period > cursors[dir] }
106
+ end
107
+ new_files.each do |data_file|
108
+ yield data_file
109
+ cursors[dir] = data_file.period
110
+ end
111
+ end
112
+ rescue OpenURI::HTTPError
113
+ puts " HTTPError!"
114
+ end
115
+ sleep(poll_interval)
116
+ end
117
+ end
118
+
119
+ def cursors
120
+ @cursors ||= Hash.new(after)
121
+ end
122
+
123
+ end
124
+
125
+ subcommand "watch", "Watch a NEMWEB data directory" do
126
+
127
+ parameter "DIR ...", "directories to watch"
128
+
129
+ include DirectoryMonitoring
130
+
131
+ def execute
132
+ on_new_data_file(dir_list) do |data_file|
133
+ puts data_file.uri
134
+ end
135
+ end
136
+
137
+ end
138
+
139
+ private
140
+
141
+ def nemweb
142
+ Nemweb::Client.new
143
+ end
144
+
145
+ end
@@ -0,0 +1,81 @@
1
+ require 'csv'
2
+ require 'nemweb/data_file'
3
+ require 'nemweb/data_record'
4
+ require 'net/http'
5
+ require 'nokogiri'
6
+ require 'open-uri'
7
+ require 'zip'
8
+
9
+ module Nemweb
10
+
11
+ class Client
12
+
13
+ # Public: Load and parse data.
14
+ #
15
+ # source - a data file or URI
16
+ #
17
+ # Yields records, as Hash objects.
18
+ #
19
+ def parse(source)
20
+ fetch(source) do |stream|
21
+ headers = nil
22
+ CSV.parse(stream) do |row|
23
+ case row[0]
24
+ when "I"
25
+ headers = row[4..-1]
26
+ when "D"
27
+ record = DataRecord.new(
28
+ row[1], row[2], Integer(row[3]),
29
+ Hash[headers.zip(row[4..-1])]
30
+ )
31
+ yield record
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ CURRENT_REPORTS = URI("http://nemweb.com.au/Reports/Current/").freeze
38
+
39
+ # Public: Get the files in a NEM data directory.
40
+ #
41
+ # dir - subdirectory of http://nemweb.com.au/Reports/Current/
42
+ #
43
+ def list_files(dir)
44
+ dir_uri = CURRENT_REPORTS + dir
45
+ doc = Nokogiri::HTML.parse(dir_uri.read)
46
+ doc.css('a').map { |link| link["href"] }.grep(/\.zip$/).map do |href|
47
+ DataFile.new(dir_uri + href, self)
48
+ end
49
+ end
50
+
51
+ # Public: List available NEM data directories
52
+ #
53
+ def dirs(root_uri = CURRENT_REPORTS)
54
+ doc = Nokogiri::HTML.parse(root_uri.read)
55
+ doc.css('a').map { |link| root_uri + link["href"] }
56
+ end
57
+
58
+ # Open a data source, and yield a stream.
59
+ #
60
+ # source - a data file or URI
61
+ #
62
+ # If the data source is a ZIP file, extract the first entry.
63
+ #
64
+ def fetch(source)
65
+ source_uri = CURRENT_REPORTS + source
66
+ source_uri.open do |stream|
67
+ if source_uri.to_s =~ /zip\Z/i
68
+ Zip::File.open_buffer(stream) do |zipfile|
69
+ zipfile.entries.each do |entry|
70
+ yield entry.get_input_stream, entry.name
71
+ end
72
+ end
73
+ else
74
+ yield stream, source_uri
75
+ end
76
+ end
77
+ end
78
+
79
+ end
80
+
81
+ end
@@ -0,0 +1,24 @@
1
+ module Nemweb
2
+
3
+ class DataFile < Struct.new(:uri)
4
+
5
+ def initialize(uri, client)
6
+ super(uri)
7
+ @client = client
8
+ end
9
+
10
+ def name
11
+ File.basename(uri.path)
12
+ end
13
+
14
+ def period
15
+ name.scan(/_(\d{4,})/).first.first
16
+ end
17
+
18
+ def parse(&block)
19
+ @client.parse(uri, &block)
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,16 @@
1
+ module Nemweb
2
+
3
+ class DataRecord < Struct.new(:type, :subtype, :version, :data)
4
+
5
+ def to_h
6
+ {
7
+ "type" => type,
8
+ "subtype" => subtype,
9
+ "version" => version,
10
+ "data" => data
11
+ }
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,3 @@
1
+ module Nemweb
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,33 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "nemweb/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nemweb"
8
+ spec.version = Nemweb::VERSION
9
+ spec.authors = ["Mike Williams"]
10
+ spec.email = ["mike.williams@greensync.com.au"]
11
+
12
+ spec.summary = %q{A Ruby client for nemweb.com.au}
13
+ spec.homepage = "https://github.com/greensync/nemweb-ruby"
14
+ spec.license = "MIT"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 2.0"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+
29
+ spec.add_runtime_dependency "clamp"
30
+ spec.add_runtime_dependency "nokogiri"
31
+ spec.add_runtime_dependency "rubyzip"
32
+
33
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nemweb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Williams
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-07-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: clamp
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubyzip
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - mike.williams@greensync.com.au
100
+ executables:
101
+ - nemweb
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - exe/nemweb
116
+ - lib/nemweb/client.rb
117
+ - lib/nemweb/data_file.rb
118
+ - lib/nemweb/data_record.rb
119
+ - lib/nemweb/version.rb
120
+ - nemweb.gemspec
121
+ homepage: https://github.com/greensync/nemweb-ruby
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.7.6
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: A Ruby client for nemweb.com.au
145
+ test_files: []