green-button-data 0.4.1 → 0.4.2

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.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- MjY1ZGViODMxZWUwODVmMjM5YTA2MDRiNTA0ZGVkNTQ0MThlZWM0Ng==
5
- data.tar.gz: !binary |-
6
- OWM5YWU0ZjU4NThjN2E0NjdlNGFhM2M1ZWRkYTgyZjllM2MxZGEyYQ==
2
+ SHA1:
3
+ metadata.gz: 1ceb62da3fba8372097a53e504b7a9a5873dd2ff
4
+ data.tar.gz: 44fc0e9c4c3ee4a6ffac789714a250322cd8c3ef
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- MjBjOWM2YTYzODJlNDgxZjE1OWZlOGNlNTcwY2Y5YTg2ZDI3Y2UzZGEzMWQw
10
- ZGU1OTAzMzU5M2MwOWJkODcxZDU4MjhkNTRmMmQzY2Y3YjdkNmM0YzVjMTIy
11
- YjgwMWU3N2I5OWJlMWMzMWI1Y2JjYWQ0OTQ3Y2JiN2Q4ZjViYjE=
12
- data.tar.gz: !binary |-
13
- N2NlMWZkNzVjZjBlNDQ4YmFhNzA0ZjhlNzcwZDEyOGJmNGUyNjY0OTcxMTk4
14
- YmMyMzg4YzQ3NDcyZmY4MzhhYWM0MWQ5OTA0ZjUzMjU2YTg3ZDM0OTk0YzY2
15
- NTg0NTAyZjE1ZTY4NzUyMzAwYWUyNWQ5YzFhNTUyYjFlODc0Y2E=
6
+ metadata.gz: 69d63b6058a2eae2b6339c59ae2acd5adfc5a68ab19b87bbba35e4ad797b59ad5849970aa8593c496cff0e4fc417cea0d6d9bd5e0d0155dc64eca1d04b1b2a56
7
+ data.tar.gz: e0367553136129ea42ff0c84f8abb185256e0d2d73bc984840f4a8178691193c3a1bc31142069b323b77342bde47522aa215eff085fc183b85b9e72d93bf3c6b
@@ -49,7 +49,7 @@ module GreenButtonData
49
49
  end
50
50
 
51
51
  id ||= options[:interval_block_id]
52
- meter_reading_id = options[:meter_Reading_id]
52
+ meter_reading_id = options[:meter_reading_id]
53
53
  usage_point_id = options[:usage_point_id]
54
54
  subscription_id = options[:subscription_id]
55
55
 
@@ -14,9 +14,7 @@ module GreenButtonData
14
14
  :usage_summary_path
15
15
 
16
16
  def application_information_url(id = nil)
17
- uri = URI.join @base_url, @application_information_path
18
- uri = URI.join uri, "#{id}/" if id
19
- return uri.to_s
17
+ return build_url @application_information_path, id
20
18
  end
21
19
 
22
20
  def application_information_url=(url)
@@ -26,9 +24,7 @@ module GreenButtonData
26
24
  end
27
25
 
28
26
  def authorization_url(id = nil)
29
- uri = URI.join @base_url, @authorization_path
30
- uri = URI.join uri, "#{id}/" if id
31
- return uri.to_s
27
+ return build_url @authorization_path, id
32
28
  end
33
29
 
34
30
  def authorization_url=(url)
@@ -61,9 +57,7 @@ module GreenButtonData
61
57
  end
62
58
 
63
59
  def local_time_parameters_url(id = nil)
64
- uri = URI.join @base_url, @local_time_parameters_path
65
- uri = URI.join uri, "#{id}/" if id
66
- return uri.to_s
60
+ return build_url @local_time_parameters_path, id
67
61
  end
68
62
 
69
63
  def meter_reading_url(kwargs = {})
@@ -88,16 +82,11 @@ module GreenButtonData
88
82
  end
89
83
 
90
84
  def reading_type_url(id = nil)
91
- uri = URI.join @base_url, @reading_type_path
92
- uri = URI.join uri, "#{id}/" if id
93
-
94
- return uri.to_s
85
+ return build_url @reading_type_path, id
95
86
  end
96
87
 
97
88
  def subscription_url(id)
