Class: EpnApi::ApiDoc

Inherits:
Object
  • Object
show all
Includes:
Api
Defined in:
lib/epn_api/api_doc.rb

Constant Summary collapse

TREES_PER_TON_OF_WOOD_USE =
6.93

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Api

#accept_status, #get_epn_response_doc, #uri_builder

Instance Attribute Details

#annualqpObject

Returns the value of attribute annualqp.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def annualqp
  @annualqp
end

#bodObject

Returns the value of attribute bod.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def bod
  @bod
end

#codObject

Returns the value of attribute cod.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def cod
  @cod
end

#gradeObject

Returns the value of attribute grade.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def grade
  @grade
end

#greenhouse_gasObject

Returns the value of attribute greenhouse_gas.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def greenhouse_gas
  @greenhouse_gas
end

#hapObject

Returns the value of attribute hap.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def hap
  @hap
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def name
  @name
end

#net_energyObject

Returns the value of attribute net_energy.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def net_energy
  @net_energy
end

#noxObject

Returns the value of attribute nox.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def nox
  @nox
end

#particulatesObject

Returns the value of attribute particulates.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def particulates
  @particulates
end

#purchased_energyObject

Returns the value of attribute purchased_energy.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def purchased_energy
  @purchased_energy
end

#recycledcontentObject

Returns the value of attribute recycledcontent.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def recycledcontent
  @recycledcontent
end

#so2Object

Returns the value of attribute so2.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def so2
  @so2
end

#solid_wasteObject

Returns the value of attribute solid_waste.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def solid_waste
  @solid_waste
end

#trsObject

Returns the value of attribute trs.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def trs
  @trs
end

#tssObject

Returns the value of attribute tss.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def tss
  @tss
end

#vocObject

Returns the value of attribute voc.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def voc
  @voc
end

#wastewaterObject

Returns the value of attribute wastewater.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def wastewater
  @wastewater
end

#wood_useObject

Returns the value of attribute wood_use.



5
6
7
# File 'lib/epn_api/api_doc.rb', line 5

def wood_use
  @wood_use
end

Instance Method Details

#check_epn(request_xml) ⇒ Object



64
65
66
67
68
# File 'lib/epn_api/api_doc.rb', line 64

def check_epn( request_xml )
  response_doc = self.get_epn_response_doc( request_xml )
  self.accept_status( response_doc )
  self.from_epn_api( response_doc.body )
end

#do_conversions!(paper) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/epn_api/api_doc.rb', line 70

def do_conversions!( paper )
  unit = paper.unit_check
  
  paper.trees = ((self.wood_use["value"].to_f/unit) * TREES_PER_TON_OF_WOOD_USE)
  paper.water = (self.wastewater["value"].to_f/unit)
  paper.energy = (self.net_energy["value"].to_f/unit)
  paper.solid_waste = (self.solid_waste["value"].to_f/unit)
  paper.greenhouse_gas = (self.greenhouse_gas["value"].to_f/unit)
  paper.annualqp["amount"] = (paper.annualqp["amount"]/unit)
  return nil
end

#epn_response!(paper) ⇒ Object



82
83
84
85
86
87
# File 'lib/epn_api/api_doc.rb', line 82

def epn_response!( paper )
  request_xml = self.to_epn_xml( paper )
  self.check_epn( request_xml )
  self.do_conversions!( paper )
  return paper
end

#from_epn_api(response_body) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/epn_api/api_doc.rb', line 36

def from_epn_api(response_body)
  doc = Nokogiri::HTML(response_body)
  doc.xpath('//paper/grade').each do |node|
    self.grade= node.text.strip
  end
  doc.xpath('//paper/name').each do |node|
    self.name = node.text.strip
  end
  doc.xpath('//paper/annualqp').each do |node|
    self.annualqp ||= {}
    self.annualqp["amount"] = node.text.strip
  end
  doc.xpath('//paper/qpunits').each do |node|
    self.annualqp ||= {}
    self.annualqp["qpunits"] = node.attributes['value'].value.strip
  end
  doc.xpath('//paper/recycledcontent').each do |node|
    self.recycledcontent = node.text.strip
  end
  doc.xpath('//eparam').each do |node|
    node_name = node.xpath('name').text.strip

    hash = {"unit" => node.xpath('unit').text.strip, "value" => node.xpath('value').text.strip}
    self.send("#{node_name}=", hash)
  end
  nil
end

#get_api_codeObject



30
31
32
33
34
# File 'lib/epn_api/api_doc.rb', line 30

def get_api_code
  api_code_doc = ''
  api_code_doc = YAML::load_file( './config/api_code.yml' )
  return api_code_doc['epn_api_code']
end

#to_epn_xml(paper) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/epn_api/api_doc.rb', line 13

def to_epn_xml( paper )
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.papers('client' => self.get_api_code) do
      xml.group('id' => '0', 'name' => "default") do
        xml.paper('name' => 'PaperApi') do
          xml.grade_ paper.grade
          xml.name_ paper.name
          xml.annualqp_ paper.annualqp['amount']
          xml.qpunits('value' => "#{paper.annualqp["qpunits"]}")
          xml.recycledcontent_ paper.recycledcontent
        end
      end
    end
  end
  builder.to_xml
end