lelylan-rb 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/.gitignore +1 -0
  2. data/CHANGELOG.md +6 -2
  3. data/Gemfile +1 -3
  4. data/Guardfile +1 -1
  5. data/README.md +155 -84
  6. data/Rakefile +2 -15
  7. data/images/bg_hr.png +0 -0
  8. data/images/blacktocat.png +0 -0
  9. data/images/icon_download.png +0 -0
  10. data/images/sprite_download.png +0 -0
  11. data/index.html +74 -0
  12. data/javascripts/main.js +1 -0
  13. data/lelylan_rb.gemspec +12 -13
  14. data/lib/lelylan/client.rb +20 -23
  15. data/lib/lelylan/client/device.rb +121 -0
  16. data/lib/lelylan/client/function.rb +73 -0
  17. data/lib/lelylan/client/history.rb +28 -0
  18. data/lib/lelylan/client/location.rb +62 -0
  19. data/lib/lelylan/client/physical.rb +34 -0
  20. data/lib/lelylan/client/profile.rb +17 -0
  21. data/lib/lelylan/client/property.rb +75 -0
  22. data/lib/lelylan/client/status.rb +74 -0
  23. data/lib/lelylan/client/subscription.rb +62 -0
  24. data/lib/lelylan/client/type.rb +73 -0
  25. data/lib/lelylan/configuration.rb +4 -4
  26. data/lib/lelylan/connection.rb +9 -2
  27. data/lib/lelylan/request.rb +2 -16
  28. data/lib/lelylan/version.rb +1 -1
  29. data/spec/fixtures/profile.json +8 -0
  30. data/spec/fixtures/subscription.json +10 -0
  31. data/spec/fixtures/subscriptions.json +10 -0
  32. data/spec/lelylan/client/device_spec.rb +234 -0
  33. data/spec/lelylan/client/function_spec.rb +158 -0
  34. data/spec/lelylan/client/history_spec.rb +48 -0
  35. data/spec/lelylan/client/location_spec.rb +122 -0
  36. data/spec/lelylan/client/physical_spec.rb +27 -0
  37. data/spec/lelylan/client/profile_spec.rb +27 -0
  38. data/spec/lelylan/client/property_spec.rb +159 -0
  39. data/spec/lelylan/client/status_spec.rb +158 -0
  40. data/spec/lelylan/client/subscription_spec.rb +144 -0
  41. data/spec/lelylan/client/type_spec.rb +158 -0
  42. data/spec/lelylan/oauth2_spec.rb +13 -19
  43. data/stylesheets/pygment_trac.css +70 -0
  44. data/stylesheets/stylesheet.css +431 -0
  45. metadata +75 -114
  46. data/lib/lelylan/client/categories.rb +0 -112
  47. data/lib/lelylan/client/consumptions.rb +0 -93
  48. data/lib/lelylan/client/devices.rb +0 -211
  49. data/lib/lelylan/client/functions.rb +0 -118
  50. data/lib/lelylan/client/histories.rb +0 -42
  51. data/lib/lelylan/client/locations.rb +0 -92
  52. data/lib/lelylan/client/properties.rb +0 -115
  53. data/lib/lelylan/client/statuses.rb +0 -110
  54. data/lib/lelylan/client/types.rb +0 -109
  55. data/spec/lelylan/client/categories_spec.rb +0 -178
  56. data/spec/lelylan/client/consumptions_spec.rb +0 -150
  57. data/spec/lelylan/client/devices_spec.rb +0 -342
  58. data/spec/lelylan/client/functions_spec.rb +0 -184
  59. data/spec/lelylan/client/histories_spec.rb +0 -64
  60. data/spec/lelylan/client/locations_spec.rb +0 -155
  61. data/spec/lelylan/client/properties_spec.rb +0 -184
  62. data/spec/lelylan/client/statuses_spec.rb +0 -184
  63. data/spec/lelylan/client/types_spec.rb +0 -184