98
- uri = URI.join @base_url, @subscription_path
99
- uri = URI.join uri, "#{id}/" if id
100
- return uri.to_s
89
+ return build_url @subscription_path, id
101
90
  end
102
91
 
103
92
  def usage_point_url(kwargs = {})
@@ -135,5 +124,14 @@ module GreenButtonData
135
124
  " usage_point_id"
136
125
  end
137
126
  end
127
+
128
+ private
129
+
130
+ def build_url(path, id = nil)
131
+ uri = URI.join @base_url, path
132
+ uri = URI.join uri, "#{id}/" if id
133
+
134
+ return uri.to_s
135
+ end
138
136
  end
139
137
  end
@@ -68,16 +68,17 @@ module GreenButtonData
68
68
  month = (byte & BITMASK_MONTH) >> BITSHIFT_MONTH
69
69
 
70
70
  # Raise an error unless all the values are in valid range
71
- seconds.between?(0, 3599) and hour.between?(0, 23) and
72
- weekday.between?(1, 7) and day.between?(0, 31) and
73
- dst_rule.between?(0, 7) and month.between?(1, 12) or
74
- raise RangeError, 'Invalid value range'
71
+ validate_dst_rules dst_rule, month, weekday, day, hour, seconds
75
72
 
76
73
  # In Ruby, Sunday = 0 not 7
77
74
  weekday = weekday == 7 ? 0 : weekday
78
75
 
79
- # Check the DST rule
80
- dst_day = if dst_rule == 1
76
+ # Add the hour and seconds component to the day
77
+ dst_datetime dst_rule, year, month, weekday, day, hour, seconds
78
+ end
79
+
80
+ def dst_datetime(dst_rule, year, month, weekday, day, hour, seconds)
81
+ if dst_rule == 1
81
82
  # Rule 1: DST starts/ends on Day of Week on or after the Day of Month
82
83
  day_of_month = DateTime.new year, month, day
83
84
  day_offset = if weekday >= day_of_month.wday
@@ -97,10 +98,14 @@ module GreenButtonData
97
98
  else
98
99
  # Rule 0: DST starts/ends on the Day of Month
99
100
  DateTime.new year, month, day
100
- end
101
+ end + Rational(hour * 3600 + seconds, 86400)
102
+ end
101
103
 
102
- # Add the hour and seconds component to the day
103
- dst_day + Rational(hour * 3600 + seconds, 86400)
104
+ def validate_dst_rules(dst_rule, month, weekday, day, hour, seconds)
105
+ seconds.between?(0, 3599) and hour.between?(0, 23) and
106
+ weekday.between?(1, 7) and day.between?(0, 31) and
107
+ dst_rule.between?(0, 7) and month.between?(1, 12) or
108
+ raise RangeError, 'Invalid value range'
104
109
  end
105
110
  end
106
111
  end
@@ -9,9 +9,7 @@ module GreenButtonData
9
9
  def initialize(attributes)
10
10
  # Automagically sets instance variables from attribute hash parsed from
11
11
  # the GreenButtonData::Parser classes
12
- attributes.each do |key, value|
13
- self.instance_variable_set :"@#{key}", value
14
- end
12
+ init_instance_vars attributes
15
13
 
16
14
  # Handle relations via related_urls
17
15
  @related_urls.is_a?(Hash) and @related_urls.each do |key, value|
@@ -21,56 +19,7 @@ module GreenButtonData
21
19
  end
22
20
 
23
21
  # Define accessor methods from pluralized resource names
24
- self.class.send :define_method, "#{key.to_s.pluralize}" do |*args|
25
- id = args[0]
26
- options = args[1]
27
-
28
- klazz_name = "GreenButtonData::#{key.to_s.camelize}"
29
-
30
- # Handle deprecations
31
- klazz_name.gsub! /ElectricPowerUsageSummary/, 'UsageSummary'
32
-
33
- klazz = class_from_name klazz_name
34
-
35
- collection = self.instance_variable_get "@#{key.to_s.pluralize}"
36
- url = self.instance_variable_get "@#{key}_url"
37
-
38
- # Make the ID argument optional
39
- options ||= if id.is_a?(Hash)
40
- id
41
- else
42
- {}
43
- end
44
-
45
- result = if id.is_a?(Numeric) || id.is_a?(String) || id.is_a?(Symbol)
46
- # Try returning cached results first
47
- collection and instance = collection.find_by_id(id)
48
- cache_miss = instance.nil?
49
-
50
- # On cache miss or forced reload, send API request
51
- instance = if !options[:reload] && instance
52
- instance
53
- else
54
- klazz.find "#{url}/#{id}", options
55
- end
56
-
57
- # Cache the result
58
- collection ||= ModelCollection.new
59
- collection << instance if cache_miss
60
-
61
- instance
62
- else
63
- if !options[:reload] && collection
64
- collection
65
- else
66
- collection = klazz.all url, options
67
- end
68
- end
69
-
70
- self.instance_variable_set :"@#{key.to_s.pluralize}", collection
71
-
72
- return result
73
- end
22
+ define_attr_accessors key
74
23
  end
