Permalink
Browse files

adds config file

adds cache ability
adds usage instructions to readme
  • Loading branch information...
1 parent 49465cd commit fc66304e5b5ec257f76a1cdaf7afce70358e00e6 @adamzaninovich committed Nov 19, 2011
Showing with 65 additions and 31 deletions.
  1. +1 −0 .gitignore
  2. +12 −6 README.md
  3. +9 −7 app.rb
  4. 0 cache/.gitkeep
  5. +9 −0 config/config.yml
  6. +34 −18 lib/dras.rb
View
1 .gitignore
@@ -1 +1,2 @@
.rvmrc
+cache/**.xml
View
18 README.md
@@ -1,8 +1,6 @@
-Akuacom DRAS OpenADR Client
-===========================
-REST Web Service
-----------------
-Release 1.5, 15 July 2011
+Akuacom DRAS OpenADR Ruby Client
+================================
+Release 1.0, 18 November 2011, by Adam Zaninovich
Introduction
------------
@@ -63,4 +61,12 @@ If the returned XML was empty, all relays would be turned off.
Example Ruby Client
-------------------
-See a demo of this running at [drasdemo.heroku.com](http://drasdemo.heroku.com/).
+See a demo of this running at [drasdemo.heroku.com](http://drasdemo.heroku.com/).
+
+To Use:
+
+* udpate config/config.yml with your credentials
+* run `gem install bundler`
+* run `bundle install`
+* run `bundle exec thin start -p 3000`
+* open localhost:3000 in your browser
View
16 app.rb
@@ -1,14 +1,15 @@
require 'sinatra'
+require 'yaml'
require File.dirname(__FILE__) + '/lib/dras'
class DrasDemo < Sinatra::Base
def initialize(*args)
super(*args)
- @dras = DRAS.new "cdp.openadr.com",
- "/RestClientWS/rest2",
- "/RestClientWS/restConfirm",
- ssl:true, auth: ['akua.client1','Test_1234']
+
+ @config = YAML.load(File.read(File.dirname(__FILE__) + '/config/config.yml'))['dras']
+
+ @dras = DRAS.new @config
end
get '/' do
@@ -22,9 +23,10 @@ def initialize(*args)
"a:hover{color: #eb0077;text-decoration: none;background: #181818;}" +
"</style></head>" +
"<body><h1>DRAS Demo</h1>" +
- "<p>End point: #{(@dras.ssl ? 'https://' : 'http://')}#{@dras.site}#{@dras.endpoint}</p>" +
- "<p>Last result: #{@dras.operation_mode_value} operation was #{@dras.event_status} at #{@dras.last_check}</p>" +
- "<p><a href='/check'>check again</a></p></body></html>"
+ "<p>Account: #{@config['auth']['user']}" +
+ "<p>End point: #{(@dras.ssl ? 'https://' : 'http://')}#{@dras.site}:#{@dras.port}#{@dras.endpoint}</p>" +
+ "<p>Last result: #{@dras.operation_mode_value} operation was #{@dras.event_status} (#{@dras.last_check})</p>" +
+ "<p><a href='/check'>udpate</a></p></body></html>"
end
get '/check' do
View
0 cache/.gitkeep
No changes.
View
9 config/config.yml
@@ -0,0 +1,9 @@
+dras:
+ site: "cdp.openadr.com"
+ endpoint: "/RestClientWS/rest2"
+ confirm_ep: "/RestClientWS/restConfirm"
+ use_ssl: true
+ cache: "cache"
+ auth:
+ user: "cfirr.test"
+ pass: "Test_1234"
View
52 lib/dras.rb
@@ -2,36 +2,45 @@
require 'net/https'
require 'nokogiri'
-# Example usage
-# dras = DRAS.new "cdp.openadr.com",
-# "/RestClientWS/rest2",
-# "/RestClientWS/restConfirm",
-# ssl:true, auth: ['akua.client1','Test_1234']
-# dras.check
-# puts "#{dras.operation_mode_value} operation was #{dras.event_status} at #{dras.last_check}"
-
# TODO: add confirmation
class DRAS
- attr_accessor :site, :endpoint, :confirmation_endpoint
- attr_reader :event_status, :operation_mode_value, :last_check, :ssl
+ attr_reader :site,:endpoint, :confirmation_endpoint, :port,
+ :event_status, :operation_mode_value, :last_check, :ssl
- def initialize(site, endpoint, confirmation_endpoint, opts={})
+ def initialize(opts)
@last_check = "NEVER"
@event_status = "NOT_SET"
@operation_mode_value = "NOT_SET"
- @site = site
- @endpoint = endpoint
- @confirmation_endpoint = confirmation_endpoint
+ @site = opts['site']
+ @endpoint = opts['endpoint']
+ @confirmation_endpoint = opts['confirm_ep']
+
+ @cache_file = "#{File.dirname(__FILE__)}/../cache/#{opts['cache']}.xml" if opts['cache']
- @auth = opts[:auth]
- @ssl = !!opts[:ssl]
- port = opts[:port].nil? ? (@ssl ? 443 : 80) : opts[:port]
+ @auth = [opts['auth']['user'],opts['auth']['pass']]
+ @ssl = !!opts['use_ssl']
+ @port = opts['port'].nil? ? (@ssl ? 443 : 80) : opts['port']
- @http = Net::HTTP.new(@site, port)
+ @http = Net::HTTP.new(@site, @port)
@http.use_ssl = @ssl
+
+ update_from_cache if @cache_file
+ end
+
+ def update_from_cache
+ if @cache_file and FileTest.file?(@cache_file)
+ file = File.open(@cache_file)
+ doc = Nokogiri::XML(file)
+ file.close
+ @event_status = doc.xpath("//p:EventStatus").first.text
+ @operation_mode_value = doc.xpath("//p:OperationModeValue").first.text
+ @last_check = 'from cache file'
+ else
+ puts "WARNING: No cache file."
+ end
end
def check
@@ -42,6 +51,13 @@ def check
doc = Nokogiri::XML(response.body)
@last_check = Time.new
+
+ if @cache_file
+ file = File.new(@cache_file, "w")
+ file.write(response.body)
+ file.close
+ end
+
@event_status = doc.xpath("//p:EventStatus").first.text
@operation_mode_value = doc.xpath("//p:OperationModeValue").first.text
end

0 comments on commit fc66304

Please sign in to comment.