Class: GreenButton::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/greenbutton.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ Parser

Returns a new instance of Parser



26
27
28
29
30
31
32
33
34
35
# File 'lib/greenbutton.rb', line 26

def initialize(doc)
    self.doc = doc
    self.usage_points = []
    doc.xpath('//UsagePoint/../..').each do |usage_point_entry|
      up = UsagePoint.new(nil, usage_point_entry.remove, self)
      if filter_usage_points(href: up.href).length == 0
        @usage_points << up
      end
    end
end

Instance Attribute Details

#docObject

Returns the value of attribute doc



24
25
26
# File 'lib/greenbutton.rb', line 24

def doc
  @doc
end

#usage_pointsObject

Returns the value of attribute usage_points



24
25
26
# File 'lib/greenbutton.rb', line 24

def usage_points
  @usage_points
end

Instance Method Details

#end_time(params = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/greenbutton.rb', line 79

def end_time(params=nil)
  if params
    ups = filtered_usage_points(params)
  else
    ups = self.usage_points
  end
  end_time = ups[0].end_time
  ups.each do |up|
    end_time = [end_time, up.end_time].max
  end
  end_time
end

#filter_usage_points(params) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/greenbutton.rb', line 37

def filter_usage_points(params)
  # customer_id, service_kind, title, id, href
  filtered = []
  self.usage_points.each do |usage_point|
    include = true
    params.each_pair do |key, value|
      if usage_point.send(key) != value
        include = false
        break
      end
    end
    filtered << usage_point if include
  end
  filtered
end

#get_unique(attr) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/greenbutton.rb', line 53

def get_unique(attr)
  #customer_id, service_kind, title
  unique = []
    self.usage_points.each do |usage_point|
      val = usage_point.send(attr)
      if !unique.include?(val)
        unique << val
      end
    end
    unique
end

#start_time(params = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/greenbutton.rb', line 65

def start_time(params=nil)
  if params
    ups = filtered_usage_points(params)
  else
    ups = self.usage_points
  end
  start_time = Time.now
  ups.each do |up|
    start_time = [start_time, up.start_time].min
  end
  start_time
end