75
24
  end # initialize
76
25
 
@@ -83,5 +32,80 @@ module GreenButtonData
83
32
  value
84
33
  end
85
34
  end
35
+
36
+ private
37
+
38
+ def init_instance_vars(attributes)
39
+ attributes.each do |key, value|
40
+ self.instance_variable_set :"@#{key}", value
41
+ end
42
+ end
43
+
44
+ def define_attr_accessors(resource)
45
+ self.class.send :define_method, "#{resource.to_s.pluralize}" do |*args|
46
+ id = args[0]
47
+ options = args[1]
48
+
49
+ # Make the ID argument optional
50
+ options ||= if id.is_a?(Hash)
51
+ id
52
+ else
53
+ {}
54
+ end
55
+
56
+ if id.is_a?(Numeric) || id.is_a?(String) || id.is_a?(Symbol)
57
+ get_or_fetch_entry id, resource, options
58
+ else
59
+ get_or_fetch_collection resource, options
60
+ end
61
+ end
62
+ end
63
+
64
+ def klazz_name(name)
65
+ str = "GreenButtonData::#{name}"
66
+ str.gsub! /ElectricPowerUsageSummary/, 'UsageSummary'
67
+ return str
68
+ end
69
+
70
+ def get_or_fetch_collection(resource, options = {})
71
+ klazz = class_from_name klazz_name(resource.to_s.camelize)
72
+ url = self.instance_variable_get "@#{resource}_url"
73
+ collection = self.instance_variable_get "@#{resource.to_s.pluralize}"
74
+
75
+ collection = if !options[:reload] && collection
76
+ collection
77
+ else
78
+ collection = klazz.all url, options
79
+ end
80
+
81
+ self.instance_variable_set :"@#{resource.to_s.pluralize}", collection
82
+
83
+ return collection
84
+ end
85
+
86
+ def get_or_fetch_entry(id, resource, options = {})
87
+ klazz = class_from_name klazz_name(resource.to_s.camelize)
88
+ url = self.instance_variable_get "@#{resource}_url"
89
+ collection = self.instance_variable_get "@#{resource.to_s.pluralize}"
90
+
91
+ # Try returning cached results first
92
+ collection and instance = collection.find_by_id(id)
93
+ cache_miss = instance.nil?
94
+
95
+ # On cache miss or forced reload, send API request
96
+ instance = if !options[:reload] && instance
97
+ instance
98
+ else
99
+ klazz.find "#{url}/#{id}", options
100
+ end
101
+
102
+ # Cache the result
103
+ collection ||= ModelCollection.new
104
+ collection << instance if cache_miss
105
+
106
+ self.instance_variable_set :"@#{resource.to_s.pluralize}", collection
107
+
108
+ instance
109
+ end
86
110
  end # Entry
87
111
  end # GreenButtonData
@@ -25,15 +25,7 @@ module GreenButtonData
25
25
  url = nil
26
26
  end
27
27
 
28
- url_options = url_options(options)
29
-
30
- url ||= if url_options.keys.size > 0
31
- GreenButtonData.configuration.send(
32
- "#{class_name.underscore}_url", url_options(options)
33
- )
34
- else
35
- GreenButtonData.configuration.send "#{class_name.underscore}_url"
36
- end
28
+ url ||= url_path_prefix url_options(options)
37
29
 
38
30
  @url = url
39
31
  @options = options
@@ -76,15 +68,7 @@ module GreenButtonData
76
68
  url = if id =~ /\A#{URI::regexp}\z/
