cs 0.1.0beta3 → 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.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +8 -0
  3. data/Gemfile +22 -6
  4. data/README.md +15 -4
  5. data/cs.gemspec +7 -7
  6. data/lib/{commonsense-ruby-lib.rb → cs.rb} +80 -31
  7. data/lib/{commonsense-ruby-lib → cs}/auth/http.rb +52 -33
  8. data/lib/{commonsense-ruby-lib → cs}/auth/oauth.rb +28 -28
  9. data/lib/cs/collection.rb +7 -0
  10. data/lib/cs/collection/sensor_data_collection.rb +61 -0
  11. data/lib/{commonsense-ruby-lib → cs}/end_point.rb +36 -24
  12. data/lib/cs/end_point/group.rb +26 -0
  13. data/lib/cs/end_point/notification.rb +16 -0
  14. data/lib/{commonsense-ruby-lib → cs}/end_point/sensor.rb +22 -6
  15. data/lib/{commonsense-ruby-lib → cs}/end_point/sensor_data.rb +17 -6
  16. data/lib/cs/end_point/trigger.rb +16 -0
  17. data/lib/{commonsense-ruby-lib → cs}/end_point/user.rb +8 -4
  18. data/lib/{commonsense-ruby-lib → cs}/error.rb +7 -1
  19. data/lib/cs/parameter_processor.rb +99 -0
  20. data/lib/cs/relation.rb +244 -0
  21. data/lib/cs/relation/group_relation.rb +24 -0
  22. data/lib/cs/relation/notification_relation.rb +20 -0
  23. data/lib/{commonsense-ruby-lib → cs}/relation/sensor_data_relation.rb +7 -52
  24. data/lib/{commonsense-ruby-lib → cs}/relation/sensor_relation.rb +28 -55
  25. data/lib/cs/relation/trigger_relation.rb +21 -0
  26. data/lib/cs/relation/user_relation.rb +20 -0
  27. data/lib/{commonsense-ruby-lib → cs}/serializer.rb +1 -1
  28. data/lib/cs/session.rb +170 -0
  29. data/lib/cs/time.rb +45 -0
  30. data/lib/cs/version.rb +3 -0
  31. data/spec/features/sensor_management_spec.rb +146 -45
  32. data/spec/features/user_management_spec.rb +94 -22
  33. data/spec/lib/cs/collection/sensor_data_collection_spec.rb +27 -0
  34. data/spec/lib/cs/end_point/group_spec.rb +120 -0
  35. data/spec/lib/cs/end_point/sensor_data_spec.rb +110 -0
  36. data/spec/lib/{commonsense-ruby-lib → cs}/end_point/sensor_spec.rb +6 -6
  37. data/spec/lib/{commonsense-ruby-lib → cs}/end_point/user_spec.rb +14 -7
  38. data/spec/lib/{commonsense-ruby-lib → cs}/end_point_spec.rb +25 -12
  39. data/spec/lib/cs/relation/group_relation_spec.rb +103 -0
  40. data/spec/lib/cs/relation/sensor_data_relation_spec.rb +184 -0
  41. data/spec/lib/cs/relation/sensor_relation_spec.rb +192 -0
  42. data/spec/lib/cs/relation/user_relation_spec.rb +81 -0
  43. data/spec/lib/cs/relation_spec.rb +151 -0
  44. data/spec/lib/cs/session_spec.rb +91 -0
  45. data/spec/lib/cs/time_spec.rb +71 -0
  46. data/spec/lib/cs_spec.rb +85 -0
  47. data/spec/spec_helper.rb +6 -26
  48. metadata +69 -86
  49. data/lib/commonsense-ruby-lib/end_point/group.rb +0 -28
  50. data/lib/commonsense-ruby-lib/relation.rb +0 -233
  51. data/lib/commonsense-ruby-lib/session.rb +0 -105
  52. data/lib/commonsense-ruby-lib/version.rb +0 -3
  53. data/spec/lib/commonsense-ruby-lib/end_point/sensor_data_spec.rb +0 -68
  54. data/spec/lib/commonsense-ruby-lib/relation/sensor_data_relation_spec.rb +0 -444
  55. data/spec/lib/commonsense-ruby-lib/relation/sensor_relation_spec.rb +0 -165
  56. data/spec/lib/commonsense-ruby-lib/session_spec.rb +0 -43
  57. data/spec/lib/commonsense-ruby-lib_spec.rb +0 -51
