All Files
(100.0%
covered at
4.42
hits/line)
6 files in total.
181 relevant lines.
181 lines covered and
0 lines missed
-
1
require 'nokogiri'
-
1
require 'net/http'
-
1
require 'yaml'
-
-
1
module EpnApi
-
-
1
autoload :Paper, "epn_api/paper"
-
1
autoload :Api, "epn_api/api"
-
1
autoload :ApiDoc, "epn_api/api_doc"
-
-
-
-
end
-
1
module EpnApi
-
1
module Api
-
-
-
1
def uri_builder( request_xml )
-
14
uri = URI.parse("http://calculator.environmentalpaper.org/v0/xml")
-
-
14
url_value = URI.escape( request_xml )
-
14
url_query = "papers=#{url_value}"
-
-
14
encoded_url = URI::HTTP.build({:host => "calculator.environmentalpaper.org", :path => "/v0/xml", :query => url_query})
-
14
return encoded_url
-
end
-
-
1
def get_epn_response_doc( request_xml )
-
7
encoded_url = self.uri_builder( request_xml )
-
7
Net::HTTP.get_response( encoded_url )
-
end
-
-
1
def accept_status( response_doc )
-
5
if (response_doc.inspect =~ /400/) then raise "HTTP Code 400 | Bad Request | The request cannot be fulfilled due to bad syntax."
-
4
elsif (response_doc.inspect =~ /500/) then raise "HTTP Code 500 | Internal Server Error"
-
3
elsif (response_doc.inspect =~ /200/) then return true
-
1
else raise "Unknown HTTP Code Error"
-
end
-
end
-
-
end
-
end
-
-
# output URL
-
# "http://calculator.environmentalpaper.org/v0/xml?papers=%3C?xml%20version=%221.0%22?%3E%0A%3Cpapers%20client=%221E1SLaUlfvOKO3q5MLC01x4Ap6M%22%3E%0A%20%20%3Cgroup%20id=%220%22%20name=%22default%22%3E%0A%20%20%20%20%3Cpaper%20name=%22PaperApi%22%3E%0A%20%20%20%20%20%20%3Cgrade%3E5%3C/grade%3E%0A%20%20%20%20%20%20%3Cname%3ESupercalendered%20%28e.g.%20newspaper%20inserts%29%3C/name%3E%0A%20%20%20%20%20%20%3Cannualqp%3E10%3C/annualqp%3E%0A%20%20%20%20%20%20%3Cqpunits%20value=%22tons%22/%3E%0A%20%20%20%20%20%20%3Crecycledcontent%3E30%3C/recycledcontent%3E%0A%20%20%20%20%3C/paper%3E%0A%20%20%3C/group%3E%0A%3C/papers%3E%0A"
-
1
module EpnApi
-
1
class ApiDoc
-
1
include Api
-
-
1
attr_accessor :grade, :name, :annualqp, :recycledcontent,
-
:bod, :cod, :greenhouse_gas, :hap, :nox, :particulates,
-
:purchased_energy,:solid_waste, :so2, :net_energy, :trs,
-
:tss, :voc, :wastewater, :wood_use
-
-
-
1
TREES_PER_TON_OF_WOOD_USE = 6.93
-
-
1
def to_epn_xml( paper )
-
9
builder = Nokogiri::XML::Builder.new do |xml|
-
9
xml.papers('client' => self.get_api_code) do
-
9
xml.group('id' => '0', 'name' => "default") do
-
9
xml.paper('name' => 'PaperApi') do
-
9
xml.grade_ paper.grade
-
9
xml.name_ paper.name
-
9
xml.annualqp_ paper.annualqp['amount']
-
9
xml.qpunits('value' => "#{paper.annualqp["qpunits"]}")
-
9
xml.recycledcontent_ paper.recycledcontent
-
end
-
end
-
end
-
end
-
9
builder.to_xml
-
end
-
-
1
def get_api_code
-
10
api_code_doc = ''
-
10
api_code_doc = YAML::load_file( './config/api_code.yml' )
-
10
return api_code_doc['epn_api_code']
-
end
-
-
1
def from_epn_api(response_body)
-
3
doc = Nokogiri::HTML(response_body)
-
3
doc.xpath('//paper/grade').each do |node|
-
3
self.grade= node.text.strip
-
end
-
3
doc.xpath('//paper/name').each do |node|
-
3
self.name = node.text.strip
-
end
-
3
doc.xpath('//paper/annualqp').each do |node|
-
3
self.annualqp ||= {}
-
3
self.annualqp["amount"] = node.text.strip
-
end
-
3
doc.xpath('//paper/qpunits').each do |node|
-
3
self.annualqp ||= {}
-
3
self.annualqp["qpunits"] = node.attributes['value'].value.strip
-
end
-
3
doc.xpath('//paper/recycledcontent').each do |node|
-
3
self.recycledcontent = node.text.strip
-
end
-
3
doc.xpath('//eparam').each do |node|
-
45
node_name = node.xpath('name').text.strip
-
-
45
hash = {"unit" => node.xpath('unit').text.strip, "value" => node.xpath('value').text.strip}
-
45
self.send("#{node_name}=", hash)
-
end
-
nil
-
end
-
-
1
def check_epn( request_xml )
-
1
response_doc = self.get_epn_response_doc( request_xml )
-
1
self.accept_status( response_doc )
-
1
self.from_epn_api( response_doc.body )
-
end
-
-
1
def do_conversions!( paper )
-
2
unit = paper.unit_check
-
-
2
paper.trees = ((self.wood_use["value"].to_f/unit) * TREES_PER_TON_OF_WOOD_USE)
-
2
paper.water = (self.wastewater["value"].to_f/unit)
-
2
paper.energy = (self.net_energy["value"].to_f/unit)
-
2
paper.solid_waste = (self.solid_waste["value"].to_f/unit)
-
2
paper.greenhouse_gas = (self.greenhouse_gas["value"].to_f/unit)
-
2
paper.annualqp["amount"] = (paper.annualqp["amount"]/unit)
-
return nil
-
end
-
-
1
def epn_response!( paper )
-
1
request_xml = self.to_epn_xml( paper )
-
1
self.check_epn( request_xml )
-
1
self.do_conversions!( paper )
-
1
return paper
-
end
-
-
-
end
-
end
-
1
module EpnApi
-
1
class Paper
-
-
1
attr_accessor :grade, :name, :annualqp, :recycledcontent, :trees, :water, :energy, :solid_waste, :greenhouse_gas
-
-
1
def initialize(args)
-
21
self.grade = args[:grade] or raise "Paper needs Grade"
-
20
self.recycledcontent = args[:recycled_percent] or raise "Paper needs Recycled Percent"
-
19
self.name = args[:name] || get_name( args[:grade] )
-
19
self.annualqp= {"amount" => 100000, "qpunits" => "pounds"}
-
19
%w(trees water energy solid_waste greenhouse_gas).each do |column|
-
95
self.send("#{column}=", args[column.to_sym]) if args[column.to_sym]
-
end
-
end
-
-
1
def get_name(grade)
-
19
paper_name_hash = self.initialize_paper_hash
-
19
paper_name_hash[grade]
-
end
-
-
1
def initialize_paper_hash
-
19
paper_name_hash = {
-
1 => "Uncoated Freesheet (e.g. copy paper)" ,
-
2 => "Coated Freesheet (e.g. high-end catalog)" ,
-
3 => "Uncoated Groundwood (e.g. newsprint)" ,
-
4 => "Coated Groundwood (e.g. catalog, magazine)" ,
-
5 => "Supercalendered (e.g. newspaper inserts)" ,
-
6 => "Corrugated: Unbleached" ,
-
7 => "Corrugated: Semi-bleached" ,
-
8 => "Corrugated: Bleached" ,
-
9 => "Paperboard: Solid Bleached Sulfate (SBS)" ,
-
10 => "Paperboard: Coated Unbleached Kraft (CUK)" ,
-
11 => "Paperboard: Uncoated Bleached Kraft" ,
-
12 => "Paperboard: Uncoated Unbleached Kraft" ,
-
13 => "Paperboard: Coated Recycled Board (CRB)" ,
-
}
-
end
-
-
1
def unit_check
-
4
raise "incorrect units" if self.annualqp != {"amount" => 100000, "qpunits" => "pounds"}
-
3
return self.annualqp["amount"]
-
end
-
-
# def object_id_strip_from_string
-
# new_paper_string = self.inspect.to_s
-
# new_paper_id = self.extract_id
-
# new_paper_string.gsub( new_paper_id, '' )
-
# end
-
#
-
# def extract_id
-
# self.to_s[2..-2]
-
# end
-
-
# PUSH THIS PROCESS TO THE APP FROM THE WRAPPER, LET RAILS COMPARE
-
# def compare(existing_paper)
-
# new_paper_string = self.object_id_strip_from_string
-
# existing_paper_string = existing_paper.object_id_strip_from_string
-
# if (new_paper_string == existing_paper_string)
-
# return false
-
# else
-
# return self
-
# end
-
# end
-
-
1
def update_via_epn
-
1
api_doc = EpnApi::ApiDoc.new
-
1
api_doc.epn_response!( self )
-
1
self.to_return_hash
-
end
-
-
1
def to_return_hash
-
1
return_hash = Hash.new
-
1
%w(grade recycledcontent name trees water energy solid_waste greenhouse_gas).each do |key|
-
8
return_hash.store( key, self.send("#{key}"))
-
end
-
1
return return_hash
-
end
-
-
end
-
end
-
1
require 'spec_helper'
-
-
1
describe "Api Transaction" do
-
-
1
before(:each) do
-
7
@paper_doc = EpnApi::Paper.new(:grade => 5, :recycled_percent => 30 )
-
7
@api_doc = EpnApi::ApiDoc.new
-
7
@request_xml = @api_doc.to_epn_xml( @paper_doc )
-
end
-
-
1
it "should build the correct URI" do
-
1
test_uri = "http://calculator.environmentalpaper.org/v0/xml"
-
1
@api_doc.uri_builder(@request_xml).should be_an_instance_of(URI::HTTP)
-
end
-
-
1
it "should send the request" do
-
1
stub_request(:get, "#{@api_doc.uri_builder(@request_xml)}").to_return(:body => "Sent", :header => "200")
-
-
1
response_doc = @api_doc.get_epn_response_doc( @request_xml )
-
end
-
-
1
it "should return a response" do
-
1
response_xml = File.open("./spec/samples/response.xml")
-
1
stub_request(:get, "#{@api_doc.uri_builder(@request_xml)}").to_return(:body => response_xml, :header => "200")
-
-
1
response_doc = @api_doc.get_epn_response_doc( @request_xml )
-
1
response_doc.should be_an_instance_of(Net::HTTPOK)
-
end
-
-
1
it "should allow a 200 status to pass" do
-
1
stub_request(:get, "#{@api_doc.uri_builder(@request_xml)}").to_return(:body => "Sent", :status => 200)
-
-
1
response_doc = @api_doc.get_epn_response_doc( @request_xml )
-
1
@api_doc.accept_status( response_doc ).should be_true
-
end
-
-
1
it "should block anything else" do
-
1
stub_request(:get, "#{@api_doc.uri_builder(@request_xml)}").to_return(:body => "Sent", :status => 300)
-
-
1
response_doc = @api_doc.get_epn_response_doc( @request_xml )
-
2
lambda { @api_doc.accept_status( response_doc ) }.should raise_error
-
end
-
-
1
it "should catch 400 status" do
-
1
stub_request(:get, "#{@api_doc.uri_builder(@request_xml)}").to_return(:body => "Incorrect", :status => 400)
-
-
1
response_doc = @api_doc.get_epn_response_doc( @request_xml )
-
2
lambda { @api_doc.accept_status( response_doc ) }.should raise_error
-
end
-
-
1
it "should catch 500 status" do
-
1
stub_request(:get, "#{@api_doc.uri_builder(@request_xml)}").to_return(:body => "Error", :status => 500)
-
-
1
response_doc = @api_doc.get_epn_response_doc( @request_xml )
-
2
lambda { @api_doc.accept_status( response_doc ) }.should raise_error
-
end
-
-
end
-
1
require 'spec_helper'
-
-
1
describe "Paper" do
-
-
1
it "should choose the correct paper name from the grade" do
-
1
paper1 = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30)
-
1
(paper1.name =~ /Uncoated Freesheet/).should be_true
-
-
1
paper2 = EpnApi::Paper.new(:grade => 5, :recycled_percent => 30)
-
1
(paper2.name =~ /Supercalendered/).should be_true
-
-
1
paper3 = EpnApi::Paper.new(:grade => 10, :recycled_percent => 30)
-
1
(paper3.name =~ /CUK/).should be_true
-
end
-
-
1
it "should initialize when given a grade and recycled content" do
-
1
paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30)
-
1
paper.grade.should == 1
-
1
(paper.name =~ /Uncoated Freesheet/).should be_true
-
1
paper.annualqp.should == {"amount" => 100000, "qpunits" => "pounds"}
-
1
paper.recycledcontent.should == 30
-
end
-
-
1
it "should initialize when given a grade and recycled content and other fields" do
-
1
paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30, :trees => 1, :water => 1, :energy => 1, :solid_waste => 1, :greenhouse_gas => 1)
-
1
paper.energy.should == 1
-
1
paper.greenhouse_gas == 1
-
end
-
-
1
it "should raise an exception if no arguement is provided" do
-
2
lambda { EpnApi::Paper.new() }.should raise_error
-
end
-
-
1
it "should raise an exception if paper grade is not provided" do
-
2
lambda { EpnApi::Paper.new(:recycled_percent => 30) }.should raise_error
-
end
-
-
1
it "should raise an exception if recycled percent is not provided" do
-
2
lambda { EpnApi::Paper.new(:grade => 1) }.should raise_error
-
end
-
-
1
it "should not raise an exception if grade and recycled percent are provided" do
-
2
lambda { EpnApi::Paper.new(:grade => 1, :recycled_percent => 30) }.should_not raise_error
-
end
-
-
1
it "should retrieve the new values" do
-
1
paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30, :trees => 1, :water => 1, :energy => 1, :solid_waste => 1, :greenhouse_gas => 1)
-
1
existing_paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30, :trees => 1, :water => 1, :energy => 1, :solid_waste => 1, :greenhouse_gas => 1)
-
1
response_xml = File.open("./spec/samples/response.xml")
-
-
1
path = "http://calculator.environmentalpaper.org/v0/xml?papers=%3C?xml%20version=%221.0%22?%3E%0A%3Cpapers%20client=%221E1SLaUlfvOKO3q5MLC01x4Ap6M%22%3E%0A%20%20%3Cgroup%20id=%220%22%20name=%22default%22%3E%0A%20%20%20%20%3Cpaper%20name=%22PaperApi%22%3E%0A%20%20%20%20%20%20%3Cgrade%3E1%3C/grade%3E%0A%20%20%20%20%20%20%3Cname%3EUncoated%20Freesheet%20(e.g.%20copy%20paper)%3C/name%3E%0A%20%20%20%20%20%20%3Cannualqp%3E100000%3C/annualqp%3E%0A%20%20%20%20%20%20%3Cqpunits%20value=%22pounds%22/%3E%0A%20%20%20%20%20%20%3Crecycledcontent%3E30%3C/recycledcontent%3E%0A%20%20%20%20%3C/paper%3E%0A%20%20%3C/group%3E%0A%3C/papers%3E%0A"
-
1
stub_request(:get, path).to_return(:status => 200, :body => response_xml, :headers => {})
-
-
1
return_item = paper.update_via_epn
-
1
return_item.should be_an_instance_of(Hash)
-
1
existing_paper.energy.should_not == return_item["energy"]
-
end
-
-
1
it "should raise an exception if the unit is unacceptable" do
-
1
paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30)
-
1
paper.annualqp["qpunits"] = 'tons'
-
2
lambda { paper.unit_check }.should raise_error
-
end
-
-
1
it "should return the conversion value if the unit is acceptable" do
-
1
paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30)
-
1
paper.unit_check.should == 100000
-
end
-
-
-
# REMOVED THIS FEATURE, LET RAILS HANDLE THE COMPARING OF THE OBJECTS
-
# it "should return false if the existing and new paper values are the same using compare method" do
-
# paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30, :trees => 1, :water => 1, :energy => 1, :solid_waste => 1, :greenhouse_gas => 1)
-
# existing_paper = paper.clone
-
# paper.compare(existing_paper).should be_false
-
# end
-
-
# it "should return the new values if the existing and new paper values are the same using compare method" do
-
# paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30, :trees => 1, :water => 1, :energy => 1, :solid_waste => 1, :greenhouse_gas => 1)
-
# existing_paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30, :trees => 2, :water => 1, :energy => 1, :solid_waste => 1, :greenhouse_gas => 1)
-
# paper.compare(existing_paper).should_not be_false
-
# end
-
-
# it "should return a string without the object info" do
-
# paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30, :trees => 1, :water => 1, :energy => 1, :solid_waste => 1, :greenhouse_gas => 1)
-
# paper.object_id_strip_from_string.gsub!(paper.extract_id, '').nil?.should be_true
-
# end
-
-
# it "should return false if the existing and new paper values are the same using update_paper method" do
-
# paper = EpnApi::Paper.new(:grade => 5, :recycled_percent => 30, :trees => 1, :water => 1, :energy => 1, :solid_waste => 1, :greenhouse_gas => 1)
-
# existing_paper = paper.clone
-
# api_doc = EpnApi::ApiDoc.new
-
# request_xml = api_doc.to_epn_xml( paper )
-
# response_xml = File.open("./spec/samples/response.xml")
-
# stub_request(:get, "#{api_doc.uri_builder(request_xml)}").to_return(:body => response_xml, :header => "200")
-
#
-
# p paper
-
# p existing_paper
-
#
-
# p paper.update_paper
-
#
-
# end
-
-
-
-
-
end