@@ -0,0 +1,75 @@
1
+ module Lelylan
2
+ class Client
3
+ module Property
4
+
5
+ #
6
+ # Public: Returns extended information for a given property identified from its ID.
7
+ #
8
+ # id - A String that represent the property ID.
9
+ #
10
+ # Returns Hashie The property.
11
+ #
12
+ def property(id)
13
+ get("/properties/#{id}")
14
+ end
15
+
16
+ #
17
+ # Public: Returns a list of owned properties.
18
+ #
19
+ # params - The Hash used to refine the search (default: {}).
20
+ #
21
+ # Returns Array List of properties.
22
+ #
23
+ def properties(params = {})
24
+ get('/properties', params)
25
+ end
26
+
27
+ #
28
+ # Public: Returns a list of all existing properties.
29
+ #
30
+ # params - The Hash used to refine the search (default: {}).
31
+ #
32
+ # Returns Array List of types.
33
+ #
34
+ def public_properties(params = {})
35
+ get('/properties/public', params)
36
+ end
37
+
38
+ #
39
+ # Public: Create a property and returns extended information for it.
40
+ #
41
+ # params - The Hash used to create the resource (default: {}).
42
+ #
43
+ # Returns Hashie The created property.
44
+ #
45
+ def create_property(params = {})
46
+ post('/properties', params)
47
+ end
48
+
49
+ #
50
+ # Public: Update a property identified from its ID and returns extended information for it.
51
+ #
52
+ # id - A String that represent the property ID.
53
+ # params - The Hash used to update the resource (default: {}).
54
+ #
55
+ # Returns Hashie The updated property.
56
+ #
57
+ def update_property(id, params = {})
58
+ put("/properties/#{id}", params)
59
+ end
60
+
61
+ #
62
+ # Public: Delete a property identified from its ID and returns extended information for it.
63
+ #
64
+ # id - A String that represent the property ID.
65
+ #
66
+ # Returns Hashie The deleted property.
67
+ #
68
+ def delete_property(id)
69
+ delete("/properties/#{id}")
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+
@@ -0,0 +1,74 @@
1
+ module Lelylan
2
+ class Client
3
+ module Status
4
+
5
+ #
6
+ # Public: Returns extended information for a given status identified from its ID.
7
+ #
8
+ # id - A String that represent the status ID.
9
+ #
10
+ # Returns Hashie The status.
11
+ #
12
+ def status(id)
13
+ get("/statuses/#{id}")
14
+ end
15
+
16
+ #
17
+ # Public: Returns a list of owned statuses.
18
+ #
19
+ # params - The Hash used to refine the search (default: {}).
20
+ #
21
+ # Returns Array List of statuses.
22
+ #
23
+ def statuses(params = {})
24
+ get('/statuses', params)
25
+ end
26
+
27
+ #
28
+ # Public: Returns a list of all existing statuses.
29
+ #
30
+ # params - The Hash used to refine the search (default: {}).
31
+ #
32
+ # Returns Array List of statuses.
33
+ #
34
+ def public_statuses(params = {})
35
+ get('/statuses/public', params)
36
+ end
37
+
38
+ #
39
+ # Public: Create a status and returns extended information for it.
40
+ #
41
+ # params - The Hash used to create the resource (default: {}).
42
+ #
43
+ # Returns Hashie The created status.
44
+ #
45
+ def create_status(params = {})
46
+ post('/statuses', params)
47
+ end
48
+
49
+ #
50
+ # Public: Update a status identified from its ID and returns extended information for it.
51
+ #
52
+ # id - A String that represent the status ID.
53
+ # params - The Hash used to update the resource (default: {}).
54
+ #
55
+ # Returns Hashie The updated status.
56
+ #
57
+ def update_status(id, params = {})
58
+ put("/statuses/#{id}", params)
59
+ end
60
+
61
+ #
62
+ # Public: Delete a status identified from its ID and returns extended information for it.
63
+ #
64
+ # id - A String that represent the status ID.
65
+ #
66
+ # Returns Hashie The deleted status.
67
+ #
68
+ def delete_status(id)
69
+ delete("/statuses/#{id}")
70
+ end
71
+ end
72
+ end
73
+ end
74
+
@@ -0,0 +1,62 @@
1
+ module Lelylan
2
+ class Client
3
+ module Subscription
4
+
5
+ #
6
+ # Public: Returns extended information for a given subscription identified from its ID.
7
+ #
8
+ # id - A String that represent the subscription ID.
9
+ #
10
+ # Returns Hashie The subscription.
11
+ #
12
+ def subscription(id)
13
+ get("/subscriptions/#{id}")
14
+ end
15
+
16
+ #
17
+ # Public: Returns a list of owned subscriptions.
18
+ #
19
+ # params - The Hash used to refine the search (default: {}).
20
+ #
21
+ # Returns Array List of subscriptions.
22
+ #
23
+ def subscriptions(params = {})
24
+ get('/subscriptions', params)
25
+ end
26
+
27
+ #
28
+ # Public: Create a subscription and returns extended information for it.
29
+ #
30
+ # params - The Hash used to create the resource (default: {}).
31
+ #
32
+ # Returns Hashie The created subscription.
33
+ #
34
+ def create_subscription(params = {})
35
+ post('/subscriptions', params)
36
+ end
37
+
38
+ #
39
+ # Public: Update a subscription identified from its ID and returns extended information for it.
40
+ #
41
+ # id - A String that represent the subscription ID.
42
+ # params - The Hash used to update the resource (default: {}).
43
+ #
44
+ # Returns Hashie The updated subscription.
45
+ #
46
+ def update_subscription(id, params = {})
47
+ put("/subscriptions/#{id}", params)
48
+ end
49
+
50
+ #
51
+ # Public: Delete a subscription identified from its ID and returns extended information for it.
52
+ #
53
+ # id - A String that represent the subscription ID.
54
+ #
55
+ # Returns Hashie The deleted subscription.
56
+ #
57
+ def delete_subscription(id)
58
+ delete("/subscriptions/#{id}")
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,73 @@
1
+ module Lelylan
2
+ class Client
3
+ module Type
4
+
5
+ #
6
+ # Public: Returns extended information for a given type identified from its ID.
7
+ #
8
+ # id - A String that represent the type ID.
9
+ #
10
+ # Returns Hashie The type.
11
+ #
12
+ def type(id)
13
+ get("/types/#{id}")
14
+ end
15
+
16
+ #
17
+ # Public: Returns a list of owned types.
18
+ #
19
+ # params - The Hash used to refine the search (default: {}).
20
+ #
21
+ # Returns Array List of types.
22
+ #
23
+ def types(params = {})
24
+ get('/types', params)
25
+ end
26
+
27
+ #
28
+ # Public: Returns a list of all existing types.
29
+ #
30
+ # params - The Hash used to refine the search (default: {}).
31
+ #
32
+ # Returns Array List of types.
33
+ #
34
+ def public_types(params = {})
35
+ get('/types/public', params)
36
+ end
37
+
38
+ #
39
+ # Public: Create a type and returns extended information for it.
40
+ #
41
+ # params - The Hash used to create the resource (default: {}).
42
+ #
43
+ # Returns Hashie The created type.
44
+ #
45
+ def create_type(params = {})
46
+ post('/types', params)
47
+ end
48
+
49
+ #
50
+ # Public: Update a type identified from its ID and returns extended information for it.
51
+ #
52
+ # id - A String that represent the type ID.
53
+ # params - The Hash used to update the resource (default: {}).
54
+ #
55
+ # Returns Hashie The updated type.
56
+ #
57
+ def update_type(id, params = {})
58
+ put("/types/#{id}", params)
59
+ end
60
+
61
+ #
62
+ # Public: Delete a type identified from its ID and returns extended information for it.
63
+ #
64
+ # id - A String that represent the type ID.
65
+ #
66
+ # Returns Hashie The deleted type.
67
+ #
68
+ def delete_type(id)
69
+ delete("/types/#{id}")
70
+ end
71
+ end
72
+ end
73
+ end
@@ -9,8 +9,8 @@ module Lelylan
9
9
  :api_endpoint,
