lelylan-rb 0.0.1

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 (62) hide show
  1. data/.gitignore +26 -0
  2. data/CHANGELOG.md +5 -0
  3. data/Gemfile +7 -0
  4. data/Guardfile +5 -0
  5. data/LICENSE.md +1 -0
  6. data/README.md +218 -0
  7. data/Rakefile +21 -0
  8. data/lelylan_rb.gemspec +36 -0
  9. data/lib/faraday/response/raise_http_error.rb +35 -0
  10. data/lib/lelylan.rb +26 -0
  11. data/lib/lelylan/authentication.rb +11 -0
  12. data/lib/lelylan/client.rb +47 -0
  13. data/lib/lelylan/client/categories.rb +112 -0
  14. data/lib/lelylan/client/consumptions.rb +93 -0
  15. data/lib/lelylan/client/devices.rb +211 -0
  16. data/lib/lelylan/client/functions.rb +118 -0
  17. data/lib/lelylan/client/histories.rb +42 -0
  18. data/lib/lelylan/client/locations.rb +92 -0
  19. data/lib/lelylan/client/properties.rb +115 -0
  20. data/lib/lelylan/client/statuses.rb +110 -0
  21. data/lib/lelylan/client/types.rb +109 -0
  22. data/lib/lelylan/configuration.rb +65 -0
  23. data/lib/lelylan/connection.rb +33 -0
  24. data/lib/lelylan/error.rb +34 -0
  25. data/lib/lelylan/request.rb +70 -0
  26. data/lib/lelylan/version.rb +15 -0
  27. data/spec/faraday/response_spec.rb +37 -0
  28. data/spec/fixtures/categories.json +7 -0
  29. data/spec/fixtures/category.json +7 -0
  30. data/spec/fixtures/consumption.json +11 -0
  31. data/spec/fixtures/consumptions.json +11 -0
  32. data/spec/fixtures/device.json +25 -0
  33. data/spec/fixtures/devices.json +25 -0
  34. data/spec/fixtures/function.json +15 -0
  35. data/spec/fixtures/functions.json +15 -0
  36. data/spec/fixtures/histories.json +16 -0
  37. data/spec/fixtures/history.json +16 -0
  38. data/spec/fixtures/location.json +55 -0
  39. data/spec/fixtures/locations.json +18 -0
  40. data/spec/fixtures/oauth2/refresh.json +6 -0
  41. data/spec/fixtures/oauth2/token.json +6 -0
  42. data/spec/fixtures/pending.json +12 -0
  43. data/spec/fixtures/properties.json +9 -0
  44. data/spec/fixtures/property.json +9 -0
  45. data/spec/fixtures/status.json +24 -0
  46. data/spec/fixtures/statuses.json +24 -0
  47. data/spec/fixtures/type.json +94 -0
  48. data/spec/fixtures/types.json +88 -0
  49. data/spec/helper.rb +71 -0
  50. data/spec/lelylan/client/categories_spec.rb +178 -0
  51. data/spec/lelylan/client/consumptions_spec.rb +150 -0
  52. data/spec/lelylan/client/devices_spec.rb +342 -0
  53. data/spec/lelylan/client/functions_spec.rb +184 -0
  54. data/spec/lelylan/client/histories_spec.rb +64 -0
  55. data/spec/lelylan/client/locations_spec.rb +155 -0
  56. data/spec/lelylan/client/properties_spec.rb +184 -0
  57. data/spec/lelylan/client/statuses_spec.rb +184 -0
  58. data/spec/lelylan/client/types_spec.rb +184 -0
  59. data/spec/lelylan/client_spec.rb +32 -0
  60. data/spec/lelylan/oauth2_spec.rb +54 -0
  61. data/spec/lelylan_spec.rb +32 -0
  62. metadata +351 -0
