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,18 @@
1
+ [{
2
+ "uri": "http://www.example.com/locations/1",
3
+ "id": "1",
4
+ "name": "House",
5
+ "type": "house",
6
+ "created_at": "2012-07-16T17:04:10Z",
7
+ "updated_at": "2012-07-16T17:04:10Z",
8
+ "locations": {
9
+ "parent": null,
10
+ "children": [],
11
+ "ancestors": [],
12
+ "descendants": []
13
+ },
14
+ "devices": {
15
+ "children": [],
16
+ "descendants": []
17
+ }
18
+ }]
@@ -0,0 +1,6 @@
1
+ {
2
+ "access_token": "139912a5d007526e6bb9c9af7aa82b34a60320295843fb6a924e3173b299caf1",
3
+ "token_type": "bearer",
4
+ "expires_in": 7200,
5
+ "refresh_token": "e338d5fa2e67ed4f8859e009a5a1c6066da88b4d1314fb87004e02973888d87c"
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "access_token": "87c8fdbf746da5dec55747dc3c8410dbe33854277d0622a60b46b114e901f3cc",
3
+ "token_type": "bearer",
4
+ "expires_in": 7200,
5
+ "refresh_token": "2b997a78735a87ccba756b2df65370e642297cb9806bd0f4bffcc76337a18be0"
6
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "uri": "http://www.example.com/devices/5003c553d033a96a3c0003e0",
3
+ "status": "true",
4
+ "properties": [{
5
+ "uri": "https://type.lelylan.com/properties/status",
6
+ "value": "off"
7
+ },
8
+ {
9
+ "uri": "https://type.lelylan.com/properties/intensity",
10
+ "value": "0.0"
11
+ }]
12
+ }
@@ -0,0 +1,9 @@
1
+ [{
2
+ "uri": "http://www.example.com/properties/50042603d033a9b4ac000360",
3
+ "id": "50042603d033a9b4ac000360",
4
+ "name": "Status",
5
+ "default": "off",
6
+ "values": ["on", "off"],
7
+ "created_at": "2012-07-16T14:32:35Z",
8
+ "updated_at": "2012-07-16T14:32:35Z"
9
+ }]
@@ -0,0 +1,9 @@
1
+ {
2
+ "uri": "http://www.example.com/properties/50042605d033a9b4ac00040e",
3
+ "id": "50042605d033a9b4ac00040e",
4
+ "name": "Status",
5
+ "default": "off",
6
+ "values": ["on", "off"],
7
+ "created_at": "2012-07-16T14:32:37Z",
8
+ "updated_at": "2012-07-16T14:32:37Z"
9
+ }
@@ -0,0 +1,24 @@
1
+ {
2
+ "uri": "http://www.example.com/statuses/5004260cd033a9b4ac000642",
3
+ "id": "5004260cd033a9b4ac000642",
4
+ "name": "Setting intensity",
5
+ "pending": "true",
6
+ "created_at": "2012-07-16T14:32:44Z",
7
+ "updated_at": "2012-07-16T14:32:44Z",
8
+ "properties": [{
9
+ "uri": "http://www.example.com/properties/status",
10
+ "values": ["on"],
11
+ "range": {
12
+ "start": null,
13
+ "end": null
14
+ }
15
+ },
16
+ {
17
+ "uri": "http://www.example.com/properties/intensity",
18
+ "values": [],
19
+ "range": {
20
+ "start": "0",
21
+ "end": "100"
22
+ }
23
+ }]
24
+ }
@@ -0,0 +1,24 @@
1
+ [{
2
+ "uri": "http://www.example.com/statuses/50042607d033a9b4ac000438",
3
+ "id": "50042607d033a9b4ac000438",
4
+ "name": "Setting intensity",
5
+ "pending": "true",
6
+ "created_at": "2012-07-16T14:32:39Z",
7
+ "updated_at": "2012-07-16T14:32:39Z",
8
+ "properties": [{
9
+ "uri": "http://www.example.com/properties/status",
10
+ "values": ["on"],
11
+ "range": {
12
+ "start": null,
13
+ "end": null
14
+ }
15
+ },
16
+ {
17
+ "uri": "http://www.example.com/properties/intensity",
18
+ "values": [],
19
+ "range": {
20
+ "start": "0",
21
+ "end": "100"
22
+ }
23
+ }]
24
+ }]
@@ -0,0 +1,94 @@
1
+ {
2
+ "uri": "http://www.example.com/types/50042612d033a9b4ac0007fa",
3
+ "id": "50042612d033a9b4ac0007fa",
4
+ "name": "Dimmer",
5
+ "public": "true",
6
+ "created_at": "2012-07-16T14:32:50Z",
7
+ "updated_at": "2012-07-16T14:32:50Z",
8
+ "properties": [{
9
+ "uri": "http://www.example.com/properties/50042612d033a9b4ac0007ed",
10
+ "id": "50042612d033a9b4ac0007ed",
11
+ "name": "Status",
12
+ "default": "off",
13
+ "values": ["on", "off"],
14
+ "created_at": "2012-07-16T14:32:50Z",
15
+ "updated_at": "2012-07-16T14:32:50Z"
16
+ },
17
+ {
18
+ "uri": "http://www.example.com/properties/50042612d033a9b4ac0007ee",
19
+ "id": "50042612d033a9b4ac0007ee",
20
+ "name": "Intensity",
21
+ "default": "0.0",
22
+ "values": ["0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"],
23
+ "created_at": "2012-07-16T14:32:50Z",
24
+ "updated_at": "2012-07-16T14:32:50Z"
25
+ }],
26
+ "functions": [{
27
+ "uri": "http://www.example.com/functions/50042612d033a9b4ac0007f0",
28
+ "id": "50042612d033a9b4ac0007f0",
29
+ "name": "Turn on",
30
+ "created_at": "2012-07-16T14:32:50Z",
31
+ "updated_at": "2012-07-16T14:32:50Z",
32
+ "properties": [{
33
+ "uri": "http://www.example.com/properties/status",
34
+ "value": "on"
35
+ }]
36
+ },
37
+ {
38
+ "uri": "http://www.example.com/functions/50042612d033a9b4ac0007f2",
39
+ "id": "50042612d033a9b4ac0007f2",
40
+ "name": "Turn off",
41
+ "created_at": "2012-07-16T14:32:50Z",
42
+ "updated_at": "2012-07-16T14:32:50Z",
43
+ "properties": [{
44
+ "uri": "http://www.example.com/properties/status",
45
+ "value": "off"
46
+ }]
47
+ },
48
+ {
49
+ "uri": "http://www.example.com/functions/50042612d033a9b4ac0007f5",
50
+ "id": "50042612d033a9b4ac0007f5",
51
+ "name": "Set intensity",
52
+ "created_at": "2012-07-16T14:32:50Z",
53
+ "updated_at": "2012-07-16T14:32:50Z",
54
+ "properties": [{
55
+ "uri": "http://www.example.com/properties/status",
56
+ "value": "on"
57
+ },
58
+ {
59
+ "uri": "http://www.example.com/properties/intensity",
60
+ "value": "0"
61
+ }]
62
+ }],
63
+ "statuses": [{
64
+ "uri": "http://www.example.com/statuses/50042612d033a9b4ac0007f8",
65
+ "id": "50042612d033a9b4ac0007f8",
66
+ "name": "Setting intensity",
67
+ "pending": "true",
68
+ "created_at": "2012-07-16T14:32:50Z",
69
+ "updated_at": "2012-07-16T14:32:50Z",
70
+ "properties": [{
71
+ "uri": "http://www.example.com/properties/status",
72
+ "values": ["on"],
73
+ "range": {
74
+ "start": null,
75
+ "end": null
76
+ }
77
+ },
78
+ {
79
+ "uri": "http://www.example.com/properties/intensity",
80
+ "values": [],
81
+ "range": {
82
+ "start": "0",
83
+ "end": "100"
84
+ }
85
+ }]
86
+ }],
87
+ "categories": [{
88
+ "uri": "http://www.example.com/categories/50042612d033a9b4ac0007f9",
89
+ "id": "50042612d033a9b4ac0007f9",
90
+ "name": "Lighting",
91
+ "created_at": "2012-07-16T14:32:50Z",
92
+ "updated_at": "2012-07-16T14:32:50Z"
93
+ }]
94
+ }
@@ -0,0 +1,88 @@
1
+ [{
2
+ "uri": "http://www.example.com/types/5004260fd033a9b4ac0006d7",
3
+ "id": "5004260fd033a9b4ac0006d7",
4
+ "name": "Dimmer",
5
+ "public": "true",
6
+ "created_at": "2012-07-16T14:32:47Z",
7
+ "updated_at": "2012-07-16T14:32:47Z",
8
+ "properties": [{
9
+ "uri": "http://www.example.com/properties/5004260fd033a9b4ac0006ca",
10
+ "id": "5004260fd033a9b4ac0006ca",
11
+ "name": "Status",
12
+ "default": "off",
13
+ "values": ["on", "off"],
14
+ "created_at": "2012-07-16T14:32:47Z",
15
+ "updated_at": "2012-07-16T14:32:47Z"
16
+ },
17
+ {
18
+ "uri": "http://www.example.com/properties/5004260fd033a9b4ac0006cb",
19
+ "id": "5004260fd033a9b4ac0006cb",
20
+ "name": "Intensity",
21
+ "default": "0.0",
22
+ "values": ["0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"],
23
+ "created_at": "2012-07-16T14:32:47Z",
24
+ "updated_at": "2012-07-16T14:32:47Z"
25
+ }],
26
+ "functions": [{
27
+ "uri": "http://www.example.com/functions/5004260fd033a9b4ac0006cd",
28
+ "id": "5004260fd033a9b4ac0006cd",
29
+ "name": "Turn on",
30
+ "created_at": "2012-07-16T14:32:47Z",
31
+ "updated_at": "2012-07-16T14:32:47Z",
32
+ "properties": [{
33
+ "uri": "http://www.example.com/properties/status",
34
+ "value": "on"
35
+ }]
36
+ },
37
+ {
38
+ "uri": "http://www.example.com/functions/5004260fd033a9b4ac0006cf",
39
+ "id": "5004260fd033a9b4ac0006cf",
40
+ "name": "Turn off",
41
+ "created_at": "2012-07-16T14:32:47Z",
42
+ "updated_at": "2012-07-16T14:32:47Z",
43
+ "properties": [{
44
+ "uri": "http://www.example.com/properties/status",
45
+ "value": "off"
46
+ }]
47
+ },
48
+ {
49
+ "uri": "http://www.example.com/functions/5004260fd033a9b4ac0006d2",
50
+ "id": "5004260fd033a9b4ac0006d2",
51
+ "name": "Set intensity",
52
+ "created_at": "2012-07-16T14:32:47Z",
53
+ "updated_at": "2012-07-16T14:32:47Z",
54
+ "properties": [{
55
+ "uri": "http://www.example.com/properties/status",
56
+ "value": "on"
57
+ },
58
+ {
59
+ "uri": "http://www.example.com/properties/intensity",
60
+ "value": "0"
61
+ }]
62
+ }],
63
+ "statuses": [{
64
+ "uri": "http://www.example.com/statuses/5004260fd033a9b4ac0006d5",
65
+ "id": "5004260fd033a9b4ac0006d5",
66
+ "name": "Setting intensity",
67
+ "pending": "true",
68
+ "created_at": "2012-07-16T14:32:47Z",
69
+ "updated_at": "2012-07-16T14:32:47Z",
70
+ "properties": [{
71
+ "uri": "http://www.example.com/properties/status",
72
+ "values": ["on"],
73
+ "range": {
74
+ "start": null,
75
+ "end": null
76
+ }
77
+ },
78
+ {
79
+ "uri": "http://www.example.com/properties/intensity",
80
+ "values": [],
81
+ "range": {
82
+ "start": "0",
83
+ "end": "100"
84
+ }
85
+ }]
86
+ }],
87
+ "categories": []
88
+ }]
@@ -0,0 +1,71 @@
1
+ if ENV['COVERAGE']
2
+ require 'simplecov'
3
+ SimpleCov.start { add_filter "/spec" }
4
+ end
5
+
6
+ require 'lelylan'
7
+ require 'rspec'
8
+ require 'webmock/rspec'
9
+
10
+ def a_delete(url)
11
+ a_request(:delete, lelylan_url(url))
12
+ end
13
+
14
+ def a_get(url)
15
+ a_request(:get, lelylan_url(url))
16
+ end
17
+
18
+ def a_patch(url)
19
+ a_request(:patch, lelylan_url(url))
20
+ end
21
+
22
+ def a_post(url)
23
+ a_request(:post, lelylan_url(url))
24
+ end
25
+
26
+ def a_put(url)
27
+ a_request(:put, lelylan_url(url))
28
+ end
29
+
30
+ def stub_delete(url)
31
+ stub_request(:delete, lelylan_url(url))
32
+ end
33
+
34
+ def stub_get(url)
35
+ stub_request(:get, lelylan_url(url))
36
+ end
37
+
38
+ def stub_patch(url)
39
+ stub_request(:patch, lelylan_url(url))
40
+ end
41
+
42
+ def stub_post(url)
43
+ stub_request(:post, lelylan_url(url))
44
+ end
45
+
46
+ def stub_put(url)
47
+ stub_request(:put, lelylan_url(url))
48
+ end
49
+
50
+ def fixture_path
51
+ File.expand_path("../fixtures", __FILE__)
52
+ end
53
+
54
+ def fixture(file)
55
+ File.new(fixture_path + '/' + file)
56
+ end
57
+
58
+ def lelylan_url(url)
59
+ url =~ /^http/ ? url : "http://api.lelylan.com#{url}"
60
+ end
61
+
62
+ def a_client
63
+ client_id = '36963b6bd9dc1127553b57f55ea54d4cecf97e386bcecc5f9198e8dd0ed235f9'
64
+ client_secret = '8e6343a084320b6b11cdd3349642718c11af1af9b9f64ed4976018bdf20d0082'
65
+ site_uri = 'http://app.dev'
66
+ application = OAuth2::Client.new client_id, client_secret, site: site_uri
67
+ json_token = MultiJson.load fixture('oauth2/token.json').read
68
+ token = OAuth2::AccessToken.from_hash application, json_token
69
+
70
+ Lelylan::Client.new token: token
71
+ end
@@ -0,0 +1,178 @@
1
+ require 'helper'
2
+
3
+ describe Lelylan::Client::Categories do
4
+
5
+ let(:client) do
6
+ a_client
7
+ end
8
+
9
+ describe '.category' do
10
+
11
+ let(:path) do
12
+ '/categories/4dcb9e23d033a9088900000a'
13
+ end
14
+
15
+ let(:uri) do
16
+ "http://api.lelylan.com/#{path}"
17
+ end
18
+
19
+ before do
20
+ stub_get(path).to_return(body: fixture('category.json'))
21
+ end
22
+
23
+ let!(:category) do
24
+ client.category(uri)
25
+ end
26
+
27
+ it 'returns the category' do
28
+ category.uri.should_not be_nil
29
+ end
30
+
31
+ it 'sends the request' do
32
+ a_get(path).should have_been_made
33
+ end
34
+ end
35
+
36
+ describe '.categories' do
37
+
38
+ let(:path) do
39
+ '/categories'
40
+ end
41
+
42
+ before do
43
+ stub_get('/categories').to_return(body: fixture('categories.json'))
44
+ end
45
+
46
+ let!(:categories) do
47
+ client.categories
48
+ end
49
+
50
+ it 'returns a list of categories' do
51
+ categories.should have(1).item
52
+ end
53
+
54
+ it 'sends the request' do
55
+ a_get(path).should have_been_made
56
+ end
57
+
58
+ context 'with params' do
59
+
60
+ before do
61
+ stub_get(path).with(query: {per: '25'}).to_return(body: fixture('category.json'))
62
+ end
63
+
64
+ before do
65
+ client.categories(per: 25)
66
+ end
67
+
68
+ it 'sends the params' do
69
+ a_get(path).with(query: {per: '25'}).should have_been_made
70
+ end
71
+ end
72
+ end
73
+
74
+ describe '.public_categories' do
75
+
76
+ let(:path) do
77
+ '/categories/public'
78
+ end
79
+
80
+ before do
81
+ client.user = nil
82
+ client.password = nil
83
+ end
84
+
85
+ before do
86
+ stub_get(path).to_return(body: fixture('categories.json'))
87
+ end
88
+
89
+ let!(:categories) do
90
+ client.public_categories
91
+ end
92
+
93
+ it 'returns a list of categories' do
94
+ categories.should have(1).item
95
+ end
96
+
97
+ it 'sends the request' do
98
+ a_get('http://api.lelylan.com/categories/public').should have_been_made
99
+ end
100
+ end
101
+
102
+ describe '.create_category' do
103
+
104
+ let(:path) do
105
+ '/categories'
106
+ end
107
+
108
+ before do
109
+ stub_post(path).with(body: {name: 'Dimmer'}).to_return(body: fixture('category.json'))
110
+ end
111
+
112
+ let!(:category) do
113
+ client.create_category(name: 'Dimmer')
114
+ end
115
+
116
+ it 'returns the category' do
117
+ category.uri.should_not be_nil
118
+ end
119
+
120
+ it 'sends the request' do
121
+ a_post(path).with(body: {name: 'Dimmer'}).should have_been_made
122
+ end
123
+ end
124
+
125
+ describe '.update_category' do
126
+
127
+ let(:path) do
128
+ '/categories/4dcb9e23d033a9088900000a'
129
+ end
130
+
131
+ let(:uri) do
132
+ "http://api.lelylan.com/#{path}"
133
+ end
134
+
135
+ before do
136
+ stub_put(path).with(body: {name: 'Dimmer'}).to_return(body: fixture('category.json'))
137
+ end
138
+
139
+ let!(:category) do
140
+ client.update_category(uri, name: 'Dimmer')
141
+ end
142
+
143
+ it 'returns the category' do
144
+ category.uri.should_not be_nil
145
+ end
146
+
147
+ it 'sends the request' do
148
+ a_put(path).with(body: {name: 'Dimmer'}).should have_been_made
149
+ end
150
+ end
151
+
152
+ describe '.delete_category' do
153
+
154
+ let(:path) do
155
+ '/categories/4dcb9e23d033a9088900000a'
156
+ end
157
+
158
+ let(:uri) do
159
+ "http://api.lelylan.com/#{path}"
160
+ end
161
+
162
+ before do
163
+ stub_delete(path).to_return(body: fixture('category.json'))
164
+ end
165
+
166
+ let!(:category) do
167
+ client.delete_category(uri)
168
+ end
169
+
170
+ it 'returns the category' do
171
+ category.uri.should_not be_nil
172
+ end
173
+
174
+ it 'sends the request' do
175
+ a_delete(path).should have_been_made
176
+ end
177
+ end
178
+ end