Skip to content

Commit

Permalink
Merge pull request #2 from mejackreed/add-more
Browse files Browse the repository at this point in the history
Add more REST services
  • Loading branch information
mejackreed committed Dec 30, 2015
2 parents a456a67 + 655774f commit 510283b
Show file tree
Hide file tree
Showing 18 changed files with 300 additions and 19 deletions.
1 change: 0 additions & 1 deletion Gemfile
Expand Up @@ -5,6 +5,5 @@ gemspec

group :test do
gem 'webmock'
gem 'nokogiri'
gem 'coveralls', require: false
end
6 changes: 6 additions & 0 deletions lib/portfolio_manager/rest/api.rb
@@ -1,5 +1,8 @@
require 'portfolio_manager/rest/account'
require 'portfolio_manager/rest/building'
require 'portfolio_manager/rest/data_exchange_settings'
require 'portfolio_manager/rest/meter'
require 'portfolio_manager/rest/property'

module PortfolioManager
module REST
Expand All @@ -8,7 +11,10 @@ module REST
# @see http://portfoliomanager.energystar.gov/webservices/home/api
module API
include PortfolioManager::REST::Account
include PortfolioManager::REST::Building
include PortfolioManager::REST::DataExchangeSettings
include PortfolioManager::REST::Meter
include PortfolioManager::REST::Property
end
end
end
31 changes: 31 additions & 0 deletions lib/portfolio_manager/rest/building.rb
@@ -0,0 +1,31 @@
require 'portfolio_manager/rest/utils'

module PortfolioManager
module REST
##
# Building services
# @see http://portfoliomanager.energystar.gov/webservices/home/api/building
module Building
include PortfolioManager::REST::Utils

##
# This web service retrieves information for a specific building. The
# building must already be shared with you.
#
# @see http://portfoliomanager.energystar.gov/webservices/home/api/building/building/get
# @param [String, Integer] building_id
def building(building_id)
perform_get_request("/building/#{building_id}")
end

##
# This web service returns a list of buildings for a specific property
# that is shared with you.
#
# @see http://portfoliomanager.energystar.gov/webservices/home/api/building/buildingList/get
def building_list(property_id)
perform_get_request("/property/#{property_id}/building/list")
end
end
end
end
43 changes: 43 additions & 0 deletions lib/portfolio_manager/rest/meter.rb
@@ -0,0 +1,43 @@
require 'portfolio_manager/rest/utils'

module PortfolioManager
module REST
##
# Meter services
# @see http://portfoliomanager.energystar.gov/webservices/home/api/meter
module Meter
include PortfolioManager::REST::Utils

##
# This web service retrieves information for a specific meter. The meter
# must already be shared with you.
#
# @see http://portfoliomanager.energystar.gov/webservices/home/api/meter/meter/get
# @param [String, Integer] meter_id
def meter(meter_id)
perform_get_request("/meter/#{meter_id}")
end

##
# This web service retrieves a list of all the meters for a specific
# property. The property must already be shared with you.
#
# @see http://portfoliomanager.energystar.gov/webservices/home/api/meter/meterList/get
def meter_list(property_id)
perform_get_request("/property/#{property_id}/meter/list")
end

def metrics(property_id, year, month, measurement_system, metric)
perform_get_request(
"/property/#{property_id}/metrics",
query: {
year: year, month: month, measurementSystem: measurement_system
},
header: {
'PM-Metrics' => metric
}
)
end
end
end
end
32 changes: 32 additions & 0 deletions lib/portfolio_manager/rest/property.rb
@@ -0,0 +1,32 @@
require 'portfolio_manager/rest/utils'

module PortfolioManager
module REST
##
# Property services
# @see http://portfoliomanager.energystar.gov/webservices/home/api/property
module Property
include PortfolioManager::REST::Utils

##
# This web service returns a list of properties for a specific customer
# that are shared with you.
#
# @see http://portfoliomanager.energystar.gov/webservices/home/api/property/propertyList/get
# @param [String, Integer] account_id
def property_list(account_id)
perform_get_request("/account/#{account_id}/property/list")
end

##
# This web service retrieves information for a specific property. The
# property must already be shared with you. This service can also be used
# for to retrieve information on a building.
#
# @see http://portfoliomanager.energystar.gov/webservices/home/api/property/property/get
def property(property_id)
perform_get_request("/property/#{property_id}")
end
end
end
end
35 changes: 31 additions & 4 deletions lib/portfolio_manager/rest/request.rb
@@ -1,4 +1,5 @@
require 'hurley'
require 'nori'