10
10
  :web_endpoint,
11
11
  :endpoint,
12
- :user,
13
- :password,
12
+ :client_id,
13
+ :client_secret,
14
14
  :proxy,
15
15
  :token,
16
16
  :user_agent,
@@ -54,8 +54,8 @@ module Lelylan
54
54
  self.api_version = DEFAULT_API_VERSION
55
55
  self.api_endpoint = DEFAULT_API_ENDPOINT
56
56
  self.web_endpoint = DEFAULT_WEB_ENDPOINT
57
- self.user = nil
58
- self.password = nil
57
+ self.client_id = nil
58
+ self.client_secret = nil
59
59
  self.proxy = nil
60
60
  self.token = nil
61
61
  self.user_agent = DEFAULT_USER_AGENT
@@ -1,3 +1,4 @@
1
+ require 'base64'
1
2
  require 'faraday_middleware'
2
3
  require 'faraday/response/raise_http_error'
3
4
 
@@ -5,7 +6,7 @@ module Lelylan
5
6
  module Connection
6
7
  private
7
8
 
8
- def connection(authenticate=true, raw=false, version=0, force_urlencoded=false)
9
+ def connection(authenticate=true, raw=false, version=0, force_urlencoded=false, path='')
9
10
 
10
11
  options = {
11
12
  :headers => {'Accept' => 'application/json', 'User-Agent' => user_agent, 'Content-Type' => 'application/json'},
@@ -16,7 +17,13 @@ module Lelylan
16
17
 
17
18
  if authenticated?
18
19
  self.token = token.refresh! if token.expired?
19
- options[:headers].merge!('Authorization' => "Bearer #{token.token}")
20
+ options[:headers].merge!('Authorization' => "Bearer #{token.token}")
21
+ end
22
+
23
+ if path =~ /subscriptions/
24
+ raise Lelylan::Error, 'To make a request to the realtime services you need both client id and client secret' if (!client_id or !client_secret) and path =~ /subscriptions/
25
+ basic = Base64.encode64("#{self.client_id}:#{self.client_secret}")
26
+ options[:headers].merge!('Authorization' => "Bearer #{basic}")
20
27
  end
21
28
 
22
29
  connection = Faraday.new(options) do |builder|
@@ -28,20 +28,6 @@ module Lelylan
28
28
  request(:put, path, options, authenticate, raw, version, force_urlencoded)
29
29
  end
30
30
 
31
- # Internal: Gets the user requests limit.
32
- def ratelimit
33
- headers = get("/ratelimit",{}, true, true).headers
34
- return headers["X-RateLimit-Limit"].to_i
35
- end
36
- alias rate_limit ratelimit
37
-
38
- # Internal: Gets the remaining user requests.
39
- def ratelimit_remaining
40
- headers = get("/ratelimit",{}, api_version, true, true).headers
41
- return headers["X-RateLimit-Remaining"].to_i
42
- end
43
- alias rate_limit_remaining ratelimit_remaining
44
-
45
31
  private
46
32
 
47
33
  # Internal: Perform the HTTP request.
@@ -51,10 +37,10 @@ module Lelylan
51
37
  # options - The Hash options containing the params to send to the
52
38
  # service.
53
39
  # authenticate - The Boolean value that authenticate the user.
54
- # raw - The Boolean value let return the complete response.
40
+ # raw - The Boolean value let return the complete response.
55
41
  # force_urlencoded - The Boolean value that force the url encoding.
56
42
  def request(method, path, options, authenticate, raw, version, force_urlencoded)
57
- response = connection(authenticate, raw, version, force_urlencoded).send(method) do |request|
43
+ response = connection(authenticate, raw, version, force_urlencoded, path).send(method) do |request|
58
44
  case method
59
45
  when :delete, :get
60
46
  request.url(path, options)
@@ -2,7 +2,7 @@ module Lelylan
2
2
  class Version
3
3
  MAJOR = 0 unless defined? MAJOR
4
4
  MINOR = 0 unless defined? MINOR
5
- PATCH = 1 unless defined? PATCH
5
+ PATCH = 2 unless defined? PATCH
6
6
  PRE = nil unless defined? PRE
7
7
 
8
8
  class << self
@@ -0,0 +1,8 @@
1
+ {
2
+ "id": "5036227a4f1b030200009000",
3
+ "email": "reggie@lelylan.com",
4
+ "full_name": "Reggie",
5
+ "homepage": "http://lelylan.com",
6
+ "location": "New York",
7
+ "rate_limit": 5000
8
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "uri": "http://api.lelylan.com/subscriptions/508acdffd033a9ea77000026",
3
+ "id": "508acdffd033a9ea77000026",
4
+ "client": { "uri": "http://people.lelylan.com/oauth/applications/508acdffd033a9ea77025" },
5
+ "resource": "devices",
6
+ "event": "property-update",
7
+ "callback_uri": "http://callback.com/lelylan",
8
+ "created_at": "2012-10-26T17:53:03Z",
9
+ "updated_at": "2012-10-26T17:53:03Z"
10
+ }
@@ -0,0 +1,10 @@
1
+ [{
2
+ "uri": "http://api.lelylan.com/subscriptions/508acdffd033a9ea77000026",
3
+ "id": "508acdffd033a9ea77000026",
4
+ "client": { "uri": "http://people.lelylan.com/oauth/applications/508acdffd033a9ea77025" },
5
+ "resource": "devices",
6
+ "event": "property-update",
7
+ "callback_uri": "http://callback.com/lelylan",
8
+ "created_at": "2012-10-26T17:53:03Z",
9
+ "updated_at": "2012-10-26T17:53:03Z"
10
+ }]
@@ -0,0 +1,234 @@
1
+ require 'helper'
2
+
3
+ describe Lelylan::Client::Device do
4
+
5
+ let(:lelylan) do
6
+ a_client
7
+ end
8
+
9
+ describe '#device' do
10
+
11
+ before do
12
+ stub_get('/devices/1').to_return(body: fixture('device.json'))
13
+ end
14
+
15
+ let!(:device) do
16
+ lelylan.device('1')
17
+ end
18
+
19
+ it 'returns the device' do
20
+ device.id.should_not be_nil
21
+ end
22
+
23
+ it 'sends the request' do
24
+ a_get('/devices/1').should have_been_made
25
+ end
26
+ end
27
+
28
+
29
+ describe '#device_privates' do
30
+
31
+ before do
32
+ stub_get('/devices/1/privates').to_return(body: fixture('device.json'))
33
+ end
34
+
35
+ let!(:device) do
36
+ lelylan.device_privates('1')
37
+ end
38
+
39
+ it 'returns the device' do
40
+ device.id.should_not be_nil
41
+ end
42
+
43
+ it 'sends the request' do
44
+ a_get('/devices/1/privates').should have_been_made
45
+ end
46
+ end
47
+
48
+
49
+ describe '#devices' do
50
+
51
+ before do
52
+ stub_get('/devices').to_return(body: fixture('devices.json'))
53
+ end
54
+
55
+ let!(:devices) do
56
+ lelylan.devices
57
+ end
58
+
59
+ it 'returns a list of devices' do
60
+ devices.should have(1).item
61
+ end
62
+
63
+ it 'sends the request' do
64
+ a_get('/devices').should have_been_made
65
+ end
66
+
67
+ context 'with params' do
68
+
69
+ before do
70
+ stub_get('/devices').with(query: { per: '25' }).to_return(body: fixture('device.json'))
71
+ end
72
+
73
+ before do
74
+ lelylan.devices(per: 25)
75
+ end
76
+
77
+ it 'sends the params' do
78
+ a_get('/devices').with(query: { per: '25' }).should have_been_made
79
+ end
80
+ end
81
+ end
82
+
83
+
84
+ describe '#create_device' do
85
+
86
+ before do
87
+ stub_post('/devices').with(body: {name: 'Dimmer'}).to_return(body: fixture('device.json'))
88
+ end
89
+
90
+ let!(:device) do
91
+ lelylan.create_device(name: 'Dimmer')
92
+ end
93
+
94
+ it 'returns the device' do
95
+ device.id.should_not be_nil
96
+ end
97
+
98
+ it 'sends the request' do
99
+ a_post('/devices').with(body: {name: 'Dimmer'}).should have_been_made
100
+ end
101
+ end
102
+
103
+
104
+ describe '#update_device' do
105
+
106
+ before do
107
+ stub_put('/devices/1').with(body: {name: 'Dimmer'}).to_return(body: fixture('device.json'))
108
+ end
109
+
110
+ let!(:device) do
111
+ lelylan.update_device('1', name: 'Dimmer')
112
+ end
113
+
114
+ it 'returns the device' do
115
+ device.id.should_not be_nil
116
+ end
117
+
118
+ it 'sends the request' do
119
+ a_put('/devices/1').with(body: {name: 'Dimmer'}).should have_been_made
120
+ end
121
+ end
122
+
123
+
124
+ describe '#delete_device' do
125
+
126
+ before do
127
+ stub_delete('/devices/1').to_return(body: fixture('device.json'))
128
+ end
129
+
130
+ let!(:device) do
131
+ lelylan.delete_device('1')
132
+ end
133
+
134
+ it 'returns the device' do
135
+ device.id.should_not be_nil
136
+ end
137
+
138
+ it 'sends the request' do
139
+ a_delete('/devices/1').should have_been_made
140
+ end
141
+ end
142
+
143
+
144
+ describe '#execute' do
145
+
146
+ let(:function) do
147
+ 'http://api.lelylan.com/functions/4dcb9e23d033a9088900020a'
148
+ end
149
+
150
+ let(:properties) do
151
+ [ { uri: 'http://api.lelylan.com/properties/4dcb9e23d033a9088900020a', value: '50' } ]
152
+ end
153
+
154
+ before do
155
+ stub_put('/devices/1/functions').with(body: { properties: properties, function: function }).to_return(body: fixture('device.json'))
156
+ end
157
+
158
+ let!(:device) do
159
+ lelylan.execute('1', { function: function, properties: properties })
160
+ end
161
+
162
+ it 'returns the updated device' do
163
+ device.id.should_not be_nil
164
+ end
165
+
166
+ it 'sends the request' do
167
+ a_put('/devices/1/functions').with(body: { properties: properties, function: function }).should have_been_made
168
+ end
169
+ end
170
+
171
+
172
+ describe '#device_properties' do
173
+
174
+ let(:properties) do
175
+ [{uri: 'http://api.lelylan.com/properties/4dcb9e23d033a9088900020a', value: '50'}]
176
+ end
177
+
178
+ before do
179
+ stub_put("/devices/1/properties").with(body: { properties: properties }).to_return(body: fixture('device.json'))
180
+ end
181
+
182
+ let!(:device) do
183
+ lelylan.device_properties('1', properties: properties)
184
+ end
185
+
186
+ it 'returns the updated device' do
187
+ device.id.should_not be_nil
188
+ end
189
+
190
+ it 'sends the request' do
191
+ a_put('/devices/1/properties').with(body: { properties: properties }).should have_been_made
192
+ end
193
+ end
194
+
195
+
196
+ describe '#activate_device' do
197
+
198
+ before do
199
+ stub_post('/activations').with(body: { activation_code: '1' }).to_return(body: fixture('device.json'))
200
+ end
201
+
202
+ let!(:device) do
203
+ lelylan.activate_device(activation_code: '1')
204
+ end
205
+
206
+ it 'returns the device' do
207
+ device.id.should_not be_nil
208
+ end
209
+
210
+ it 'sends the request' do
211
+ a_post('/activations').with(body: { activation_code: '1' }).should have_been_made
212
+ end
213
+ end
214
+
215
+
216
+ describe '#deactivate_device' do
217
+
218
+ before do
219
+ stub_delete('/activations/1').to_return(body: fixture('device.json'))
220
+ end
221
+
222
+ let!(:device) do
223
+ lelylan.deactivate_device('1')
224
+ end
225
+
226
+ it 'returns the device' do
227
+ device.id.should_not be_nil
228
+ end
229
+
230
+ it 'sends the request' do
231
+ a_delete('/activations/1').should have_been_made
232
+ end
233
+ end
234
+ end