epn-api 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.DS_Store +0 -0
- data/.gitignore +57 -0
- data/.rspec +3 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +34 -0
- data/api_code_sample +1 -0
- data/coverage/.resultset.json +401 -0
- data/coverage/assets/0.5.3/app.js +88 -0
- data/coverage/assets/0.5.3/fancybox/blank.gif +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_close.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_loading.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_nav_left.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_nav_right.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_e.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_n.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_ne.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_nw.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_s.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_se.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_sw.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_w.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_title_left.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_title_main.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_title_over.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_title_right.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancybox-x.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancybox-y.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancybox.png +0 -0
- data/coverage/assets/0.5.3/fancybox/jquery.fancybox-1.3.1.css +363 -0
- data/coverage/assets/0.5.3/fancybox/jquery.fancybox-1.3.1.pack.js +44 -0
- data/coverage/assets/0.5.3/favicon_green.png +0 -0
- data/coverage/assets/0.5.3/favicon_red.png +0 -0
- data/coverage/assets/0.5.3/favicon_yellow.png +0 -0
- data/coverage/assets/0.5.3/highlight.css +129 -0
- data/coverage/assets/0.5.3/highlight.pack.js +1 -0
- data/coverage/assets/0.5.3/jquery-1.6.2.min.js +18 -0
- data/coverage/assets/0.5.3/jquery.dataTables.min.js +152 -0
- data/coverage/assets/0.5.3/jquery.timeago.js +141 -0
- data/coverage/assets/0.5.3/jquery.url.js +174 -0
- data/coverage/assets/0.5.3/loading.gif +0 -0
- data/coverage/assets/0.5.3/magnify.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-icons_222222_256x240.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-icons_454545_256x240.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-icons_888888_256x240.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
- data/coverage/assets/0.5.3/smoothness/jquery-ui-1.8.4.custom.css +295 -0
- data/coverage/assets/0.5.3/stylesheet.css +383 -0
- data/coverage/index.html +2541 -0
- data/epn-api.gemspec +23 -0
- data/lib/Epn_api/api.rb +32 -0
- data/lib/Epn_api/api_doc.rb +93 -0
- data/lib/Epn_api/paper.rb +80 -0
- data/lib/Epn_api/version.rb +11 -0
- data/lib/epn-api.rb +12 -0
- data/rakefile +14 -0
- data/spec/api_doc_spec.rb +45 -0
- data/spec/api_spec.rb +58 -0
- data/spec/paper_spec.rb +107 -0
- data/spec/samples/epn_paper_types.txt +13 -0
- data/spec/samples/request.xml +11 -0
- data/spec/samples/response.xml +183 -0
- data/spec/spec_helper.rb +9 -0
- metadata +201 -0
data/epn-api.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.expand_path('../lib/epn_api/version', __FILE__)
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.add_dependency 'nokogiri', '~> 1.4'
|
7
|
+
gem.add_dependency 'multi_json', '~> 1.0.3'
|
8
|
+
gem.add_development_dependency 'rake', '~> 0.9'
|
9
|
+
gem.add_development_dependency 'rdoc', '~> 3.8'
|
10
|
+
gem.add_development_dependency 'rspec', '~> 2.6'
|
11
|
+
gem.add_development_dependency 'simplecov', '~> 0.5'
|
12
|
+
gem.add_development_dependency 'webmock', '~> 1.7'
|
13
|
+
gem.author = 'Chad Metcalf'
|
14
|
+
gem.description = %q{Ruby wrapper for the Environmental Paper Network API}
|
15
|
+
gem.email = 'chad@databasically.com'
|
16
|
+
gem.files = `git ls-files`.split("\n")
|
17
|
+
gem.homepage = 'http://github.com/databasically/epn_api'
|
18
|
+
gem.name = 'epn-api'
|
19
|
+
gem.require_paths = ['lib']
|
20
|
+
gem.summary = %q{Ruby wrapper for the Environmental Paper Network Eco Calculator API}
|
21
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
gem.version = EpnApi::VERSION::STRING
|
23
|
+
end
|
data/lib/Epn_api/api.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module EpnApi
|
2
|
+
module Api
|
3
|
+
|
4
|
+
|
5
|
+
def uri_builder( request_xml )
|
6
|
+
uri = URI.parse("http://calculator.environmentalpaper.org/v0/xml")
|
7
|
+
|
8
|
+
url_value = URI.escape( request_xml )
|
9
|
+
url_query = "papers=#{url_value}"
|
10
|
+
|
11
|
+
encoded_url = URI::HTTP.build({:host => "calculator.environmentalpaper.org", :path => "/v0/xml", :query => url_query})
|
12
|
+
return encoded_url
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_epn_response_doc( request_xml )
|
16
|
+
encoded_url = self.uri_builder( request_xml )
|
17
|
+
Net::HTTP.get_response( encoded_url )
|
18
|
+
end
|
19
|
+
|
20
|
+
def accept_status( response_doc )
|
21
|
+
if (response_doc.inspect =~ /400/) then raise "HTTP Code 400 | Bad Request | The request cannot be fulfilled due to bad syntax."
|
22
|
+
elsif (response_doc.inspect =~ /500/) then raise "HTTP Code 500 | Internal Server Error"
|
23
|
+
elsif (response_doc.inspect =~ /200/) then return true
|
24
|
+
else raise "Unknown HTTP Code Error"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# output URL
|
32
|
+
# "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"
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module EpnApi
|
2
|
+
class ApiDoc
|
3
|
+
include Api
|
4
|
+
|
5
|
+
attr_accessor :grade, :name, :annualqp, :recycledcontent,
|
6
|
+
:bod, :cod, :greenhouse_gas, :hap, :nox, :particulates,
|
7
|
+
:purchased_energy,:solid_waste, :so2, :net_energy, :trs,
|
8
|
+
:tss, :voc, :wastewater, :wood_use
|
9
|
+
|
10
|
+
|
11
|
+
TREES_PER_TON_OF_WOOD_USE = 6.93
|
12
|
+
|
13
|
+
def to_epn_xml( paper )
|
14
|
+
builder = Nokogiri::XML::Builder.new do |xml|
|
15
|
+
xml.papers('client' => self.get_api_code) do
|
16
|
+
xml.group('id' => '0', 'name' => "default") do
|
17
|
+
xml.paper('name' => 'PaperApi') do
|
18
|
+
xml.grade_ paper.grade
|
19
|
+
xml.name_ paper.name
|
20
|
+
xml.annualqp_ paper.annualqp['amount']
|
21
|
+
xml.qpunits('value' => "#{paper.annualqp["qpunits"]}")
|
22
|
+
xml.recycledcontent_ paper.recycledcontent
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
builder.to_xml
|
28
|
+
end
|
29
|
+
|
30
|
+
def get_api_code
|
31
|
+
api_code = ''
|
32
|
+
File.open("./api_code", "r") { |f|
|
33
|
+
api_code = f.read
|
34
|
+
}
|
35
|
+
return api_code
|
36
|
+
end
|
37
|
+
|
38
|
+
def from_epn_api(response_body)
|
39
|
+
doc = Nokogiri::HTML(response_body)
|
40
|
+
doc.xpath('//paper/grade').each do |node|
|
41
|
+
self.grade= node.text.strip
|
42
|
+
end
|
43
|
+
doc.xpath('//paper/name').each do |node|
|
44
|
+
self.name = node.text.strip
|
45
|
+
end
|
46
|
+
doc.xpath('//paper/annualqp').each do |node|
|
47
|
+
self.annualqp ||= {}
|
48
|
+
self.annualqp["amount"] = node.text.strip
|
49
|
+
end
|
50
|
+
doc.xpath('//paper/qpunits').each do |node|
|
51
|
+
self.annualqp ||= {}
|
52
|
+
self.annualqp["qpunits"] = node.attributes['value'].value.strip
|
53
|
+
end
|
54
|
+
doc.xpath('//paper/recycledcontent').each do |node|
|
55
|
+
self.recycledcontent = node.text.strip
|
56
|
+
end
|
57
|
+
doc.xpath('//eparam').each do |node|
|
58
|
+
node_name = node.xpath('name').text.strip
|
59
|
+
|
60
|
+
hash = {"unit" => node.xpath('unit').text.strip, "value" => node.xpath('value').text.strip}
|
61
|
+
self.send("#{node_name}=", hash)
|
62
|
+
end
|
63
|
+
nil
|
64
|
+
end
|
65
|
+
|
66
|
+
def check_epn( request_xml )
|
67
|
+
response_doc = self.get_epn_response_doc( request_xml )
|
68
|
+
self.accept_status( response_doc )
|
69
|
+
self.from_epn_api( response_doc.body )
|
70
|
+
end
|
71
|
+
|
72
|
+
def do_conversions!( paper )
|
73
|
+
unit = paper.unit_check
|
74
|
+
|
75
|
+
paper.trees = ((self.wood_use["value"].to_f/unit) * TREES_PER_TON_OF_WOOD_USE)
|
76
|
+
paper.water = (self.wastewater["value"].to_f/unit)
|
77
|
+
paper.energy = (self.net_energy["value"].to_f/unit)
|
78
|
+
paper.solid_waste = (self.solid_waste["value"].to_f/unit)
|
79
|
+
paper.greenhouse_gas = (self.greenhouse_gas["value"].to_f/unit)
|
80
|
+
paper.annualqp["amount"] = (paper.annualqp["amount"]/unit)
|
81
|
+
return nil
|
82
|
+
end
|
83
|
+
|
84
|
+
def epn_response!( paper )
|
85
|
+
request_xml = self.to_epn_xml( paper )
|
86
|
+
self.check_epn( request_xml )
|
87
|
+
self.do_conversions!( paper )
|
88
|
+
return paper
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module EpnApi
|
2
|
+
class Paper
|
3
|
+
|
4
|
+
attr_accessor :grade, :name, :annualqp, :recycledcontent, :trees, :water, :energy, :solid_waste, :greenhouse_gas
|
5
|
+
|
6
|
+
def initialize(args)
|
7
|
+
self.grade = args[:grade] or raise "Paper needs Grade"
|
8
|
+
self.recycledcontent = args[:recycled_percent] or raise "Paper needs Recycled Percent"
|
9
|
+
self.name = args[:name] || get_name( args[:grade] )
|
10
|
+
self.annualqp= {"amount" => 100000, "qpunits" => "pounds"}
|
11
|
+
%w(trees water energy solid_waste greenhouse_gas).each do |column|
|
12
|
+
self.send("#{column}=", args[column.to_sym]) if args[column.to_sym]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_name(grade)
|
17
|
+
paper_name_hash = self.initialize_paper_hash
|
18
|
+
paper_name_hash[grade]
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize_paper_hash
|
22
|
+
paper_name_hash = {
|
23
|
+
1 => "Uncoated Freesheet (e.g. copy paper)" ,
|
24
|
+
2 => "Coated Freesheet (e.g. high-end catalog)" ,
|
25
|
+
3 => "Uncoated Groundwood (e.g. newsprint)" ,
|
26
|
+
4 => "Coated Groundwood (e.g. catalog, magazine)" ,
|
27
|
+
5 => "Supercalendered (e.g. newspaper inserts)" ,
|
28
|
+
6 => "Corrugated: Unbleached" ,
|
29
|
+
7 => "Corrugated: Semi-bleached" ,
|
30
|
+
8 => "Corrugated: Bleached" ,
|
31
|
+
9 => "Paperboard: Solid Bleached Sulfate (SBS)" ,
|
32
|
+
10 => "Paperboard: Coated Unbleached Kraft (CUK)" ,
|
33
|
+
11 => "Paperboard: Uncoated Bleached Kraft" ,
|
34
|
+
12 => "Paperboard: Uncoated Unbleached Kraft" ,
|
35
|
+
13 => "Paperboard: Coated Recycled Board (CRB)" ,
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
def unit_check
|
40
|
+
raise "incorrect units" if self.annualqp != {"amount" => 100000, "qpunits" => "pounds"}
|
41
|
+
return self.annualqp["amount"]
|
42
|
+
end
|
43
|
+
|
44
|
+
# def object_id_strip_from_string
|
45
|
+
# new_paper_string = self.inspect.to_s
|
46
|
+
# new_paper_id = self.extract_id
|
47
|
+
# new_paper_string.gsub( new_paper_id, '' )
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# def extract_id
|
51
|
+
# self.to_s[2..-2]
|
52
|
+
# end
|
53
|
+
|
54
|
+
# PUSH THIS PROCESS TO THE APP FROM THE WRAPPER, LET RAILS COMPARE
|
55
|
+
# def compare(existing_paper)
|
56
|
+
# new_paper_string = self.object_id_strip_from_string
|
57
|
+
# existing_paper_string = existing_paper.object_id_strip_from_string
|
58
|
+
# if (new_paper_string == existing_paper_string)
|
59
|
+
# return false
|
60
|
+
# else
|
61
|
+
# return self
|
62
|
+
# end
|
63
|
+
# end
|
64
|
+
|
65
|
+
def update_via_epn
|
66
|
+
api_doc = EpnApi::ApiDoc.new
|
67
|
+
api_doc.epn_response!( self )
|
68
|
+
self.to_return_hash
|
69
|
+
end
|
70
|
+
|
71
|
+
def to_return_hash
|
72
|
+
return_hash = Hash.new
|
73
|
+
%w(grade recycledcontent name trees water energy solid_waste greenhouse_gas).each do |key|
|
74
|
+
return_hash.store( key, self.send("#{key}"))
|
75
|
+
end
|
76
|
+
return return_hash
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
data/lib/epn-api.rb
ADDED
data/rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Api_Doc" do
|
4
|
+
|
5
|
+
it "should retrieve the API_code" do
|
6
|
+
api_doc = EpnApi::ApiDoc.new
|
7
|
+
api_doc.get_api_code.should_not be_empty
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should convert to XML doc" do
|
11
|
+
f = File.open("./spec/samples/request.xml")
|
12
|
+
sample_request_xml = Nokogiri::XML(f)
|
13
|
+
f.close
|
14
|
+
|
15
|
+
paper_doc = EpnApi::Paper.new(:grade => 5, :recycled_percent => 30 )
|
16
|
+
|
17
|
+
api_doc = EpnApi::ApiDoc.new
|
18
|
+
request_xml = api_doc.to_epn_xml( paper_doc )
|
19
|
+
request_xml.should == "#{sample_request_xml}"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should parse response XML to an Api_Doc" do
|
23
|
+
sample = {"grade"=>"5", "name"=>"Supercalendered", "annualqp"=>{"amount"=>"10", "qpunits"=>"tons"}, "recycledcontent"=>"30", "bod"=>{"unit"=>"pounds", "value"=>"65"}, "cod"=>{"unit"=>"pounds", "value"=>"139"}, "greenhouse_gas"=>{"unit"=>"pounds CO2 equiv.", "value"=>"59227"}, "hap"=>{"unit"=>"pounds", "value"=>"20"}, "nox"=>{"unit"=>"pounds", "value"=>"104"}, "particulates"=>{"unit"=>"pounds", "value"=>"48"}, "purchased_energy"=>{"unit"=>"million BTU's", "value"=>"256"}, "solid_waste"=>{"unit"=>"pounds", "value"=>"18604"}, "so2"=>{"unit"=>"pounds", "value"=>"293"}, "net_energy"=>{"unit"=>"million BTU's", "value"=>"299"}, "trs"=>{"unit"=>"pounds", "value"=>"3"}, "tss"=>{"unit"=>"pounds", "value"=>"103"}, "voc"=>{"unit"=>"pounds", "value"=>"20"}, "wastewater"=>{"unit"=>"gallons", "value"=>"126569"}, "wood_use"=>{"unit"=>"tons", "value"=>"19"}}
|
24
|
+
|
25
|
+
response_xml = File.open("./spec/samples/response.xml")
|
26
|
+
api_doc = EpnApi::ApiDoc.new
|
27
|
+
api_doc.from_epn_api( response_xml )
|
28
|
+
|
29
|
+
api_doc.grade.should == sample["grade"]
|
30
|
+
api_doc.greenhouse_gas.should == sample["greenhouse_gas"]
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should convert to per pounds of paper" do
|
35
|
+
paper = EpnApi::Paper.new(:grade => 5, :recycled_percent => 30, :trees => 1, :water => 1, :energy => 1, :solid_waste => 1, :greenhouse_gas => 1)
|
36
|
+
response_xml = File.open("./spec/samples/response.xml")
|
37
|
+
api_doc = EpnApi::ApiDoc.new
|
38
|
+
api_doc.from_epn_api( response_xml )
|
39
|
+
|
40
|
+
api_doc.do_conversions!( paper )
|
41
|
+
paper.greenhouse_gas.should == 0.59227
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
data/spec/api_spec.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Api Transaction" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@paper_doc = EpnApi::Paper.new(:grade => 5, :recycled_percent => 30 )
|
7
|
+
@api_doc = EpnApi::ApiDoc.new
|
8
|
+
@request_xml = @api_doc.to_epn_xml( @paper_doc )
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should build the correct URI" do
|
12
|
+
test_uri = "http://calculator.environmentalpaper.org/v0/xml"
|
13
|
+
@api_doc.uri_builder(@request_xml).should be_an_instance_of(URI::HTTP)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should send the request" do
|
17
|
+
stub_request(:get, "#{@api_doc.uri_builder(@request_xml)}").to_return(:body => "Sent", :header => "200")
|
18
|
+
|
19
|
+
response_doc = @api_doc.get_epn_response_doc( @request_xml )
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return a response" do
|
23
|
+
response_xml = File.open("./spec/samples/response.xml")
|
24
|
+
stub_request(:get, "#{@api_doc.uri_builder(@request_xml)}").to_return(:body => response_xml, :header => "200")
|
25
|
+
|
26
|
+
response_doc = @api_doc.get_epn_response_doc( @request_xml )
|
27
|
+
response_doc.should be_an_instance_of(Net::HTTPOK)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should allow a 200 status to pass" do
|
31
|
+
stub_request(:get, "#{@api_doc.uri_builder(@request_xml)}").to_return(:body => "Sent", :status => 200)
|
32
|
+
|
33
|
+
response_doc = @api_doc.get_epn_response_doc( @request_xml )
|
34
|
+
@api_doc.accept_status( response_doc ).should be_true
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should block anything else" do
|
38
|
+
stub_request(:get, "#{@api_doc.uri_builder(@request_xml)}").to_return(:body => "Sent", :status => 300)
|
39
|
+
|
40
|
+
response_doc = @api_doc.get_epn_response_doc( @request_xml )
|
41
|
+
lambda { @api_doc.accept_status( response_doc ) }.should raise_error
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should catch 400 status" do
|
45
|
+
stub_request(:get, "#{@api_doc.uri_builder(@request_xml)}").to_return(:body => "Incorrect", :status => 400)
|
46
|
+
|
47
|
+
response_doc = @api_doc.get_epn_response_doc( @request_xml )
|
48
|
+
lambda { @api_doc.accept_status( response_doc ) }.should raise_error
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should catch 500 status" do
|
52
|
+
stub_request(:get, "#{@api_doc.uri_builder(@request_xml)}").to_return(:body => "Error", :status => 500)
|
53
|
+
|
54
|
+
response_doc = @api_doc.get_epn_response_doc( @request_xml )
|
55
|
+
lambda { @api_doc.accept_status( response_doc ) }.should raise_error
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
data/spec/paper_spec.rb
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Paper" do
|
4
|
+
|
5
|
+
it "should choose the correct paper name from the grade" do
|
6
|
+
paper1 = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30)
|
7
|
+
(paper1.name =~ /Uncoated Freesheet/).should be_true
|
8
|
+
|
9
|
+
paper2 = EpnApi::Paper.new(:grade => 5, :recycled_percent => 30)
|
10
|
+
(paper2.name =~ /Supercalendered/).should be_true
|
11
|
+
|
12
|
+
paper3 = EpnApi::Paper.new(:grade => 10, :recycled_percent => 30)
|
13
|
+
(paper3.name =~ /CUK/).should be_true
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should initialize when given a grade and recycled content" do
|
17
|
+
paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30)
|
18
|
+
paper.grade.should == 1
|
19
|
+
(paper.name =~ /Uncoated Freesheet/).should be_true
|
20
|
+
paper.annualqp.should == {"amount" => 100000, "qpunits" => "pounds"}
|
21
|
+
paper.recycledcontent.should == 30
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should initialize when given a grade and recycled content and other fields" do
|
25
|
+
paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30, :trees => 1, :water => 1, :energy => 1, :solid_waste => 1, :greenhouse_gas => 1)
|
26
|
+
paper.energy.should == 1
|
27
|
+
paper.greenhouse_gas == 1
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should raise an exception if no arguement is provided" do
|
31
|
+
lambda { EpnApi::Paper.new() }.should raise_error
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should raise an exception if paper grade is not provided" do
|
35
|
+
lambda { EpnApi::Paper.new(:recycled_percent => 30) }.should raise_error
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should raise an exception if recycled percent is not provided" do
|
39
|
+
lambda { EpnApi::Paper.new(:grade => 1) }.should raise_error
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should not raise an exception if grade and recycled percent are provided" do
|
43
|
+
lambda { EpnApi::Paper.new(:grade => 1, :recycled_percent => 30) }.should_not raise_error
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should retrieve the new values" do
|
47
|
+
paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30, :trees => 1, :water => 1, :energy => 1, :solid_waste => 1, :greenhouse_gas => 1)
|
48
|
+
existing_paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30, :trees => 1, :water => 1, :energy => 1, :solid_waste => 1, :greenhouse_gas => 1)
|
49
|
+
response_xml = File.open("./spec/samples/response.xml")
|
50
|
+
|
51
|
+
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"
|
52
|
+
stub_request(:get, path).to_return(:status => 200, :body => response_xml, :headers => {})
|
53
|
+
|
54
|
+
return_item = paper.update_via_epn
|
55
|
+
return_item.should be_an_instance_of(Hash)
|
56
|
+
existing_paper.energy.should_not == return_item["energy"]
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should raise an exception if the unit is unacceptable" do
|
60
|
+
paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30)
|
61
|
+
paper.annualqp["qpunits"] = 'tons'
|
62
|
+
lambda { paper.unit_check }.should raise_error
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should return the conversion value if the unit is acceptable" do
|
66
|
+
paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30)
|
67
|
+
paper.unit_check.should == 100000
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
# REMOVED THIS FEATURE, LET RAILS HANDLE THE COMPARING OF THE OBJECTS
|
72
|
+
# it "should return false if the existing and new paper values are the same using compare method" do
|
73
|
+
# paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30, :trees => 1, :water => 1, :energy => 1, :solid_waste => 1, :greenhouse_gas => 1)
|
74
|
+
# existing_paper = paper.clone
|
75
|
+
# paper.compare(existing_paper).should be_false
|
76
|
+
# end
|
77
|
+
|
78
|
+
# it "should return the new values if the existing and new paper values are the same using compare method" do
|
79
|
+
# paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30, :trees => 1, :water => 1, :energy => 1, :solid_waste => 1, :greenhouse_gas => 1)
|
80
|
+
# existing_paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30, :trees => 2, :water => 1, :energy => 1, :solid_waste => 1, :greenhouse_gas => 1)
|
81
|
+
# paper.compare(existing_paper).should_not be_false
|
82
|
+
# end
|
83
|
+
|
84
|
+
# it "should return a string without the object info" do
|
85
|
+
# paper = EpnApi::Paper.new(:grade => 1, :recycled_percent => 30, :trees => 1, :water => 1, :energy => 1, :solid_waste => 1, :greenhouse_gas => 1)
|
86
|
+
# paper.object_id_strip_from_string.gsub!(paper.extract_id, '').nil?.should be_true
|
87
|
+
# end
|
88
|
+
|
89
|
+
# it "should return false if the existing and new paper values are the same using update_paper method" do
|
90
|
+
# paper = EpnApi::Paper.new(:grade => 5, :recycled_percent => 30, :trees => 1, :water => 1, :energy => 1, :solid_waste => 1, :greenhouse_gas => 1)
|
91
|
+
# existing_paper = paper.clone
|
92
|
+
# api_doc = EpnApi::ApiDoc.new
|
93
|
+
# request_xml = api_doc.to_epn_xml( paper )
|
94
|
+
# response_xml = File.open("./spec/samples/response.xml")
|
95
|
+
# stub_request(:get, "#{api_doc.uri_builder(request_xml)}").to_return(:body => response_xml, :header => "200")
|
96
|
+
#
|
97
|
+
# p paper
|
98
|
+
# p existing_paper
|
99
|
+
#
|
100
|
+
# p paper.update_paper
|
101
|
+
#
|
102
|
+
# end
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
value => "1" , name => "Uncoated Freesheet (e.g. copy paper)"
|
2
|
+
value => "2" , name => "Coated Freesheet (e.g. high-end catalog)"
|
3
|
+
value => "3" , name => "Uncoated Groundwood (e.g. newsprint)"
|
4
|
+
value => "4" , name => "Coated Groundwood (e.g. catalog, magazine)"
|
5
|
+
value => "5" , name => "Supercalendered (e.g. newspaper inserts)"
|
6
|
+
value => "6" , name => "Corrugated: Unbleached"
|
7
|
+
value => "7" , name => "Corrugated: Semi-bleached"
|
8
|
+
value => "8" , name => "Corrugated: Bleached"
|
9
|
+
value => "9" , name => "Paperboard: Solid Bleached Sulfate (SBS)"
|
10
|
+
value => "10" , name => "Paperboard: Coated Unbleached Kraft (CUK)"
|
11
|
+
value => "11" , name => "Paperboard: Uncoated Bleached Kraft"
|
12
|
+
value => "12" , name => "Paperboard: Uncoated Unbleached Kraft"
|
13
|
+
value => "13" , name => "Paperboard: Coated Recycled Board (CRB)"
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<papers client="1E1SLaUlfvOKO3q5MLC01x4Ap6M">
|
2
|
+
<group id="0" name="default">
|
3
|
+
<paper name="PaperApi">
|
4
|
+
<grade>5</grade>
|
5
|
+
<name>Supercalendered (e.g. newspaper inserts)</name>
|
6
|
+
<annualqp>100000</annualqp>
|
7
|
+
<qpunits value="pounds" />
|
8
|
+
<recycledcontent>30</recycledcontent>
|
9
|
+
</paper>
|
10
|
+
</group>
|
11
|
+
</papers>
|