module PortfolioManager
module REST
Expand All @@ -9,31 +10,57 @@ class Request
LIVE_PATH = '/ws'
TEST_PATH = '/wstest'

attr_reader :client, :path, :request_method
attr_reader :client, :path, :request_method, :parser
attr_accessor :options
##
# @param [PortfolioManager::Client] client
# @param [Symbol, String] request_method
# @param [String] path
# @param [Hash] options
# @param [Hash] options used for creating query params and headers
def initialize(client, request_method, path, options)
@client = client
@path = path
@options = options
@request_method = request_method
@conn = Hurley::Client.new(BASE_URL)
@parser = Nori.new
setup_client
end

##
# @return [String]
# @return [Hash]
def perform
@conn.public_send(request_method, api_environment + path).body
parser.parse(response_body)
end

private

##
# @return [String]
def response_body
@conn.public_send(request_method, api_environment + path).body
end

def setup_client
set_header
set_query
set_basic_authentication
end

def set_header
@conn.header[:user_agent] = 'Ruby PortfolioManager API Client'
options[:header].each do |key, value|
@conn.header[key] = value
end unless options[:header].nil?
end

def set_query
options[:query].each do |key, value|
@conn.query[key] = value
end unless options[:query].nil?
end

def set_basic_authentication
@conn.url.user = client.username
@conn.url.password = client.password
end
Expand Down
2 changes: 2 additions & 0 deletions portfolio_manager.gemspec
Expand Up @@ -20,6 +20,8 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']

spec.add_dependency 'hurley'
spec.add_dependency 'nokogiri'
spec.add_dependency 'nori'

spec.add_development_dependency 'bundler', '~> 1.10'
spec.add_development_dependency 'rake', '~> 10.0'
Expand Down
24 changes: 24 additions & 0 deletions spec/fixtures/building.xml
@@ -0,0 +1,24 @@
<building>
<name>8041- Richmond Road - Score 58</name>
<address address1="5300 Richmond Road" city="Bedford Heights" postalCode="44146" state="OH" country="US"/>
<constructionStatus>Existing</constructionStatus>
<primaryFunction>Refrigerated Warehouse</primaryFunction>
<yearBuilt>1992</yearBuilt>
<grossFloorArea units="Square Feet" temporary="false">
<value>856655</value>
</grossFloorArea>
<occupancyPercentage>10</occupancyPercentage>
<isFederalProperty>true</isFederalProperty>
<agency name="Advisory Council on Historic Preservation (ACHP)" code="ACHP" id="1" country="US"/>
<agencyDepartmentRegion>region update</agencyDepartmentRegion>
<federalCampus>campus update</federalCampus>
<notes>Notes update</notes>
<audit>
<createdBy>DUNAYT</createdBy>
<createdByAccountId>-14</createdByAccountId>
<createdDate>2012-08-16T17:04:57-04:00</createdDate>
<lastUpdatedBy>DUNAYT</lastUpdatedBy>
<lastUpdatedByAccountId>-14</lastUpdatedByAccountId>
<lastUpdatedDate>2012-08-16T17:09:35-04:00</lastUpdatedDate>
</audit>
</building>
7 changes: 7 additions & 0 deletions spec/fixtures/building_list.xml
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response status="Ok">
<links>
<link httpMethod="GET" link="/building/86" linkDescription="This is the GET url for this Building." hint="ACME Convenience Store"/>
<link httpMethod="GET" link="/building/34" linkDescription="This is the GET url for this Building." hint="ACME Grocery"/>
</links>
</response>
18 changes: 18 additions & 0 deletions spec/fixtures/meter.xml
@@ -0,0 +1,18 @@
<meter>
<id>543</id>
<type>Electric</type>
<name>Electric Main Meter</name>
<unitOfMeasure>kBtu (thousand Btu)</unitOfMeasure>
<metered>true</metered>
<firstBillDate>2010-01-01</firstBillDate>
<inUse>true</inUse>
<accessLevel>Read</accessLevel>
<audit>
<createdBy>DUNAYT</createdBy>
<createdByAccountId>-14</createdByAccountId>
<createdDate>2012-08-16T17:04:57-04:00</createdDate>
<lastUpdatedBy>DUNAYT</lastUpdatedBy>
<lastUpdatedByAccountId>-14</lastUpdatedByAccountId>
<lastUpdatedDate>2012-08-16T17:09:35-04:00</lastUpdatedDate>
</audit>
</meter>
6 changes: 6 additions & 0 deletions spec/fixtures/meter_list.xml
@@ -0,0 +1,6 @@
<response status="Ok">
<links>
<link httpMethod="GET" link="/meter/41" linkDescription="This is the GET url for this Meter." hint="Electric Main Meter"/>
<link httpMethod="GET" link="/meter/23" linkDescription="This is the GET url for this Meter." hint="Water Main Meter"/>
</links>
</response>
6 changes: 6 additions & 0 deletions spec/fixtures/property_list.xml
@@ -0,0 +1,6 @@
<response status="Ok">
<links>
<link id="86" httpMethod="GET" link="/building/86" linkDescription="This is the GET url for this Building." hint="ACME Convenience Store"/>
<link id="34" httpMethod="GET" link="/building/34" linkDescription="This is the GET url for this Building." hint="ACME Grocery"/>
</links>
</response>
15 changes: 14 additions & 1 deletion spec/lib/portfolio_manager/rest/account_spec.rb
Expand Up @@ -7,7 +7,20 @@
stub_get('/account').to_return(body: fixture('account.xml'))
end
it 'returns an account element' do
expect(client.account).to have_xpath('/account')
expect(client.account['account'])
.to include 'id', 'contact', 'organization'
end
end
describe '#property_list' do
let(:id) { 680_01 }
before do
stub_get("/account/#{id}/property/list")
.to_return(body: fixture('property_list.xml'))
end
it 'returns a list of properties' do
client.property_list(id)['response']['links']['link'].each do |link|
expect(link).to include '@id', '@link'
end
end
end
end
28 changes: 28 additions & 0 deletions spec/lib/portfolio_manager/rest/building_spec.rb
@@ -0,0 +1,28 @@
require 'spec_helper'