77
69
  id
78
70
  else
79
- url_options = url_options options
80
-
81
- path_prefix = if url_options.keys.size > 0
82
- GreenButtonData.configuration.send(
83
- "#{class_name.underscore}_url", url_options(options)
84
- )
85
- else
86
- GreenButtonData.configuration.send "#{class_name.underscore}_url"
87
- end
71
+ path_prefix = url_path_prefix url_options(options)
88
72
 
89
73
  URI.join(path_prefix, "#{id}/").to_s
90
74
  end
@@ -257,6 +241,16 @@ module GreenButtonData
257
241
 
258
242
  return url_options
259
243
  end
244
+
245
+ def url_path_prefix(options)
246
+ if options.keys.size > 0
247
+ GreenButtonData.configuration.send(
248
+ "#{class_name.underscore}_url", options
249
+ )
250
+ else
251
+ GreenButtonData.configuration.send "#{class_name.underscore}_url"
252
+ end
253
+ end
260
254
  end # ClassMethods
261
255
  end # Fetchable
262
256
  end # GreenButtonData
@@ -16,11 +16,11 @@ module GreenButtonData
16
16
  @dst_offset + @tz_offset
17
17
  end
18
18
 
19
- def to_h
19
+ def to_h(year = Time.now.year)
20
20
  {
21
21
  dst: {
22
- starts_at: dst_starts_at,
23
- ends_at: dst_ends_at,
22
+ starts_at: dst_starts_at(year),
23
+ ends_at: dst_ends_at(year),
24
24
  offset: dst_offset
25
25
  },
26
26
  tz_offset: tz_offset,
@@ -59,7 +59,7 @@ module GreenButtonData
59
59
  # need to be added before hitting the first Sunday of month
60
60
  day_offset = first_weekday == 0 ? 0 : 7 - first_weekday
61
61
 
62
- # Return first Monday of the month
62
+ # Return first Sunday of the month
63
63
  first_day + day_offset
64
64
  end
65
65
 
@@ -78,10 +78,11 @@ module GreenButtonData
78
78
  # To retrieve third Friday of July 2015,
79
79
  # nth_weekday_of 2015, 7, 5, 3
80
80
  def nth_weekday_of(year = Time.now.year, month = Time.now.month,
81
- weekday = 0, week = 1)
81
+ weekday = 0, week = 1)
82
+ first_day = DateTime.new year, month, 1
82
83
 
83
84
  # Day offset needed for Nth day of the week
84
- first_sunday_of(year, month) + weekday + (week - 1) * 7
85
+ first_day + weekday_offset(first_day.wday, weekday) + (week - 1) * 7
85
86
  end
86
87
 
87
88
  ##
@@ -110,7 +111,15 @@ module GreenButtonData
110
111
  7 + last_weekday - weekday
111
112
  end
112
113
 
113
- last_day - day_offset
114
+ last_day - weekday_offset(weekday, last_weekday)
115
+ end
116
+
117
+ def weekday_offset(first_weekday, second_weekday)
118
+ if second_weekday >= first_weekday
119
+ second_weekday - first_weekday
120
+ else
121
+ 7 + second_weekday - first_weekday
122
+ end
114
123
  end
115
124
 
116
125
  ##
@@ -1,3 +1,3 @@
1
1
  module GreenButtonData
2
- VERSION = '0.4.1'
2
+ VERSION = '0.4.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: green-button-data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Jo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-15 00:00:00.000000000 Z
11
+ date: 2016-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -179,17 +179,17 @@ require_paths:
179
179
  - lib
180
180
  required_ruby_version: !ruby/object:Gem::Requirement
181
181
  requirements:
182
- - - ! '>='
182
+ - - '>='
183
183
  - !ruby/object:Gem::Version
184
184
  version: 1.9.3
185
185
  required_rubygems_version: !ruby/object:Gem::Requirement
186
186
  requirements:
187
- - - ! '>='
187
+ - - '>='
188
188
  - !ruby/object:Gem::Version
189
189
  version: '0'
190
190
  requirements: []
191
191
  rubyforge_project:
192
- rubygems_version: 2.2.2
192
+ rubygems_version: 2.4.6
193
193
  signing_key:
194
194
  specification_version: 4
195
195
  summary: Parser for Green Button data format