@@ -1,105 +0,0 @@
1
- module CommonSense
2
- class Session
3
- def initialize(opts={})
4
- options = {
5
- base_uri: 'https://api.sense-os.nl',
6
- authentication: true
7
- }.merge(opts)
8
- @base_uri = options[:base_uri]
9
- @auth_proxy = options[:authentication] ? nil : CommonSense::Auth::HTTP.new(@base_uri)
10
- end
11
-
12
- # login to commonsense
13
- # @return [String] session_id
14
- def login(username, password)
15
- @auth_proxy = CommonSense::Auth::HTTP.new(@base_uri)
16
- @auth_proxy.login(username, password)
17
- end
18
-
19
- def oauth(consumer_key, consumer_secret, access_token, access_token_secret)
20
- @auth_proxy = CommonSense::Auth::OAuth.new(consumer_key, consumer_secret,
21
- access_token, access_token_secret,
22
- @base_uri)
23
- end
24
-
25
- def session_id
26
- auth_proxy.session_id
27
- end
28
-
29
- def session_id=(session_id)
30
- @auth_proxy = CommonSense::Auth::HTTP.new(@base_uri)
31
- @auth_proxy.session_id = session_id
32
- end
33
-
34
- def auth_proxy
35
- raise 'The session is not logged in' unless @auth_proxy
36
- @auth_proxy
37
- end
38
-
39
- def get(path, body = '', headers = {})
40
- auth_proxy.get(path, body, headers)
41
- end
42
-
43
- def post(path, body = '', headers = {})
44
- auth_proxy.post(path, body, headers = {})
45
- end
46
-
47
- def put(path, body = '', headers = {})
48
- auth_proxy.put(path, body, headers)
49
- end
50
-
51
- def delete(path, body='', headers = {})
52
- auth_proxy.delete(path, body, headers)
53
- end
54
-
55
- def head(path, headers = {})
56
- auth_proxy.head(path, headers)
57
- end
58
-
59
- def response_code
60
- auth_proxy.response_code
61
- end
62
-
63
- def response_body
64
- auth_proxy.response_body
65
- end
66
-
67
- def response_headers
68
- auth_proxy.response_headers
69
- end
70
-
71
- def errors
72
- auth_proxy.errors
73
- end
74
-
75
- def base_uri=(uri = nil)
76
- auth_proxy.base_uri = uri
77
- end
78
-
79
- def dump_to_text(path)
80
- File.open(path, 'w') do |f|
81
- f.write("Response Code: #{response_code}\n")
82
- f.write("Response Headers: #{response_headers}\n")
83
- f.write("Errors: #{errors}\n")
84
- f.write("\n")
85
- f.write(response_body)
86
- end
87
- end
88
-
89
- def open_in_browser(path=nil)
90
- require 'launchy'
91
-
92
- path ||= "/tmp/common-sense-ruby-#{Time.now.to_i}.html"
93
- dump_to_text(path)
94
- ::Launchy::Browser.run(path)
95
- end
96
-
97
- def to_s
98
- "\"#{self.session_id}\""
99
- end
100
-
101
- def inspect
102
- auth_proxy.kind_of?(CommonSense::Auth::HTTP) ? to_s : "OAuth"
103
- end
104
- end
105
- end
@@ -1,3 +0,0 @@
1
- module CommonSense
2
- VERSION = "0.1.0beta3"
3
- end
@@ -1,68 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module CommonSense
4
- module EndPoint
5
- describe SensorData do
6
-
7
- let!(:value) do
8
- {"x-axis" => 1.0, "y-axis" => 2.0, "z-axis" => 3.0}
9
- end
10
-
11
- let!(:now) do
12
- Time.now.to_f
13
- end
14
-
15
- describe "Initiating new data point" do
16
- it "should assign the data point property on initialize" do
17
- data = SensorData.new(sensor_id: 1, date: now, value: value)
18
-
19
- data.sensor_id.should eq(1)
20
- data.date.should eq(now)
21
- data.value.should eq(value)
22
- end
23
- end
24
-
25
- describe "Creating" do
26
- it "should create a new data point" do
27
- data = SensorData.new(sensor_id: 1, date: now, value: value)
28
-
29
- session = double("CommonSense::Session")
30
- session.should_receive(:post).with("/sensors/1/data.json", {data: [{date: now, value: value.to_json}]})
31
- session.stub(:response_headers => {"location" => "http://foo.bar/sensors/1/data/1"})
32
- session.stub(:response_code => 201)
33
- data.session = session
34
-
35
- data.create!.should be_true
36
- end
37
- end
38
-
39
- describe "Get specific data point" do
40
- it "should request data point from commonSense" do
41
- data = SensorData.new
42
- expect { data.retrieve!.should }.to raise_error(Error::NotImplementedError)
43
- end
44
- end
45
-
46
- describe "Update specific data point" do
47
- it "should request data point from commonSense" do
48
- data = SensorData.new
49
- expect { data.retrieve!.should }.to raise_error(Error::NotImplementedError)
50
- end
51
- end
52
-
53
- describe "Delete specific data point" do
54
- it "should perform DELETE request to commonSense" do
55
- data = SensorData.new(sensor_id: 1, id: "abcdef")
56
-
57
- session = double("CommonSense::Session")
58
- session.should_receive(:delete).with("/sensors/1/data/abcdef.json")
59
- session.stub(:response_code => 200)
60
- data.session = session
61
-
62
- data.delete!.should be_true
63
- end
64
- end
65
-
66
- end
67
- end
68
- end
@@ -1,444 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module CommonSense
4
- module Relation
5
- describe SensorDataRelation do
6
-
7
- let(:relation) do
8
- relation = SensorDataRelation.new
9
- relation.stub("check_session!").and_return(true)
10
- relation.stub("get_data").and_return(sensors)
11
- relation
12
- end
13
-
14
- let(:data) do
15
- {
16
- "data" => [{
17
- "id" => "5150e509b4b735f6290238d3",
18
- "sensor_id" => "1",
19
- "value" => "{\"x-axis\":0.259,\"y-axis\":-0.15,\"z-axis\":-9.807}",
20
- "date" => 1364256004.651,
21
- "month" => 3,
22
- "week" => 13,
23
- "year" => 2013
24
- },
25
- {
26
- "id" => "5150e760b4b735fa29027b27",
27
- "sensor_id" => "1",
28
- "value" => "{\"x-axis\":0.191,\"y-axis\":0.069,\"z-axis\":-9.875}",
29
- "date" => 1364256603.675,
30
- "month" => 3,
31
- "week" => 13,
32
- "year" => 2013
33
- },
34
- {
35
- "id" => "5150e9b7b4b735d04e010ed9",
36
- "sensor_id" => "1",
37
- "value" => "{\"x-axis\":0.191,\"y-axis\":-0.028,\"z-axis\":-9.766}",
38
- "date" => 1364257203.315,
39
- "month" => 3,
40
- "week" => 13,
41
- "year" => 2013
42
- }]
43
- }
44
- end
45
-
46
- let(:relation) do
47
- sensor_id = "1"
48
- relation = SensorDataRelation.new(sensor_id)
49
- relation.stub("check_session!").and_return(true)
50
- relation.stub("get_data!").and_return(data)
51
- relation
52
- end
53
-
54
- describe "build" do
55
- it "should return a sensorData object" do
56
- sensor_id = 1
57
- SensorDataRelation.new(sensor_id).build.should be_a_kind_of(EndPoint::SensorData)
58
- end
59
- end
60
-
61
- describe "get_data!" do
62
- it "should fetch the data point from commonSense" do
63
- sensor_id = 1
64
- session = double("Session")
65
- option = { page: 100, per_page: 99, start_date: 1365278885,
66
- end_date: 1365278886, last: 1, sort: 'ASC', interval: 300 }
67
- session.should_receive(:get).with("/sensors/#{sensor_id}/data.json", option)
68
-
69
- relation = SensorDataRelation.new(sensor_id, session)
70
- relation.get_data!(page:100, per_page: 99, start_date: 1365278885,
71
- end_date: 1365278886, last: true, sort: 'ASC', interval: 300)
72
- end
73
- end
74
-
75
- describe "get_data" do
76
- it "call get_data! and not throw exception" do
77
- sensor_id = 1
78
- relation = SensorDataRelation.new(sensor_id)
79
- relation.stub(:get_data!).and_return { raise Error }
80
-
81
- expect { relation.get_data }.to_not raise_error
82
- relation.get_data.should be_nil
83
- end
84
- end
85
-
86
- describe "each" do
87
- it "should get all sensor data based on the criteria and yield" do
88
- expect { |b| relation.each(&b) }.to yield_successive_args(EndPoint::SensorData, EndPoint::SensorData, EndPoint::SensorData)
89
- end
90
- end
91
-
92
- describe "count" do
93
- it "should return the total number of sensor data match with criteria" do
94
- relation.count.should eq(3)
95
- end
96
- end
97
-
98
- describe "first" do
99
- it "should return the first record" do
100
- session = double("Session")
101
- option = { page: 0, per_page: 1, sort: 'ASC'}
102
- sensor_id = "1"
103
- response = {
104
- "data" => [{
105
- "id" => "5150e509b4b735f6290238d3",
106
- "sensor_id" => sensor_id,
107
- "value" => "{\"x-axis\":0.259,\"y-axis\":-0.15,\"z-axis\":-9.807}",
108
- "date" => 1364256004.651,
109
- "month" => 3,
110
- "week" => 13,
111
- "year" => 2013
112
- }]
113
- }
114
-
115
- session.should_receive(:get).with("/sensors/#{sensor_id}/data.json", option).and_return(response)
116
- relation = SensorDataRelation.new(sensor_id, session)
117
-
118
- first = relation.first
119
- first.id.should eq("5150e509b4b735f6290238d3")
120
- first.sensor_id.should eq("1")
121
- first.value.should eq("{\"x-axis\":0.259,\"y-axis\":-0.15,\"z-axis\":-9.807}")
122
- first.date.should eq(1364256004.651)
123
- first.month.should eq(3)
124
- first.week.should eq(13)
125
- first.year.should eq(2013)
126
- end
127
- end
128
-
129
- describe "last" do
130
- it "should return the last record" do
131
- session = double("Session")
132
- option = { page: 0, per_page: 1, sort: 'DESC'}
133
- sensor_id = "1"
134
- response = {
135
- "data" => [{
136
- "id" => "5150e9b7b4b735d04e010ed9",
137
- "sensor_id" => sensor_id,
138
- "value" => "{\"x-axis\":0.191,\"y-axis\":-0.028,\"z-axis\":-9.766}",
139
- "date" => 1364257203.315,
140
- "month" => 3,
141
- "week" => 13,
142
- "year" => 2013
143
- }]
144
- }
145
- session.should_receive(:get).with("/sensors/#{sensor_id}/data.json", option).and_return(response)
146
- relation = SensorDataRelation.new(sensor_id, session)
147
-
148
- last = relation.last
149
- last.id.should eq("5150e9b7b4b735d04e010ed9")
150
- last.sensor_id.should eq("1")
151
- last.value.should eq("{\"x-axis\":0.191,\"y-axis\":-0.028,\"z-axis\":-9.766}")
152
- last.date.should eq(1364257203.315)
153
- last.month.should eq(3)
154
- last.week.should eq(13)
155
- last.year.should eq(2013)
156
- end
157
- end
158
-
159
- describe "where" do
160
- before(:each) do
161
- sensor_id = 1
162
- @relation = SensorDataRelation.new(sensor_id)
163
- @relation.stub("check_session!").and_return(true)
164
- end
165
-
166
- describe "page" do
167
- it "should update page" do
168
- @relation.where(page: 2)
169
- @relation.page.should eq(2)
170
- end
171
- end
172
-
173
- describe "per_page" do
174
- it "should update per_page" do
175
- @relation.where(per_page: 99)
176
- @relation.per_page.should eq(99)
177
- end
178
- end
179
-
180
- describe "start_date" do
181
- describe "number given" do
182
- it "should update the start_date" do
183
- @relation.where(start_date: 2)
184
- @relation.start_date.to_f.should eq(2.0)
185
- @relation.start_date.should be_kind_of(Time)
186
- end
187
- end
188
-
189
- describe "Time given" do
190
- it "should update the start_date" do
191
- @relation.where(start_date: Time.at(19))
192
- @relation.start_date.to_f.should eq(19)
193
- end
194
- end
195
-
196
- describe "Object that respond to 'to_time` given" do
197
- it "should update the start_date" do
198
- double = double()
199
- double.should_receive(:to_time).and_return (Time.at(2))
200
- @relation.where(start_date: double)
201
- @relation.start_date.to_f.should eq(2.0)
202
- @relation.start_date.should be_kind_of(Time)
203
- end
204
- end
205
-
206
- describe "Object that not respond to 'to_time' given" do
207
- it "should raise error" do
208
- expect { @relation.where(end_date: 'foo') }.to raise_error(NoMethodError)
209
- end
210
- end
211
- end
212
-
213
-
214
- describe "end_date" do
215
- describe "number given" do
216
- it "should update the end_date" do
217
- @relation.where(end_date: 2)
218
- @relation.end_date.to_f.should eq(2.0)
219
- @relation.end_date.should be_kind_of(Time)
220
- end
221
- end
222
-
223
- describe "Time given" do
224
- it "should update the end_date" do
225
- @relation.where(end_date: Time.at(19))
226
- @relation.end_date.to_f.should eq(19)
227
- end
228
- end
229
-
230
- describe "Object that respond to 'to_time` given" do
231
- it "should update the end_date" do
232
- double = double()
233
- double.should_receive(:to_time).and_return (Time.at(2))
234
- @relation.where(end_date: double)
235
- @relation.end_date.to_f.should eq(2.0)
236
- @relation.end_date.should be_kind_of(Time)
237
- end
238
- end
239
-
240
- describe "Object that not respond to 'to_time' given" do
241
- it "should raise error" do
242
- expect { @relation.where(end_date: 'foo') }.to raise_error(NoMethodError)
243
- end
244
- end
245
- end
246
-
247
- describe "last" do
248
- describe "with boolean value" do
249
- it "should update the last parameter" do
250
- [true, false].each do |value|
251
- @relation.where(last: value)
252
- @relation.parameter(:last).should be_true
253
- end
254
- end
255
- end
256
-
257
- describe "with invalid parameter" do
258
- it "should assign true" do
259
- @relation.where(last: 'a')
260
- @relation.parameter(:last).should be_true
261
- end
262
- end
263
- end
264
-
265
- describe "sort" do
266
- describe "valid parameter given" do
267
- it "should update the sort parameter" do
268
- @relation.where(sort: 'ASC')
269
- @relation.parameter(:sort).should eq('ASC')
270
-
271
- @relation.where(sort: 'DESC')
272
- @relation.parameter(:sort).should eq('DESC')
273
- end
274
- end
275
-
276
- describe "invalid parameter given" do
277
- it "should set parameter to nil" do
278
- expect { @relation.where(sort: 'foo') }.to raise_error(ArgumentError)
279
- end
280
- end
281
- end
282
-
283
- describe "interval" do
284
- describe "valid parameter given" do
285
- it "should update the sort parameter" do
286
- [604800, 86400, 3600, 1800, 600, 300, 60].each do |interval|
287
- @relation.where(interval: interval)
288
- @relation.interval.should eq(interval)
289
- end
290
- end
291
- end
292
-
293
- describe "invalid parameter given" do
294
- it "should set the parameter to nil" do
295
- expect { @relation.where(interval: 10) }.to raise_error(ArgumentError)
296
- expect { @relation.where(interval: 'a') }.to raise_error(ArgumentError)
297
- end
298
- end
299
- end
300
-
301
- describe "from" do
302
- describe "number given" do
303
- it "should update the end_date" do
304
- @relation.where(from: 2)
305
- @relation.start_date.to_f.should eq(2.0)
306
- @relation.start_date.should be_kind_of(Time)
307
- end
308
- end
309
-
310
- describe "Time given" do
311
- it "should update the end_date" do
312
- @relation.where(from: Time.at(19))
313
- @relation.start_date.to_f.should eq(19)
314
- end
315
- end
316
-
317
- describe "Object that respond to 'to_time` given" do
318
- it "should update the end_date" do
319
- double = double()
320
- double.should_receive(:to_time).and_return (Time.at(2))
321
- @relation.where(from: double)
322
- @relation.start_date.to_f.should eq(2.0)
323
- @relation.start_date.should be_kind_of(Time)
324
- end
325
- end
326
-
327
- describe "Object that not respond to 'to_time' given" do
328
- it "should raise error" do
329
- expect { @relation.where(from: 'foo') }.to raise_error(NoMethodError)
330
- end
331
- end
332
- end
333
-
334
- describe "to" do
335
- describe "number given" do
336
- it "should update the end_date" do
337
- @relation.where(to: 2)
338
- @relation.end_date.to_f.should eq(2.0)
339
- @relation.end_date.should be_kind_of(Time)
340
- end
341
- end
342
-
343
- describe "Time given" do
344
- it "should update the end_date" do
345
- @relation.where(to: Time.at(19))
346
- @relation.end_date.to_f.should eq(19)
347
- end
348
- end
349
-
350
- describe "Object that respond to 'to_time` given" do
351
- it "should update the end_date" do
352
- double = double()
353
- double.should_receive(:to_time).and_return (Time.at(2))
354
- @relation.where(to: double)
355
- @relation.end_date.to_f.should eq(2.0)
356
- @relation.end_date.should be_kind_of(Time)
357
- end
358
- end
359
-
360
- describe "Object that not respond to 'to_time' given" do
361
- it "should raise error" do
362
- expect { @relation.where(to: 'foo') }.to raise_error(NoMethodError)
363
- end
364
- end
365
- end
366
- end
367
-
368
- describe "from" do
369
- describe "number given" do
370
- it "should update the end_date" do
371
- relation.from(2)
372
- relation.start_date.to_f.should eq(2.0)
373
- relation.start_date.should be_kind_of(Time)
374
- end
375
- end
376
-
377
- describe "Time given" do
378
- it "should update the end_date" do
379
- relation.from(Time.at(19))
380
- relation.start_date.to_f.should eq(19)
381
- end
382
- end
383
-
384
- describe "Object that respond to 'to_time` given" do
385
- it "should update the end_date" do
386
- double = double()
387
- double.should_receive(:to_time).and_return (Time.at(2))
388
- relation.from(double)
389
- relation.start_date.to_f.should eq(2.0)
390
- relation.start_date.should be_kind_of(Time)
391
- end
392
- end
393
-
394
- describe "Object that not respond to 'to_time' given" do
395
- it "should raise error" do
396
- expect { relation.from('foo') }.to raise_error(NoMethodError)
397
- end
398
- end
399
- end
400
-
401
- describe "to" do
402
- describe "number given" do
403
- it "should update the end_date" do
404
- relation.to(2)
405
- relation.end_date.to_f.should eq(2.0)
406
- relation.end_date.should be_kind_of(Time)
407
- end
408
- end
409
-
410
- describe "Time given" do
411
- it "should update the end_date" do
412
- relation.to(Time.at(19))
413
- relation.end_date.to_f.should eq(19)
414
- end
415
- end
416
-
417
- describe "Object that respond to 'to_time` given" do
418
- it "should update the end_date" do
419
- double = double()
420
- double.should_receive(:to_time).and_return (Time.at(2))
421
- relation.to(double)
422
- relation.end_date.to_f.should eq(2.0)
423
- relation.end_date.should be_kind_of(Time)
424
- end
425
- end
426
-
427
- describe "Object that not respond to 'to_time' given" do
428
- it "should raise error" do
429
- expect { relation.to('foo') }.to raise_error(NoMethodError)
430
- end
431
- end
432
- end
433
-
434
- describe "all" do
435
- it "return array of all matching sensor data" do
436
- data = relation.to_a
437
- data.should be_a_kind_of(Array)
438
- data.size.should eq(3)
439
- data[0].should be_a_kind_of(EndPoint::SensorData)
440
- end
441
- end
442
- end
443
- end
444
- end