enlighten 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile +3 -0
- data/lib/enlighten.rb +20 -10
- data/lib/enlighten/version.rb +1 -1
- data/test/fixtures/index.json +89 -0
- data/test/test_enlighten_system.rb +55 -9
- data/test/test_helper.rb +1 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3ff0fba84b343e0a7063ddc0f597131b27a36d4c
|
4
|
+
data.tar.gz: e5b0353fed5993c86d6b76235b63adeb70d31d2f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cff1acca935b69234c06961dc1e264aca1fa86a2d1bffad35deb08272cd6458f592f2a2095b6e6110a2815b7d9cc528603e694b89e7274a650583e59f24bf180
|
7
|
+
data.tar.gz: e96994cef307c8574cf0987166f9d22b537926a162a747724471042195a78ac5ab05c4230b8cb0d85f8791afeb44e44e39b4036e5c450d6352e62837c70c346d
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/lib/enlighten.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'json'
|
2
2
|
require 'net/http'
|
3
3
|
require 'digest'
|
4
|
+
require 'cgi'
|
4
5
|
|
5
6
|
module Enlighten
|
6
7
|
class EnlightenError < StandardError
|
@@ -14,13 +15,19 @@ module Enlighten
|
|
14
15
|
end
|
15
16
|
|
16
17
|
class System
|
18
|
+
attr_reader :params
|
17
19
|
@default_params = {
|
18
20
|
host: 'api.enphaseenergy.com',
|
19
|
-
path: '/api/v2/systems'
|
20
|
-
time_zone: 7
|
21
|
+
path: '/api/v2/systems'
|
21
22
|
}
|
22
|
-
|
23
|
+
END_POINTS = [:energy_lifetime,:envoys,:inventory,:monthly_production,:rgm_stats,:stats,:summary]
|
24
|
+
END_POINTS.each do |end_point|
|
25
|
+
define_method(end_point) do |*args|
|
26
|
+
@attributes[end_point.to_s + Digest::MD5.base64digest(args[0].to_s)] ||= fetch(end_point, args[0])
|
27
|
+
end
|
28
|
+
end
|
23
29
|
class << self
|
30
|
+
|
24
31
|
def config(args={})
|
25
32
|
@default_params = @default_params.merge(args).freeze unless args.empty?
|
26
33
|
@default_params
|
@@ -32,16 +39,19 @@ module Enlighten
|
|
32
39
|
end
|
33
40
|
|
34
41
|
def method_missing(method,*args)
|
35
|
-
@attributes[method.to_s
|
42
|
+
@attributes[method.to_s] || super
|
36
43
|
end
|
37
44
|
|
38
|
-
def initialize(
|
39
|
-
@id = id
|
40
|
-
|
45
|
+
def initialize(params={})
|
46
|
+
@id = params[:id]
|
47
|
+
#overide the default config with parameters passed in, like (user_id)
|
48
|
+
@params=self.class.default_params.merge(params)
|
49
|
+
@systems = fetch(nil).systems
|
50
|
+
@attributes = @systems.map{|system| system if system['system_id'] == @id }[0] rescue {}
|
41
51
|
end
|
42
52
|
|
43
53
|
def self.find(id)
|
44
|
-
new(id)
|
54
|
+
new(:id=>id)
|
45
55
|
end
|
46
56
|
|
47
57
|
def self.default_params
|
@@ -64,8 +74,8 @@ protected
|
|
64
74
|
end
|
65
75
|
|
66
76
|
def format_url(method,args={})
|
67
|
-
params = {key:
|
68
|
-
self.class.url + '/' +
|
77
|
+
params = {key: @params[:key], user_id: @params[:user_id]}.merge(args||{})
|
78
|
+
self.class.url + '/' + (method && (@id.to_s + '/' + method.to_s)||'') + '?' + query_string(params)
|
69
79
|
end
|
70
80
|
def query_string(args)
|
71
81
|
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('&')
|
data/lib/enlighten/version.rb
CHANGED
@@ -0,0 +1,89 @@
|
|
1
|
+
{
|
2
|
+
"systems": [
|
3
|
+
{
|
4
|
+
"system_id": 67,
|
5
|
+
"system_name": "Eich Residence",
|
6
|
+
"system_public_name": "Eich Residence",
|
7
|
+
"status": "normal",
|
8
|
+
"timezone": "America/Los_Angeles",
|
9
|
+
"country": "US",
|
10
|
+
"state": "CA",
|
11
|
+
"city": "Sebastopol",
|
12
|
+
"postal_code": "95472",
|
13
|
+
"other_references": ["Solarfox"],
|
14
|
+
"connection_type": "ethernet"
|
15
|
+
},
|
16
|
+
{
|
17
|
+
"system_id": 239922,
|
18
|
+
"system_name": "Fornage D st",
|
19
|
+
"system_public_name": "Residential System",
|
20
|
+
"status": "normal",
|
21
|
+
"timezone": "America/Los_Angeles",
|
22
|
+
"country": "US",
|
23
|
+
"state": "CA",
|
24
|
+
"city": "Petaluma",
|
25
|
+
"postal_code": "94952",
|
26
|
+
"connection_type": "ethernet"
|
27
|
+
},
|
28
|
+
{
|
29
|
+
"system_id": 283776,
|
30
|
+
"system_name": "Brown/Pontoriero Residence",
|
31
|
+
"system_public_name": "Brown/Pontoriero Residence",
|
32
|
+
"status": "micro",
|
33
|
+
"timezone": "America/Los_Angeles",
|
34
|
+
"country": "US",
|
35
|
+
"state": "CA",
|
36
|
+
"city": "Petaluma",
|
37
|
+
"postal_code": "94954",
|
38
|
+
"connection_type": "wifi"
|
39
|
+
},
|
40
|
+
{
|
41
|
+
"system_id": 286840,
|
42
|
+
"system_name": "Rogers, Jim",
|
43
|
+
"system_public_name": "Residential System",
|
44
|
+
"status": "normal",
|
45
|
+
"timezone": "America/Los_Angeles",
|
46
|
+
"country": "US",
|
47
|
+
"state": "CA",
|
48
|
+
"city": "Petaluma",
|
49
|
+
"postal_code": "94952",
|
50
|
+
"connection_type": "ethernet"
|
51
|
+
},
|
52
|
+
{
|
53
|
+
"system_id": 339784,
|
54
|
+
"system_name": "Weed Patch Solar",
|
55
|
+
"system_public_name": "Residential System",
|
56
|
+
"status": "normal",
|
57
|
+
"timezone": "America/Denver",
|
58
|
+
"country": "US",
|
59
|
+
"state": "ID",
|
60
|
+
"city": "Mountain Home",
|
61
|
+
"postal_code": "83647",
|
62
|
+
"connection_type": "wifi"
|
63
|
+
},
|
64
|
+
{
|
65
|
+
"system_id": 490782,
|
66
|
+
"system_name": "Derek's House - 5 Regalia",
|
67
|
+
"system_public_name": "Derek's House - 5 Regalia",
|
68
|
+
"status": "normal",
|
69
|
+
"timezone": "America/Los_Angeles",
|
70
|
+
"country": "US",
|
71
|
+
"state": "CA",
|
72
|
+
"city": "Novato",
|
73
|
+
"postal_code": "94947-2125",
|
74
|
+
"connection_type": "ethernet"
|
75
|
+
},
|
76
|
+
{
|
77
|
+
"system_id": 537438,
|
78
|
+
"system_name": "Wagoner",
|
79
|
+
"system_public_name": "Residential System",
|
80
|
+
"status": "normal",
|
81
|
+
"timezone": "America/Los_Angeles",
|
82
|
+
"country": "US",
|
83
|
+
"state": "CA",
|
84
|
+
"city": "Windsor",
|
85
|
+
"postal_code": "95492",
|
86
|
+
"connection_type": "ethernet"
|
87
|
+
}
|
88
|
+
]
|
89
|
+
}
|
@@ -1,13 +1,18 @@
|
|
1
1
|
require_relative 'test_helper'
|
2
2
|
require 'date'
|
3
3
|
|
4
|
+
|
4
5
|
describe Enlighten::System do
|
5
6
|
include Enlighten
|
6
7
|
|
7
|
-
let(:system){
|
8
|
+
let(:system){
|
9
|
+
Enlighten::System.stub_any_instance(:api_response,load_fixture(:index)) do
|
10
|
+
Enlighten::System.new(:id=>67)
|
11
|
+
end
|
12
|
+
}
|
8
13
|
before do
|
9
14
|
#my key with the test user id
|
10
|
-
Enlighten::System.config(key: '
|
15
|
+
Enlighten::System.config(key: 'abc123', user_id: '123abc')
|
11
16
|
end
|
12
17
|
it "should initalize with default params" do
|
13
18
|
Enlighten::System.url.must_equal "https://api.enphaseenergy.com/api/v2/systems"
|
@@ -23,11 +28,11 @@ describe Enlighten::System do
|
|
23
28
|
|
24
29
|
describe 'format_url' do
|
25
30
|
it "should format the url" do
|
26
|
-
system.send(:format_url,:summary).must_equal('https://api.enphaseenergy.com/api/v2/systems/67/summary?key=
|
31
|
+
system.send(:format_url,:summary).must_equal('https://api.enphaseenergy.com/api/v2/systems/67/summary?key=abc123&user_id=123abc')
|
27
32
|
end
|
28
33
|
it "should format the url with parameters" do
|
29
34
|
system.send(:format_url,:energy_lifetime,start_date: '2010-01-01',end_date: '2010-12-31').must_equal(
|
30
|
-
"https://api.enphaseenergy.com/api/v2/systems/67/energy_lifetime?key=
|
35
|
+
"https://api.enphaseenergy.com/api/v2/systems/67/energy_lifetime?key=abc123&user_id=123abc&start_date=2010-01-01&end_date=2010-12-31"
|
31
36
|
)
|
32
37
|
end
|
33
38
|
it "should format the url with Time parameters" do
|
@@ -35,14 +40,20 @@ describe Enlighten::System do
|
|
35
40
|
end_date = DateTime.parse('2010-12-31')
|
36
41
|
|
37
42
|
system.send(:format_url,:energy_lifetime,start_date: start_date,end_date: end_date).must_equal(
|
38
|
-
"https://api.enphaseenergy.com/api/v2/systems/67/energy_lifetime?key=
|
43
|
+
"https://api.enphaseenergy.com/api/v2/systems/67/energy_lifetime?key=abc123&user_id=123abc&start_date=2010-01-01&end_date=2010-12-31"
|
39
44
|
)
|
40
45
|
end
|
41
46
|
it "should format the url with 'start_at', and 'end_at' parameters" do
|
42
47
|
system.send(:format_url,:stats,start_at: DateTime.strptime('2015-01-01 00:00','%Y-%m-%d %H:%M').to_time,end_at: DateTime.strptime('2015-01-02 00:00', '%Y-%m-%d %H:%M').to_time).must_equal(
|
43
|
-
"https://api.enphaseenergy.com/api/v2/systems/67/stats?key=
|
48
|
+
"https://api.enphaseenergy.com/api/v2/systems/67/stats?key=abc123&user_id=123abc&start_at=1420070400&end_at=1420156800"
|
44
49
|
)
|
45
50
|
end
|
51
|
+
it "should format an index url if given nil params" do
|
52
|
+
system.send(:format_url,nil,nil).must_equal(
|
53
|
+
"https://api.enphaseenergy.com/api/v2/systems/?key=abc123&user_id=123abc"
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
46
57
|
end
|
47
58
|
|
48
59
|
describe 'date_format' do
|
@@ -66,6 +77,41 @@ describe Enlighten::System do
|
|
66
77
|
end
|
67
78
|
end
|
68
79
|
|
80
|
+
describe 'attributes' do
|
81
|
+
it "should set a system_id" do
|
82
|
+
system.system_id.must_equal 67
|
83
|
+
end
|
84
|
+
it "should set a system_name" do
|
85
|
+
system.system_name.must_equal "Eich Residence"
|
86
|
+
end
|
87
|
+
it "should set a system_public_name" do
|
88
|
+
system.system_public_name.must_equal "Eich Residence"
|
89
|
+
end
|
90
|
+
it "should set a status" do
|
91
|
+
system.status.must_equal "normal"
|
92
|
+
end
|
93
|
+
it "should set a timezone" do
|
94
|
+
system.timezone.must_equal "America/Los_Angeles"
|
95
|
+
end
|
96
|
+
it "should set a country" do
|
97
|
+
system.country.must_equal "US"
|
98
|
+
end
|
99
|
+
it "should set a state" do
|
100
|
+
system.state.must_equal "CA"
|
101
|
+
end
|
102
|
+
it "should set a city" do
|
103
|
+
system.city.must_equal"Sebastopol"
|
104
|
+
end
|
105
|
+
it "should set a connection_type" do
|
106
|
+
system.connection_type.must_equal "ethernet"
|
107
|
+
end
|
108
|
+
it "should set other_references" do
|
109
|
+
system.other_references.must_equal ["Solarfox"]
|
110
|
+
end
|
111
|
+
it "should set a postal_code" do
|
112
|
+
system.postal_code.must_equal "95472"
|
113
|
+
end
|
114
|
+
end
|
69
115
|
|
70
116
|
describe 'REST end points' do
|
71
117
|
it "should return a system summary upon creation" do
|
@@ -97,9 +143,9 @@ describe Enlighten::System do
|
|
97
143
|
system.energy_lifetime.start_date.must_equal "2010-01-01"
|
98
144
|
end
|
99
145
|
it "should 'find' a system" do
|
100
|
-
|
101
|
-
|
102
|
-
|
146
|
+
Enlighten::System.stub_any_instance(:api_response,load_fixture(:index)) do
|
147
|
+
new_system = Enlighten::System.find(67)
|
148
|
+
new_system.system_id.must_equal 67
|
103
149
|
end
|
104
150
|
end
|
105
151
|
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enlighten
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Martin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -56,6 +56,7 @@ files:
|
|
56
56
|
- test/fixtures/energy_lifetime.json
|
57
57
|
- test/fixtures/envoys.json
|
58
58
|
- test/fixtures/error.json
|
59
|
+
- test/fixtures/index.json
|
59
60
|
- test/fixtures/inventory.json
|
60
61
|
- test/fixtures/monthly_production.json
|
61
62
|
- test/fixtures/rgm_stats.json
|
@@ -91,6 +92,7 @@ test_files:
|
|
91
92
|
- test/fixtures/energy_lifetime.json
|
92
93
|
- test/fixtures/envoys.json
|
93
94
|
- test/fixtures/error.json
|
95
|
+
- test/fixtures/index.json
|
94
96
|
- test/fixtures/inventory.json
|
95
97
|
- test/fixtures/monthly_production.json
|
96
98
|
- test/fixtures/rgm_stats.json
|