Skip to content

Commit

Permalink
ruby 2esday
Browse files Browse the repository at this point in the history
  • Loading branch information
wilg committed May 2, 2015
1 parent 05e3517 commit b491ca2
Showing 1 changed file with 4 additions and 63 deletions.
67 changes: 4 additions & 63 deletions lib/metermaid/fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ module Fetcher
require 'tempfile'
require 'csv'

def self.scrape!(options = {})
def self.scrape!(username:, password:, additional_metadata: {})
base_url = "http://www.pge.com/"
agent = Mechanize.new
page = agent.get(base_url)
login_form = page.forms_with(method: "POST").first
login_form.USER = options[:username]
login_form.PASSWORD = options[:password]
login_form.USER = username
login_form.PASSWORD = password
page = agent.submit(login_form, login_form.buttons.first)
page = agent.page.link_with(:text => 'My Usage').click
page = page.forms.first.submit
Expand All @@ -22,7 +22,7 @@ def self.scrape!(options = {})
export_page = agent.get(export_form_url)
export_url_base = export_page.forms.first.action
file = agent.get("#{export_url_base}?exportFormat=ESPI_AMI&bill=&xmlFrom=04%2F18%2F2015&xmlTo=04%2F29%2F2015")
files_to_rows(decompress(file), options[:additional_metadata] || {})
files_to_rows(decompress(file), additional_metadata)
end

def self.decompress(file)
Expand Down Expand Up @@ -59,65 +59,6 @@ def self.to_rows(xml, additional_metadata = {})
end
end

def self.parse(text)
hashes = hashify_csv text
# {:type=>"Electric usage", :date=>"2012-07-25", :start_time=>"00:00", :end_time=>"00:59", :usage=>"1.31", :units=>"kWh", :notes=>nil}
samples = []
hashes.each do |item|
if item[:type] == "Electric usage"

sample = Sample.new

# Start Time
start_time = DateTime.parse "#{item[:date]} #{item[:start_time]}#{DateTime.now.zone}"
sample.start_time = start_time

# End Time
end_time = DateTime.parse "#{item[:date]} #{item[:end_time]}"
duration = (end_time.to_time - sample.start_time.to_time).to_i
sample.duration = duration == 3540 ? 3600 : duration # Opower incorrectly says that the period is 59 minutes when they actually mean 60

# Units
if item[:units] == "kWh"
sample.kwh = item[:usage].to_f
else
raise "Using unsupported unit: #{item[:units]}"
end

samples << sample

else
raise "Using unsupported type: #{item[:type]}"
end
end
SampleCollection.new(samples)
end

private

def self.hashify_csv(csv_text)
csv = CSV.parse(csv_text).to_a
start = false
header_index = nil
csv.each_with_index do |line, index|
if line == []
header_index = index + 1
break
end
end
header = csv[header_index]
csv.shift header_index + 1
items = []
csv.each do |line|
item = {}
header.each_with_index do |k, i|
item[k.to_s.downcase.gsub(" ", "_").to_sym] = line[i]
items << item
end
end
items
end

end
end
end

0 comments on commit b491ca2

Please sign in to comment.