green-button-data 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +11 -0
  5. data/Guardfile +50 -0
  6. data/LICENSE.txt +26 -0
  7. data/README.md +72 -0
  8. data/Rakefile +5 -0
  9. data/green-button-data.gemspec +24 -0
  10. data/lib/green-button-data/core_ext/date.rb +9 -0
  11. data/lib/green-button-data/core_ext/fixnum.rb +10 -0
  12. data/lib/green-button-data/core_ext.rb +2 -0
  13. data/lib/green-button-data/dst.rb +49 -0
  14. data/lib/green-button-data/enumerations.rb +523 -0
  15. data/lib/green-button-data/feed.rb +7 -0
  16. data/lib/green-button-data/parser/application_information.rb +180 -0
  17. data/lib/green-button-data/parser/authorization.rb +45 -0
  18. data/lib/green-button-data/parser/content.rb +36 -0
  19. data/lib/green-button-data/parser/entry.rb +47 -0
  20. data/lib/green-button-data/parser/feed.rb +47 -0
  21. data/lib/green-button-data/parser/interval.rb +32 -0
  22. data/lib/green-button-data/parser/interval_block.rb +32 -0
  23. data/lib/green-button-data/parser/interval_reading.rb +30 -0
  24. data/lib/green-button-data/parser/local_time_parameters.rb +105 -0
  25. data/lib/green-button-data/parser/rational_number.rb +22 -0
  26. data/lib/green-button-data/parser/reading_type.rb +134 -0
  27. data/lib/green-button-data/parser/service_category.rb +17 -0
  28. data/lib/green-button-data/parser/usage_point.rb +20 -0
  29. data/lib/green-button-data/parser.rb +2 -0
  30. data/lib/green-button-data/utilities.rb +106 -0
  31. data/lib/green-button-data/version.rb +3 -0
  32. data/lib/green-button-data.rb +24 -0
  33. data/spec/fixtures/ESPIApplicationInformation.xml +49 -0
  34. data/spec/fixtures/ESPIAuthorization.xml +25 -0
  35. data/spec/fixtures/ESPIIntervalBlock.xml +3814 -0
  36. data/spec/fixtures/ESPILocalTimeParameters.xml +16 -0
  37. data/spec/fixtures/ESPIReadingType.xml +535 -0
  38. data/spec/fixtures/ESPIUsagePoint.xml +18 -0
  39. data/spec/fixtures.rb +22 -0
  40. data/spec/green-button-data/core_ext/date_spec.rb +18 -0
  41. data/spec/green-button-data/core_ext/fixnum_spec.rb +13 -0
  42. data/spec/green-button-data/parser/application_information_spec.rb +154 -0
  43. data/spec/green-button-data/parser/authorization_spec.rb +52 -0
  44. data/spec/green-button-data/parser/interval_block_spec.rb +39 -0
  45. data/spec/green-button-data/parser/local_time_parameter_spec.rb +56 -0
  46. data/spec/green-button-data/parser/reading_type_spec.rb +76 -0
  47. data/spec/green-button-data/parser/usage_point_spec.rb +22 -0
  48. data/spec/green-button-data/utilities_spec.rb +63 -0
  49. data/spec/spec_helper.rb +20 -0
  50. data/spec/support/custom_expectations/warn_expectation.rb +31 -0
  51. metadata +148 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9a24bc8d778aad1694ad3eb8be0bebcd066cfbd6
