Module: GreenButton
- Defined in:
- lib/ce-greenbutton.rb,
lib/ce-greenbutton/parser.rb,
lib/ce-greenbutton/version.rb,
lib/ce-greenbutton/elements/gb_entry.rb,
lib/ce-greenbutton/elements/gb_content.rb,
lib/ce-greenbutton/elements/gb_interval.rb,
lib/ce-greenbutton/elements/gb_data_feed.rb,
lib/ce-greenbutton/elements/gb_usage_point.rb,
lib/ce-greenbutton/elements/gb_reading_type.rb,
lib/ce-greenbutton/elements/gb_interval_block.rb,
lib/ce-greenbutton/elements/gb_interval_reading.rb,
lib/ce-greenbutton/elements/gb_local_time_parameters.rb,
lib/ce-greenbutton/elements/gb_application_information.rb,
lib/ce-greenbutton/interpreters/electricity_interpreter.rb
Overview
The main entry of the module. provides the download_data method that will
download and parse GreenButton data.
For example:
# The following will download and parse all data for subscription
require 'ce-greenbutton'
GreenButton.config(reg_access_token: "your access token"
application_information_url: "http://app_info_url")
gb = GreenButton.download_data('your access token', 'http://greenbutton.data.url')
# => [gb_data_description]
# The following will download and parse all data for 2013
require 'date'
require 'ce-greenbutton'
GreenButton.config(reg_access_token: "your access token"
application_information_url: "http://app_info_url")
gb = GreenButton.download_data('your access token',
'http://greenbutton.data.url'), Date.new(2013,1,1), Date.new(2013,12,31))
# => [gb_data_description]
Changes for v1.1 (SunShot - Clearly Energy GreenButton Ruby Gem Update)
1. added new method download_data_ftp
2. parsing code is refactored to parse_data private method.
3. download_data method is updated to use the parse_data method.
4. config method is updated to support ftp .
5. check_config method is updated to check ftp .
6. added check_arguments_ftp method.
Author: ahmed.seddiq Version: 1.1
Defined Under Namespace
Modules: Interpreters, Parser Classes: ConfigurationError, InvalidGbDataError
Constant Summary collapse
- SERVICEKIND_NAMES =
Constant: the known data kind names mapped to the kind value,
the values are retrieved from the GreenButton xml schema. { 0 => 'electricity', 1 => 'gas', 2 => 'water', 3 => 'time', 4 => 'heat', 5 => 'refuse', 6 => 'sewerage', 7 => 'rates', 8 => 'tvLicence', 9 => 'internet' }
- VERSION =
"0.1.7"
Class Method Summary collapse
-
.download_data(access_token, subscription_id, interval_start_time = nil, interval_end_time = nil) ⇒ Object
Public: Downloads and parses the GreenButton data for the given subscription.
-
.download_data_ftp(application_id, time = Time.now, utility_name) ⇒ Object
Public: Downloads and parses the GreenButton data, hosted in a FTP server.
- .download_data_ftp_by_filename(filename) ⇒ Object
-
.interpreter(kind) ⇒ Object
Public: Gets the interpreter of the given kind.
Class Method Details
.download_data(access_token, subscription_id, interval_start_time = nil, interval_end_time = nil) ⇒ Object
Public: Downloads and parses the GreenButton data for the given
subscription. It also allows to filter returned readings by date.
access_token - represents the retail customer access token. subscription_id - represents the retail customer resourceURI. interval_start_time - represents the start date to retrieve data. interval_end_time - represents the end date to retrieve data.
Examples
# The following will download and parse all data for 2013
require 'date'
require 'ce-greenbutton'
gb = GreenButton.download_data('688b026c-665f-4994-9139-6b21b13fbeee', 5,
Date.new(2013,1,1), Date.new(2013,12,31))
# => [gb_data_description]
Returns an array of gb_data_description Raises ArgumentError if any passed argument is invalid. Propagates errors from OpenURI for any connection/authentication
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/ce-greenbutton.rb', line 87 def self.download_data(access_token, subscription_id, interval_start_time = nil, interval_end_time = nil) check_arguments(access_token, subscription_id, interval_start_time, interval_end_time) # Construct the resource url resource_url = subscription_id params = [] if interval_start_time params << "published-min=#{interval_start_time.to_s}" end if interval_end_time params << "published-max=#{interval_end_time.to_s}" end resource_url += (resource_url.index('?').nil?? '?':'&')+ params.join('&') if params.length > 0 data = open(resource_url, 'Authorization' => "Bearer #{access_token}") parse_data(data) #Return end |
.download_data_ftp(application_id, time = Time.now, utility_name) ⇒ Object
Public: Downloads and parses the GreenButton data, hosted in a FTP server. The FTP host, user, password and path is configured through the config method. The File name is constructed as follows:
D_{utility_name}_{application_id}_{YYYYMMDDHHMMSS}.XML.
application_id - represents the GreenButton 3rd party application id. time - used to construct file name. (optional, defaults
to current time)
utility_name - represents the utility name, used to construct the
XML file name.
Returns an array of gb_data_description Raises ArgumentError if any passed argument is invalid. Propagates errors from OpenURI for any connection/authentication
123 124 125 126 127 128 129 130 131 |
# File 'lib/ce-greenbutton.rb', line 123 def self.download_data_ftp(application_id, time=Time.now, utility_name) check_arguments_ftp(application_id, time, utility_name) # construct the ftp url ftp_url = "ftp://#{@ftp_user}:#{@ftp_password}@#{@ftp_host}/#{@ftp_path}/" + "D_#{utility_name}_#{application_id}_CONSUMPTION_#{time.strftime('%Y%m%d%H%M%S')}.XML" parse_data(open(ftp_url)) end |
.download_data_ftp_by_filename(filename) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/ce-greenbutton.rb', line 133 def self.download_data_ftp_by_filename(filename) ftp_url = "ftp://#{@ftp_user}:#{@ftp_password}@#{@ftp_host}/#{@ftp_path}/" + "#{filename}" parse_data(open(ftp_url)) end |
.interpreter(kind) ⇒ Object
Public: Gets the interpreter of the given kind. kind - the kind of the GreenButton Data Returns the registered interpreter for the given kind, or nil if none found.
63 64 65 |
# File 'lib/ce-greenbutton.rb', line 63 def self.interpreter(kind) @interpreters[kind] end |