describe PortfolioManager::REST::Building do
let(:client) { test_client }
describe '#building_list' do
let(:id) { 680_01 }
before do
stub_get("/property/#{id}/building/list")
.to_return(body: fixture('building_list.xml'))
end
it 'returns a list of buidings' do
client.building_list(id)['response']['links']['link'].each do |link|
expect(link).to include '@link'
end
end
end
describe '#building' do
let(:id) { 680_01 }
before do
stub_get("/building/#{id}")
.to_return(body: fixture('building.xml'))
end
it 'returns a building' do
expect(client.building(id)['building'])
.to include 'name', 'address', 'constructionStatus', 'yearBuilt', 'grossFloorArea', 'occupancyPercentage'
end
end
end
13 changes: 7 additions & 6 deletions spec/lib/portfolio_manager/rest/data_exchange_settings_spec.rb
Expand Up @@ -8,8 +8,8 @@
.to_return(body: fixture('data_exchange_settings.xml'))
end
it 'returns an dataExchangeSettings element' do
expect(client.data_exchange_settings)
.to have_xpath('/dataExchangeSettings')
expect(client.data_exchange_settings['dataExchangeSettings'])
.to include 'termsOfUse', 'supportedMeterTypes'
end
end
describe '#data_exchange_custom_field_list' do
Expand All @@ -18,10 +18,11 @@
.to_return(body: fixture('data_exchange_custom_field_list.xml'))
end
it 'returns a response element and links' do
expect(client.data_exchange_custom_field_list)
.to have_xpath('/response')
expect(client.data_exchange_custom_field_list)
.to have_xpath('/response/links/link')
client
.data_exchange_custom_field_list['response']['links']['link']
.each do |link|
expect(link).to include '@link'
end
end
end
end
28 changes: 28 additions & 0 deletions spec/lib/portfolio_manager/rest/meter_spec.rb
@@ -0,0 +1,28 @@
require 'spec_helper'

describe PortfolioManager::REST::Meter do
let(:client) { test_client }
describe '#meter' do
let(:id) { 543 }
before do
stub_get("/meter/#{id}")
.to_return(body: fixture('meter.xml'))
end
it 'returns a meter' do
expect(client.meter(id)['meter'])
.to include 'id', 'type', 'name', 'unitOfMeasure', 'metered', 'firstBillDate'
end
end
describe '#meter_list' do
let(:id) { 680_01 }
before do
stub_get("/property/#{id}/meter/list")
.to_return(body: fixture('meter_list.xml'))
end
it 'returns a list of meters' do
client.meter_list(id)['response']['links']['link'].each do |link|
expect(link).to include '@link'
end
end
end
end

0 comments on commit 510283b

Please sign in to comment.