amber_electric 0.0.1

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.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +2 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.md +19 -0
  5. data/lib/amber_electric.rb +84 -0
  6. metadata +89 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '0080d683e3ce298401efef6dd5831929119e83b44d96dbd73efbcbae4625ae0e'
4
+ data.tar.gz: f30a5a064c78a23fdc23f9572d4fadba441eb5a2bc1f314488f1ae05bc4c60fe
5
+ SHA512:
6
+ metadata.gz: '0739e12b7e0131c2c4bbcc6a682cf83eb0bd1928add7b1fb8e0bca394012044c87e748562f1b7ccb0ef9f744e7cc5b158aa40827e769fa9d7d43bc74482916d6'
7
+ data.tar.gz: e5bfa07bcddab84e4b1932778a9f0b9fcd03c8983136590476ca785e9abcaa58ebf6a89b8c3a6293b32d6baeb379fe6691b1ec3eafda94511a88324ef1f7a65d
data/CHANGELOG ADDED
@@ -0,0 +1,2 @@
1
+ v0.0.1
2
+ - Initial Release
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2021 James Healy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # Amber Electric
2
+
3
+ A ruby API client for https://amberelectric.com.au, and Australian Electricity Retailer.
4
+
5
+ Currently Amber only provides an unauthenticated API for pricing, so no credentials are required.
6
+
7
+ ## Installation
8
+
9
+ gem install amber_electric
10
+
11
+ ... or add it to your Gemfile:
12
+
13
+ gem "amber_electric"
14
+
15
+ ## Usage
16
+
17
+ require "amber_electric"
18
+
19
+ puts AmberElectric::Prices.for(postcode: 3000).inspect
@@ -0,0 +1,84 @@
1
+ require "net/http"
2
+ require "json"
3
+ require "tzinfo"
4
+ require "bigdecimal"
5
+ require "time"
6
+
7
+ module AmberElectric
8
+
9
+ class Client
10
+ def self.prices_for(postcode: )
11
+ self.new.prices_for(postcode: postcode)
12
+ end
13
+
14
+ # curl -X POST "https://api.amberelectric.com.au/prices/listprices" -H 'Content-Type: application/json' -d '{ "postcode": "3058" }'
15
+ def prices_for(postcode: )
16
+ http = Net::HTTP.new("api.amberelectric.com.au", 443)
17
+ http.use_ssl = true
18
+
19
+ request = Net::HTTP::Post.new("/prices/listprices")
20
+ request["Content-Type"] = "application/json"
21
+ request.body = {postcode: postcode.to_s}.to_json
22
+
23
+ response = http.request(request)
24
+
25
+ if response.code.to_i == 200
26
+ data = JSON.parse(response.body)
27
+
28
+ Prices.new(
29
+ market_period_end: current_period_end,
30
+ static_import_cents_per_kwh: per_kwh(data, "E1"),
31
+ controlled_load_cents_per_kwh: per_kwh(data, "E2"),
32
+ export_cents_per_kwh: per_kwh(data, "B1")
33
+ )
34
+ else
35
+ raise "Error fetching prices: #{response.inspect}"
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def current_period_end
42
+ tz = TZInfo::Timezone.get('Australia/Brisbane')
43
+ market_time = tz.now
44
+ if market_time.min >= 0 and market_time.min <= 30
45
+ market_period_end = tz.local_time(market_time.year, market_time.month, market_time.day, market_time.hour, 30)
46
+ else
47
+ market_period_end = tz.local_time(market_time.year, market_time.month, market_time.day, market_time.hour+1)
48
+ end
49
+ end
50
+
51
+ def per_kwh(data, type)
52
+ total_fixed_kwh_price = BigDecimal(data.fetch("data", {}).fetch("staticPrices", {}).fetch(type, {}).fetch("totalfixedKWHPrice", "0"))
53
+ loss_factor = BigDecimal(data.fetch("data", {}).fetch("staticPrices", {}).fetch("E1", {}).fetch("lossFactor", "0"))
54
+ market_period_end_iso8601 = current_period_end.strftime("%Y-%m-%dT%H:%M:%S")
55
+
56
+ current_actual = data.fetch("data", {}).fetch("variablePricesAndRenewables", []).select { |row|
57
+ row["periodType"] == "ACTUAL"
58
+ }.select { |row|
59
+ row["period"] == market_period_end_iso8601
60
+ }.first
61
+
62
+ if current_actual
63
+ (total_fixed_kwh_price + (loss_factor * BigDecimal(current_actual.fetch("wholesaleKWHPrice", "0")))).round(3)
64
+ end
65
+ end
66
+ end
67
+ class Prices
68
+
69
+ attr_reader :market_period_end
70
+ attr_reader :static_import_cents_per_kwh, :controlled_load_cents_per_kwh, :export_cents_per_kwh
71
+
72
+ def initialize(market_period_end:, static_import_cents_per_kwh: , controlled_load_cents_per_kwh: , export_cents_per_kwh: )
73
+ @market_period_end = market_period_end
74
+ @static_import_cents_per_kwh = static_import_cents_per_kwh
75
+ @controlled_load_cents_per_kwh = controlled_load_cents_per_kwh
76
+ @export_cents_per_kwh = export_cents_per_kwh
77
+ end
78
+
79
+ def self.for(postcode: )
80
+ AmberElectric::Client.prices_for(postcode: postcode)
81
+ end
82
+
83
+ end
84
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: amber_electric
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - James Healy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tzinfo
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '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: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '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
+ description: an API client for Amber Electric (https://amberelectric.com.au)
56
+ email:
57
+ - james@yob.id.au
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - CHANGELOG
63
+ - MIT-LICENSE
64
+ - README.md
65
+ - lib/amber_electric.rb
66
+ homepage: http://github.com/yob/amber_electric
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '2.2'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.2.3
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: an API client for Amber Electric (https://amberelectric.com.au)
89
+ test_files: []