4
+ data.tar.gz: 4b4e442ebd295efc472eed20d4f0144184980739
5
+ SHA512:
6
+ metadata.gz: e7e9c0506c26dddce65132206f8855bc994d3636abdf4b0c5f88ec02b947a00a40bfb971be82b05e2c587c40cf1023b883d29c5d003d20cbd1d5927ef1f9dba4
7
+ data.tar.gz: 75ae8319f7b1ed2969107dc13aaf2664d287c9ffb0338274342cfb6fc6b9a772f7fa41a4de724503e46d0da27db72c9f2dd1f2bd67a34dcf879809f2a2d48084
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ .bundle
2
+ .ruby-gemset
3
+ .ruby-version
4
+ Gemfile.lock
5
+ doc/
6
+ pkg/
7
+ rdoc/
8
+ coverage/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org/'
2
+
3
+ gemspec
4
+
5
+ group :deveopment do
6
+ gem 'guard-rspec', require: false
7
+ end
8
+
9
+ group :test do
10
+ gem 'codecov', require: false
11
+ end
data/Guardfile ADDED
@@ -0,0 +1,50 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ directories %w(lib spec)
6
+
7
+ ## Uncomment to clear the screen before every task
8
+ clearing :on
9
+
10
+ ## Guard internally checks for changes in the Guardfile and exits.
11
+ ## If you want Guard to automatically start up again, run guard in a
12
+ ## shell loop, e.g.:
13
+ ##
14
+ ## $ while bundle exec guard; do echo "Restarting Guard..."; done
15
+ ##
16
+ ## Note: if you are using the `directories` clause above and you are not
17
+ ## watching the project directory ('.'), then you will want to move
18
+ ## the Guardfile to a watched dir and symlink it back, e.g.
19
+ #
20
+ # $ mkdir config
21
+ # $ mv Guardfile config/
22
+ # $ ln -s config/Guardfile .
23
+ #
24
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
25
+
26
+ # Note: The cmd option is now required due to the increasing number of ways
27
+ # rspec may be run, below are examples of the most common uses.
28
+ # * bundler: 'bundle exec rspec'
29
+ # * bundler binstubs: 'bin/rspec'
30
+ # * spring: 'bin/rspec' (This will use spring if running and you have
31
+ # installed the spring binstubs per the docs)
32
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
33
+ # * 'just' rspec: 'rspec'
34
+
35
+ guard :rspec, cmd: "bundle exec rspec" do
36
+ require "guard/rspec/dsl"
37
+ dsl = Guard::RSpec::Dsl.new(self)
38
+
39
+ # Feel free to open issues for suggestions and improvements
40
+
41
+ # RSpec files
42
+ rspec = dsl.rspec
43
+ watch(rspec.spec_helper) { rspec.spec_dir }
44
+ watch(rspec.spec_support) { rspec.spec_dir }
45
+ watch(rspec.spec_files)
46
+
47
+ # Ruby files
48
+ ruby = dsl.ruby
49
+ dsl.watch_spec_files_for(ruby.lib_files)
50
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,26 @@
1
+ Copyright (c) 2015, Verdigris Technologies Inc.
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
+
24
+ The views and conclusions contained in the software and documentation are those
25
+ of the authors and should not be interpreted as representing official policies,
26
+ either expressed or implied, of the FreeBSD Project.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # Green Button Data
2
+
3
+ [![CI Results](https://img.shields.io/circleci/project/VerdigrisTech/green-button-data.svg)](https://circleci.com/gh/VerdigrisTech/green-button-data)
4
+ [![Dependencies](https://img.shields.io/gemnasium/VerdigrisTech/green-button-data.svg)](https://gemnasium.com/VerdigrisTech/green-button-data)
5
+ [![Code Coverage](https://img.shields.io/codecov/c/github/VerdigrisTech/green-button-data.svg)](https://codecov.io/github/VerdigrisTech/green-button-data)
6
+ [![Code Climate](https://img.shields.io/codeclimate/github/VerdigrisTech/green-button-data.svg)](https://codeclimate.com/github/VerdigrisTech/green-button-data)
7
+
8
+ Green Button Data is a Ruby gem that can quickly parse the Green Button data
9
+ standard. It uses an event-driven <abbr title="Simple API for XML">SAX</abbr>
10
+ parser which does not build the entire <abbr title="Document Object Model">DOM</abbr>
11
+ in memory.
12
+
13
+ ## Getting Started
14
+
15
+ Add the Green Button Data gem to your Gemfile:
16
+
17
+ ```ruby
18
+ gem 'green-button-data'
19
+ ```
20
+
21
+ Then run Bundler:
22
+
23
+ ```bash
24
+ $ bundle
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ Almost all of the functionality for parsing data is wrapped in the
30
+ `GreenButtonData::Feed` class.
31
+
32
+ To parse a Green Button Atom feed, simply call the `parse` method and pass in
33
+ the entire XML document:
34
+
35
+ ```ruby
36
+ require 'green-button-data'
37
+
38
+ xml_text = File.read "#{File.dirname __FILE__}/gb_example_interval_block.xml"
39
+ data = GreenButtonData::Feed.parse xml_text
40
+ ```
41
+
42
+ You can then access the data like this:
43
+
44
+ ```ruby
45
+ # Print all the interval readings from the feed
46
+ data.entries.each do |entry|
47
+ p "Entry: #{entry.title}"
48
+ entry.content.interval_block.interval_readings.each do |reading|
49
+ time_period = reading.time_period
50
+ p "[#{time_period.starts_at} - #{time_period.ends_at}]: #{reading.value}"
51
+ end
52
+ end
53
+ ```
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork this project
58
+ 2. Create a feature branch: `git checkout -b my-awesome-feature`
59
+ 3. Make changes and commit: `git commit -am "Add awesome feature"`
60
+ 4. Push the branch: `git push origin my-awesome-feature`
61
+ 5. Submit a pull request
62
+
63
+ ## License
64
+
65
+ This software is distributed AS IS WITHOUT WARRANTY under [Simplified BSD](https://raw.githubusercontent.com/VerdigrisTech/green-button-data/master/LICENSE.txt)
66
+ license.
67
+
68
+ Verdigris Technologies Inc. assumes NO RESPONSIBILITY OR LIABILITY
69
+ UNDER ANY CIRCUMSTANCES for usage of this software. See the [LICENSE.txt](https://raw.githubusercontent.com/VerdigrisTech/green-button-data/master/LICENSE.txt)
70
+ file for detailed legal information.
71
+
72
+ Copyright © 2015, Verdigris Technologies Inc. All rights reserved.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
4
+
5
+ task default: :spec
@@ -0,0 +1,24 @@
1
+ require File.expand_path('../lib/green-button-data/version', __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'green-button-data'
5
+ s.version = GreenButtonData::VERSION
6
+
7
+ s.authors = ['Andrew Jo']
8
+ s.email = 'engineering@verdigris.co'
9
+ s.homepage = 'http://verdigris.co'
10
+
11
+ s.summary = 'Parser for Green Button data format'
12
+ s.description = 'A library to parse large Green Button feed quickly'
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.require_paths = ['lib']
16
+
17
+ s.platform = Gem::Platform::RUBY
18
+
19
+ s.add_dependency 'nokogiri', '~> 1.6'
20
+ s.add_dependency 'sax-machine', '~> 1.3'
21
+
22
+ s.add_development_dependency 'rspec', '~> 3.0'
23
+ s.add_development_dependency 'guard'
24
+ end
@@ -0,0 +1,9 @@
1
+ class Date
2
+ def utc
3
+ self.new_offset(0)
4
+ end
5
+
6
+ def local
7
+ new_offset(DateTime.now.offset - offset)
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ class Fixnum
2
+ def digits(base = 10)
3
+ num = self.abs
4
+ if num == 0
5
+ 1
6
+ else
7
+ Math.log10(num).floor + 1
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,2 @@
1
+ require 'green-button-data/core_ext/date'
2
+ require 'green-button-data/core_ext/fixnum'
@@ -0,0 +1,49 @@
1
+ module GreenButtonData
2
+ module Dst
3
+ # From ESPI XML schema:
4
+ # [extension] Bit map encoded rule from which is calculated the start or
5
+ # end time, within the current year, to which daylight savings time offset
6
+ # must be applied.
7
+ #
8
+ # The rule encoding:
9
+ # Bits 0 - 11: seconds 0 - 3599
10
+ # Bits 12 - 16: hours 0 - 23
11
+ # Bits 17 - 19: day of the week 0 = not applicable, 1 - 7 (Monday = 1)
12
+ # Bits:20 - 24: day of the month 0 = not applicable, 1 - 31
13
+ # Bits: 25 - 27: operator (detailed below)
14
+ # Bits: 28 - 31: month 1 - 12
15
+ #
16
+ # Rule value of 0xFFFFFFFF means rule processing/DST correction is disabled.
17
+ #
18
+ # The operators:
19
+ #
20
+ # 0: DST starts/ends on the Day of the Month
21
+ # 1: DST starts/ends on the Day of the Week that is on or after the Day of the Month
22
+ # 2: DST starts/ends on the first occurrence of the Day of the Week in a month
23
+ # 3: DST starts/ends on the second occurrence of the Day of the Week in a month
24
+ # 4: DST starts/ends on the third occurrence of the Day of the Week in a month
25
+ # 5: DST starts/ends on the forth occurrence of the Day of the Week in a month
26
+ # 6: DST starts/ends on the fifth occurrence of the Day of the Week in a month
27
+ # 7: DST starts/ends on the last occurrence of the Day of the Week in a month
28
+ #
29
+ # An example: DST starts on third Friday in March at 1:45 AM. The rule...
30
+ # Seconds: 2700
31
+ # Hours: 1
32
+ # Day of Week: 5
33
+ # Day of Month: 0
34
+ # Operator: 4
35
+ # Month: 3
36
+ BITMASK_SECOND = 0x00000fff
37
+ BITMASK_HOUR = 0x0001f000
38
+ BITMASK_DAY_OF_WEEK = 0x000e0000
39
+ BITMASK_DAY_OF_MONTH = 0x01f00000
40
+ BITMASK_DST_RULE = 0x0e000000
41
+ BITMASK_MONTH = 0xf0000000
42
+
43
+ BITSHIFT_HOUR = 12
44
+ BITSHIFT_DAY_OF_WEEK = 17
45
+ BITSHIFT_DAY_OF_MONTH = 20
46
+ BITSHIFT_DST_RULE = 25
47
+ BITSHIFT_MONTH = 28
48
+ end
49
+ end