module AEMO::Market

AEMO::Market

@author Joel Courtney @abstract @since 0.1.0

Public Class Methods

current_dispatch(region) click to toggle source

Return the current dispatch dataset for a region

@param [String, AEMO::Region] region AEMO::Region @return [Array<AEMO::Market::Interval>] the current dispatch data

# File lib/aemo/market.rb, line 19
def current_dispatch(region)
  region = AEMO::Region.new(region) if region.is_a?(String)

  response = get "/mms.GRAPHS/GRAPHS/GRAPH_5#{region}1.csv"
  values = parse_response(response)
  values
end
current_trading(region) click to toggle source

Description of method

@param [String, AEMO::Region] region AEMO::Region @return [Array<AEMO::Market::Interval>] the current trading data

# File lib/aemo/market.rb, line 31
def current_trading(region)
  region = AEMO::Region.new(region) if region.is_a?(String)

  response = get "/mms.GRAPHS/GRAPHS/GRAPH_30#{region}1.csv"
  values = parse_response(response)
  values
end
historic_trading(region, year, month) click to toggle source

Return an array of historic trading values for a Year, Month and Region

As per the historical data from AEMO

@param [String, AEMO::Region] region AEMO::Region @param [Integer] year The year for the report from AEMO @param [Integer] month The month for the report from AEMO @return [Array<AEMO::Market::Interval>]

# File lib/aemo/market.rb, line 67
def historic_trading(region, year, month)
  region = AEMO::Region.new(region) if region.is_a?(String)

  month = Kernel.format('%02d', month)
  url = 'https://aemo.com.au/aemo/data/nem/priceanddemand/' \
        "PRICE_AND_DEMAND_#{year}#{month}_#{region}1.csv"

  response = HTTParty.get(url)
  parse_response(response)
end
historic_trading_by_range(region, start, finish) click to toggle source

Return an array of historic trading values based on a start and finish

@param [String, AEMO::Region] region AEMO::Region @param [Time] start this is inclusive not exclusive @param [Time] finish this is inclusive not exclusive @return [Array<AEMO::Market::Interval>]

# File lib/aemo/market.rb, line 45
def historic_trading_by_range(region, start, finish)
  region = AEMO::Region.new(region) if region.is_a?(String)

  required_data = []
  (start..finish).map { |d| { year: d.year, month: d.month } }
                 .uniq.each do |period|
    required_data += historic_trading(region, period[:year],
                                      period[:month])
  end

  required_data.select do |values|
    values.datetime >= start && values.datetime <= finish
  end
end

Protected Class Methods

parse_response(response) click to toggle source
# File lib/aemo/market.rb, line 80
def parse_response(response)
  values = []
  if response.response.code == '200'
    CSV.parse(response.body, headers: true, converters: :numeric) do |row|
      if row.respond_to?(:to_h)
        row = row.to_h
      elsif row.respond_to?(:to_hash)
        row = row.to_hash
      else
        raise NoMethodError, "cannot convert #{row.class} to Hash"
      end
      values.push ::AEMO::Market::Interval.new(row['SETTLEMENTDATE'], row)
    end
  end
  values
end