@@ -0,0 +1,184 @@
1
+ require 'helper'
2
+
3
+ describe Lelylan::Client::Statuses do
4
+
5
+ let(:client) do
6
+ a_client
7
+ end
8
+
9
+
10
+ describe '.status' do
11
+
12
+ let(:path) do
13
+ '/statuses/4dcb9e23d033a9088900000a'
14
+ end
15
+
16
+ let(:uri) do
17
+ "http://api.lelylan.com/#{path}"
18
+ end
19
+
20
+ before do
21
+ stub_get(path).to_return(body: fixture('status.json'))
22
+ end
23
+
24
+ let!(:status) do
25
+ client.status(uri)
26
+ end
27
+
28
+ it 'returns the status' do
29
+ status.uri.should_not be_nil
30
+ end
31
+
32
+ it 'sends the request' do
33
+ a_get(path).should have_been_made
34
+ end
35
+ end
36
+
37
+
38
+ describe '.statuses' do
39
+
40
+ let(:path) do
41
+ '/statuses'
42
+ end
43
+
44
+ before do
45
+ stub_get('/statuses').to_return(body: fixture('statuses.json'))
46
+ end
47
+
48
+ let!(:statuses) do
49
+ client.statuses
50
+ end
51
+
52
+ it 'returns a list of statuses' do
53
+ statuses.should have(1).item
54
+ end
55
+
56
+ it 'sends the request' do
57
+ a_get(path).should have_been_made
58
+ end
59
+
60
+ context 'with params' do
61
+
62
+ before do
63
+ stub_get(path).with(query: {per: '25'}).to_return(body: fixture('status.json'))
64
+ end
65
+
66
+ before do
67
+ client.statuses(per: 25)
68
+ end
69
+
70
+ it 'sends the params' do
71
+ a_get(path).with(query: {per: '25'}).should have_been_made
72
+ end
73
+ end
74
+ end
75
+
76
+
77
+ describe '.public_statuses' do
78
+
79
+ let(:path) do
80
+ '/statuses/public'
81
+ end
82
+
83
+ before do
84
+ client.user = nil
85
+ client.password = nil
86
+ end
87
+
88
+ before do
89
+ stub_get(path).to_return(body: fixture('statuses.json'))
90
+ end
91
+
92
+ let!(:statuses) do
93
+ client.public_statuses
94
+ end
95
+
96
+ it 'returns a list of statuses' do
97
+ statuses.should have(1).item
98
+ end
99
+
100
+ it 'sends the request' do
101
+ a_get('http://api.lelylan.com/statuses/public').should have_been_made
102
+ end
103
+ end
104
+
105
+
106
+ describe '.create_status' do
107
+
108
+ let(:path) do
109
+ '/statuses'
110
+ end
111
+
112
+ before do
113
+ stub_post(path).with(body: {name: 'Setting intensity'}).to_return(body: fixture('status.json'))
114
+ end
115
+
116
+ let!(:status) do
117
+ client.create_status(name: 'Setting intensity')
118
+ end
119
+
120
+ it 'returns the status' do
121
+ status.uri.should_not be_nil
122
+ end
123
+
124
+ it 'sends the request' do
125
+ a_post(path).with(body: {name: 'Setting intensity'}).should have_been_made
126
+ end
127
+ end
128
+
129
+
130
+ describe '.update_status' do
131
+
132
+ let(:path) do
133
+ '/statuses/4dcb9e23d033a9088900000a'
134
+ end
135
+
136
+ let(:uri) do
137
+ "http://api.lelylan.com/#{path}"
138
+ end
139
+
140
+ before do
141
+ stub_put(path).with(body: {name: 'Setting intensity'}).to_return(body: fixture('status.json'))
142
+ end
143
+
144
+ let!(:status) do
145
+ client.update_status(uri, name: 'Setting intensity')
146
+ end
147
+
148
+ it 'returns the status' do
149
+ status.uri.should_not be_nil
150
+ end
151
+
152
+ it 'sends the request' do
153
+ a_put(path).with(body: {name: 'Setting intensity'}).should have_been_made
154
+ end
155
+ end
156
+
157
+
158
+ describe '.delete_status' do
159
+
160
+ let(:path) do
161
+ '/statuses/4dcb9e23d033a9088900000a'
162
+ end
163
+
164
+ let(:uri) do
165
+ "http://api.lelylan.com/#{path}"
166
+ end
167
+
168
+ before do
169
+ stub_delete(path).to_return(body: fixture('status.json'))
170
+ end
171
+
172
+ let!(:status) do
173
+ client.delete_status(uri)
174
+ end
175
+
176
+ it 'returns the status' do
177
+ status.uri.should_not be_nil
178
+ end
179
+
180
+ it 'sends the request' do
181
+ a_delete(path).should have_been_made
182
+ end
183
+ end
184
+ end
@@ -0,0 +1,184 @@
1
+ require 'helper'
2
+
3
+ describe Lelylan::Client::Types do
4
+
5
+ let(:client) do
6
+ a_client
7
+ end
8
+
9
+
10
+ describe '.type' do
11
+
12
+ let(:path) do
13
+ '/types/4dcb9e23d033a9088900000a'
14
+ end
15
+
16
+ let(:uri) do
17
+ "http://api.lelylan.com/#{path}"
18
+ end
19
+
20
+ before do
21
+ stub_get(path).to_return(body: fixture('type.json'))
22
+ end
23
+
24
+ let!(:type) do
25
+ client.type(uri)
26
+ end
27
+
28
+ it 'returns the type' do
29
+ type.uri.should_not be_nil
30
+ end
31
+
32
+ it 'sends the request' do
33
+ a_get(path).should have_been_made
34
+ end
35
+ end
36
+
37
+
38
+ describe '.types' do
39
+
40
+ let(:path) do
41
+ '/types'
42
+ end
43
+
44
+ before do
45
+ stub_get(path).to_return(body: fixture('types.json'))
46
+ end
47
+
48
+ let!(:types) do
49
+ client.types
50
+ end
51
+
52
+ it 'returns a list of types' do
53
+ types.should have(1).item
54
+ end
55
+
56
+ it 'sends the request' do
57
+ a_get(path).should have_been_made
58
+ end
59
+
60
+ context 'with params' do
61
+
62
+ before do
63
+ stub_get(path).with(query: {per: '25'}).to_return(body: fixture('type.json'))
64
+ end
65
+
66
+ before do
67
+ client.types(per: 25)
68
+ end
69
+
70
+ it 'sends the params' do
71
+ a_get(path).with(query: {per: '25'}).should have_been_made
72
+ end
73
+ end
74
+ end
75
+
76
+
77
+ describe '.public_types' do
78
+
79
+ let(:path) do
80
+ '/types/public'
81
+ end
82
+
83
+ before do
84
+ client.user = nil
85
+ client.password = nil
86
+ end
87
+
88
+ before do
89
+ stub_get(path).to_return(body: fixture('types.json'))
90
+ end
91
+
92
+ let!(:types) do
93
+ client.public_types
94
+ end
95
+
96
+ it 'returns a list of types' do
97
+ types.should have(1).item
98
+ end
99
+
100
+ it 'sends the request' do
101
+ a_get('http://api.lelylan.com/types/public').should have_been_made
102
+ end
103
+ end
104
+
105
+
106
+ describe '.create_type' do
107
+
108
+ let(:path) do
109
+ '/types'
110
+ end
111
+
112
+ before do
113
+ stub_post(path).with(body: {name: 'Dimmer'}).to_return(body: fixture('type.json'))
114
+ end
115
+
116
+ let!(:type) do
117
+ client.create_type(name: 'Dimmer')
118
+ end
119
+
120
+ it 'returns the type' do
121
+ type.uri.should_not be_nil
122
+ end
123
+
124
+ it 'sends the request' do
125
+ a_post(path).with(body: {name: 'Dimmer'}).should have_been_made
126
+ end
127
+ end
128
+
129
+
130
+ describe '.update_type' do
131
+
132
+ let(:path) do
133
+ '/types/4dcb9e23d033a9088900000a'
134
+ end
135
+
136
+ let(:uri) do
137
+ "http://api.lelylan.com/#{path}"
138
+ end
139
+
140
+ before do
141
+ stub_put(path).with(body: {name: 'Dimmer'}).to_return(body: fixture('type.json'))
142
+ end
143
+
144
+ let!(:type) do
145
+ client.update_type(uri, name: 'Dimmer')
146
+ end
147
+
148
+ it 'returns the type' do
149
+ type.uri.should_not be_nil
150
+ end
151
+
152
+ it 'sends the request' do
153
+ a_put(path).with(body: {name: 'Dimmer'}).should have_been_made
154
+ end
155
+ end
156
+
157
+
158
+ describe '.delete_type' do
159
+
160
+ let(:path) do
161
+ '/types/4dcb9e23d033a9088900000a'
162
+ end
163
+
164
+ let(:uri) do
165
+ "http://api.lelylan.com/#{path}"
166
+ end
167
+
168
+ before do
169
+ stub_delete(path).to_return(body: fixture('type.json'))
170
+ end
171
+
172
+ let!(:type) do
173
+ client.delete_type(uri)
174
+ end
175
+
176
+ it 'returns the type' do
177
+ type.uri.should_not be_nil
178
+ end
179
+
180
+ it 'sends the request' do
181
+ a_delete(path).should have_been_made
182
+ end
183
+ end
184
+ end
@@ -0,0 +1,32 @@
1
+ require 'helper'
2
+
3
+ describe Lelylan::Client do
4
+
5
+ let(:client) do
6
+ a_client
7
+ end
8
+
9
+ describe '#api_endpoint' do
10
+
11
+ after(:each) do
12
+ Lelylan.reset
13
+ end
14
+
15
+ it 'sets default to http://api.lelylan.com' do
16
+ client = Lelylan::Client.new
17
+ client.api_endpoint.should == 'http://api.lelylan.com/'
18
+ end
19
+
20
+ it 'can be set' do
21
+ Lelylan.api_endpoint = 'http://lelylan.dev'
22
+ client = Lelylan::Client.new
23
+ client.api_endpoint.should == 'http://lelylan.dev/'
24
+ end
25
+
26
+ it 'can use the alias endpoint' do
27
+ Lelylan.endpoint = 'http://lelylan.dev'
28
+ client = Lelylan::Client.new
29
+ client.endpoint.should == 'http://lelylan.dev/'
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,54 @@
1
+ require 'helper'
2
+
3
+ describe OAuth2 do
4
+
5
+ let(:client_id) { '36963b6bd9dc1127553b57f55ea54d4cecf97e386bcecc5f9198e8dd0ed235f9' }
6
+ let(:client_secret) { '8e6343a084320b6b11cdd3349642718c11af1af9b9f64ed4976018bdf20d0082' }
7
+ let(:redirect_uri) { 'http://app.dev/callback' }
8
+ let(:site_uri) { 'http://app.dev' }
9
+ let(:application) { OAuth2::Client.new client_id, client_secret, site: site_uri }
10
+ let(:json_token) { MultiJson.load fixture('oauth2/token.json').read }
11
+ let(:token) { OAuth2::AccessToken.from_hash application, json_token }
12
+
13
+ let(:client) { Lelylan::Client.new token: token }
14
+ let(:path) { '/types/4dcb9e23d033a9088900000a' }
15
+ let(:uri) { "http://api.lelylan.com/#{path}" }
16
+ let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
17
+
18
+ context 'with not expired token' do
19
+
20
+ before { stub_get(path).with(headers: headers).to_return(body: fixture('type.json')) }
21
+ before { client.type uri }
22
+
23
+ it 'adds the oauth2 token in the header' do
24
+ a_get(path).with(headers: headers).should have_been_made
25
+ end
26
+ end
27
+
28
+ context 'with expired token' do
29
+
30
+ let(:refresh_uri) { "#{site_uri}/oauth/token" }
31
+ let(:refresh_token) { MultiJson.load fixture('oauth2/refresh.json').read }
32
+ let(:refresh_headers) { { 'Content-Type' => 'application/json' } }
33
+ let(:headers) { { 'Authorization' => "Bearer #{refresh_token['access_token']}" } }
34
+
35
+ let(:refresh_body) {{
36
+ client_id: client_id,
37
+ client_secret: client_secret,
38
+ grant_type: 'refresh_token',
39
+ refresh_token: token.refresh_token
40
+ }}
41
+
42
+ before { stub_post(refresh_uri).with(body: refresh_body).to_return(headers: refresh_headers, body: refresh_token) }
43
+ before { stub_get(path).with(headers: headers).to_return(body: fixture('type.json')) }
44
+ before { token.instance_variable_set '@expires_at', 0 }
45
+
46
+ it 'expires token' do
47
+ token.expired?.should == true
48
+ end
49
+
50
+ it 'refreshes token' do
51
+ expect{ client.type uri }.to change{ client.token.token }
52
+ end
53
+ end
54
+ end