enlighten 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 490f41eb62d8eaa500eee7d55b146cada84cb1d2
4
+ data.tar.gz: 89e55551d49d54fa9e7c11dba4ba15a4444f62fd
5
+ SHA512:
6
+ metadata.gz: 3d6a8250062987be653d60689e6110f4a8f129681038bf55f384f955cead46d8c4ef4fc78c0cdaf935d2bd165af55130dceb1016cf7c20ea9d7d3373cc799283
7
+ data.tar.gz: cebf2777340f390901c5fe99e26c4350a22e24a79cd85f1d48047c0b4dfb83c954dd2f5f9f9661ef417bfe8e690107b9dba93f9d632661de9584ec3fd6fba1ee
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .idea/
16
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in enlighten_system.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Robert Martin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+
2
+ # Enlighten
3
+
4
+ Ruby Gem for use with the Enphase "Elighten" API version 2
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'enlighten', :git => 'https://github.com/datadude/enlighten.git'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+
19
+ ## Usage
20
+
21
+ ### Rails
22
+ #### Configuraton
23
+
24
+ Add the following line to an initializer at `config/initializers/enlighten.rb`:
25
+
26
+ ```ruby
27
+ Enlighten::System.config(
28
+ key: '0960c4df1203f3079489fa8ccc251b59', user_id: '4d7a45774e6a41320a'
29
+ )
30
+ ```
31
+
32
+ #### Controller
33
+
34
+ From the Example App: `https://github.com/datadude/enlighten-example`
35
+
36
+ ```ruby
37
+ def home
38
+ @system = Enlighten::System.new(params[:id])
39
+ @summary = @system.summary
40
+ @inventory = @system.inventory
41
+ @envoys = @system.envoys
42
+ @production = @system.monthly_production(:start_date=>1.month.ago - 1.day)
43
+ @stats = @system.stats(:start_at=>DateTime.new(Time.now.year,Time.now.month,Time.now.day,7,0), :end_at=>Time.now)
44
+ end
45
+ ```
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it ( https://github.com/[my-github-username]/enlighten_system/fork )
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create a new Pull Request
54
+
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'enlighten/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "enlighten"
8
+ spec.version = Enlighten::VERSION
9
+ spec.authors = ["Robert Martin"]
10
+ spec.email = [""]
11
+ spec.summary = %q{Use this gem to connect to the enphase 'enlighten' API. }
12
+ spec.description = %q{Docs at https://developer.enphase.com/docs}
13
+ spec.homepage = "https://www.github.com/datadude/enlighten"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
24
+
@@ -0,0 +1,78 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'digest'
4
+
5
+ module Enlighten
6
+ class EnlightenError < StandardError
7
+ end
8
+ class EnlightenApiError < EnlightenError
9
+ attr_reader :code
10
+ def initialize(json)
11
+ @code = json.reason.to_i if json.reason.to_i
12
+ super((json.message && json.message.join('; ')) || json.reason)
13
+ end
14
+ end
15
+
16
+ class System
17
+ @default_params = {
18
+ host: 'api.enphaseenergy.com',
19
+ path: '/api/v2/systems',
20
+ time_zone: 7
21
+ }
22
+
23
+ class << self
24
+ def config(args={})
25
+ @default_params = @default_params.merge(args).freeze unless args.empty?
26
+ @default_params
27
+ end
28
+ # Allows to set defaults through app configuration:
29
+ #
30
+ # config.action_mailer.default_options = { from: "no-reply@example.org" }
31
+ alias :default_options= :config
32
+ end
33
+
34
+ def method_missing(method,*args)
35
+ @attributes[method.to_s + Digest::MD5.base64digest(args[0].to_s)] ||= fetch(method, args[0])
36
+ end
37
+
38
+ def initialize(id)
39
+ @id = id
40
+ @attributes = {}
41
+ end
42
+
43
+ def self.find(id)
44
+ new(id)
45
+ end
46
+
47
+ def self.default_params
48
+ @default_params
49
+ end
50
+
51
+ def self.url
52
+ "https://#{@default_params[:host]}#{@default_params[:path]}"
53
+ end
54
+
55
+ protected
56
+ def fetch(method, args={})
57
+ result = OpenStruct.new(JSON.parse(api_response(method,args)))
58
+ raise(EnlightenApiError.new(result)) if result.respond_to?(:reason)
59
+ result
60
+ end
61
+
62
+ def api_response(method,args={})
63
+ Net::HTTP.get(URI(format_url(method,args)))
64
+ end
65
+
66
+ def format_url(method,args={})
67
+ params = {key: self.class.default_params[:key], user_id: self.class.default_params[:user_id]}.merge(args||{})
68
+ self.class.url + '/' + @id.to_s + '/' + method.to_s + '?' + query_string(params)
69
+ end
70
+ def query_string(args)
71
+ args.map{|k,v|value= (['start_at','end_at'].include? k.to_s) ? v.to_i.to_s : date_format(v);"#{k.to_s}=#{CGI.escape(value)}"}.join('&')
72
+ end
73
+ def date_format(date)
74
+ date ? (date.respond_to?(:strftime)?date.strftime('%Y-%m-%d'):date.to_s) : ''
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,3 @@
1
+ module Enlighten
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,371 @@
1
+ {
2
+ "system_id": 67,
3
+ "start_date": "2010-01-01",
4
+ "production": [
5
+ 3127,
6
+ 14110,
7
+ 17198,
8
+ 15659,
9
+ 14237,
10
+ 6539,
11
+ 4290,
12
+ 10067,
13
+ 4420,
14
+ 4358,
15
+ 6763,
16
+ 5630,
17
+ 16868,
18
+ 20527,
19
+ 8873,
20
+ 8483,
21
+ 4225,
22
+ 5329,
23
+ 10043,
24
+ 6402,
25
+ 1062,
26
+ 9669,
27
+ 17194,
28
+ 4439,
29
+ 2282,
30
+ 5438,
31
+ 13094,
32
+ 12720,
33
+ 5866,
34
+ 14052,
35
+ 10574,
36
+ 8738,
37
+ 11305,
38
+ 14856,
39
+ 2916,
40
+ 15552,
41
+ 16057,
42
+ 27711,
43
+ 13529,
44
+ 8295,
45
+ 17578,
46
+ 15026,
47
+ 10560,
48
+ 19489,
49
+ 21192,
50
+ 22895,
51
+ 26994,
52
+ 25892,
53
+ 14793,
54
+ 4146,
55
+ 7647,
56
+ 10755,
57
+ 25478,
58
+ 2956,
59
+ 21711,
60
+ 20036,
61
+ 6465,
62
+ 20471,
63
+ 28685,
64
+ 6525,
65
+ 19653,
66
+ 15296,
67
+ 26730,
68
+ 15237,
69
+ 23879,
70
+ 32267,
71
+ 27005,
72
+ 27128,
73
+ 34080,
74
+ 32566,
75
+ 6583,
76
+ 34968,
77
+ 33152,
78
+ 34043,
79
+ 30036,
80
+ 32771,
81
+ 33954,
82
+ 34776,
83
+ 25908,
84
+ 22426,
85
+ 34804,
86
+ 34827,
87
+ 29282,
88
+ 29091,
89
+ 36017,
90
+ 35532,
91
+ 32512,
92
+ 13991,
93
+ 31278,
94
+ 14430,
95
+ 30085,
96
+ 12487,
97
+ 31628,
98
+ 7673,
99
+ 34264,
100
+ 38094,
101
+ 37773,
102
+ 38189,
103
+ 38038,
104
+ 11104,
105
+ 5528,
106
+ 30811,
107
+ 33827,
108
+ 34101,
109
+ 36760,
110
+ 36455,
111
+ 34317,
112
+ 38834,
113
+ 22368,
114
+ 37169,
115
+ 21740,
116
+ 39407,
117
+ 39597,
118
+ 39997,
119
+ 39496,
120
+ 31209,
121
+ 24311,
122
+ 29883,
123
+ 39720,
124
+ 41366,
125
+ 39875,
126
+ 40160,
127
+ 39827,
128
+ 40365,
129
+ 41138,
130
+ 41510,
131
+ 41696,
132
+ 41461,
133
+ 32475,
134
+ 23574,
135
+ 41688,
136
+ 38853,
137
+ 40947,
138
+ 33689,
139
+ 34975,
140
+ 23837,
141
+ 9077,
142
+ 35178,
143
+ 20402,
144
+ 41445,
145
+ 36696,
146
+ 38997,
147
+ 42461,
148
+ 18665,
149
+ 9785,
150
+ 34792,
151
+ 25582,
152
+ 42151,
153
+ 41393,
154
+ 40121,
155
+ 34519,
156
+ 40803,
157
+ 35699,
158
+ 29882,
159
+ 18384,
160
+ 37070,
161
+ 39492,
162
+ 40481,
163
+ 0,
164
+ 0,
165
+ 0,
166
+ 0,
167
+ 0,
168
+ 0,
169
+ 0,
170
+ 0,
171
+ 0,
172
+ 0,
173
+ 0,
174
+ 0,
175
+ 0,
176
+ 39653,
177
+ 38213,
178
+ 36622,
179
+ 38922,
180
+ 35113,
181
+ 37723,
182
+ 38263,
183
+ 38139,
184
+ 38681,
185
+ 39237,
186
+ 38351,
187
+ 38278,
188
+ 38326,
189
+ 38669,
190
+ 33773,
191
+ 18903,
192
+ 34637,
193
+ 35073,
194
+ 32284,
195
+ 35184,
196
+ 30683,
197
+ 15233,
198
+ 26058,
199
+ 37106,
200
+ 37266,
201
+ 35507,
202
+ 34226,
203
+ 37507,
204
+ 38476,
205
+ 34713,
206
+ 32492,
207
+ 32458,
208
+ 33306,
209
+ 35685,
210
+ 33608,
211
+ 11365,
212
+ 26785,
213
+ 35709,
214
+ 34082,
215
+ 33383,
216
+ 34050,
217
+ 32762,
218
+ 32018,
219
+ 34539,
220
+ 31751,
221
+ 32461,
222
+ 31323,
223
+ 31895,
224
+ 31065,
225
+ 27040,
226
+ 26545,
227
+ 25768,
228
+ 33553,
229
+ 31831,
230
+ 31668,
231
+ 30836,
232
+ 34117,
233
+ 31446,
234
+ 29489,
235
+ 33060,
236
+ 29001,
237
+ 24720,
238
+ 33294,
239
+ 33492,
240
+ 32502,
241
+ 32747,
242
+ 25064,
243
+ 32463,
244
+ 33932,
245
+ 32553,
246
+ 33708,
247
+ 32474,
248
+ 31759,
249
+ 31920,
250
+ 33449,
251
+ 29744,
252
+ 32877,
253
+ 32169,
254
+ 36882,
255
+ 37297,
256
+ 36184,
257
+ 35788,
258
+ 35410,
259
+ 33994,
260
+ 26158,
261
+ 27800,
262
+ 31437,
263
+ 34909,
264
+ 25639,
265
+ 17778,
266
+ 11325,
267
+ 34429,
268
+ 28702,
269
+ 34574,
270
+ 33763,
271
+ 33132,
272
+ 32622,
273
+ 33098,
274
+ 31862,
275
+ 31283,
276
+ 31725,
277
+ 32718,
278
+ 25269,
279
+ 21066,
280
+ 9118,
281
+ 27927,
282
+ 29552,
283
+ 29430,
284
+ 19932,
285
+ 26729,
286
+ 27985,
287
+ 27756,
288
+ 27967,
289
+ 27760,
290
+ 26708,
291
+ 24802,
292
+ 27009,
293
+ 17608,
294
+ 4540,
295
+ 21930,
296
+ 23787,
297
+ 15730,
298
+ 10183,
299
+ 4167,
300
+ 3047,
301
+ 2946,
302
+ 21228,
303
+ 27323,
304
+ 15232,
305
+ 7245,
306
+ 4590,
307
+ 5456,
308
+ 15172,
309
+ 25215,
310
+ 25160,
311
+ 21818,
312
+ 16401,
313
+ 9918,
314
+ 9157,
315
+ 11644,
316
+ 24259,
317
+ 17781,
318
+ 22672,
319
+ 23286,
320
+ 22559,
321
+ 22385,
322
+ 21246,
323
+ 23047,
324
+ 23526,
325
+ 23376,
326
+ 15914,
327
+ 5362,
328
+ 10712,
329
+ 16481,
330
+ 14896,
331
+ 19896,
332
+ 23693,
333
+ 22921,
334
+ 18459,
335
+ 10275,
336
+ 15439,
337
+ 23170,
338
+ 12040,
339
+ 16386,
340
+ 4569,
341
+ 4592,
342
+ 4181,
343
+ 5169,
344
+ 14197,
345
+ 12906,
346
+ 2094,
347
+ 8733,
348
+ 3860,
349
+ 13914,
350
+ 3913,
351
+ 5733,
352
+ 3294,
353
+ 16115,
354
+ 14271,
355
+ 3317,
356
+ 4058,
357
+ 5249,
358
+ 3990,
359
+ 6788,
360
+ 7227,
361
+ 11788,
362
+ 13496,
363
+ 1459,
364
+ 4716,
365
+ 11142,
366
+ 3466,
367
+ 17517,
368
+ 21495,
369
+ 7049
370
+ ]
371
+ }