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
@@ -0,0 +1,103 @@
1
+ require 'spec_helper'
2
+
3
+ module CS
4
+ module Relation
5
+ describe GroupRelation do
6
+
7
+ let(:relation) do
8
+ relation = GroupRelation.new
9
+ relation.stub("check_session!").and_return(true)
10
+ relation.stub("get_data").and_return(groups)
11
+ relation
12
+ end
13
+
14
+ let(:groups) do
15
+ {
16
+ "groups" => [
17
+ {
18
+ "accepted" => false,
19
+ "id" => "4765",
20
+ "name" => "Group 1",
21
+ "description" => "Group 1",
22
+ "public" => false
23
+ },
24
+ {
25
+ "accepted" => false,
26
+ "id" => "3155",
27
+ "name" => "Testgroep",
28
+ "description" => "",
29
+ "public" => true
30
+ },
31
+ {
32
+ "accepted" => true,
33
+ "id" => "6072",
34
+ "name" => "ahmy test group",
35
+ "description" => "ahmy test group",
36
+ "public" => true
37
+ }
38
+ ]
39
+ }
40
+ end
41
+
42
+ let(:relation) do
43
+ relation = GroupRelation.new
44
+ relation.stub("check_session!").and_return(true)
45
+ relation.stub("get_data!").and_return(groups)
46
+ relation
47
+ end
48
+
49
+ describe "get_data!" do
50
+ it "should fetch groups from commonSense" do
51
+ session = double('Session')
52
+ option = {page: 100, per_page: 99, public: 1, total:1, sort: "ASC", sort_field: "name"}
53
+ session.should_receive(:get).with("/groups.json", option)
54
+
55
+ relation = GroupRelation.new(session)
56
+ relation.get_data!(page: 100, per_page: 99, public:1, total:1, sort:"ASC", sort_field:"name")
57
+ end
58
+ end
59
+
60
+ describe "each" do
61
+ it "should get all users group and yield each" do
62
+ session = double('Session')
63
+ relation = GroupRelation.new(session)
64
+ relation.stub("get_data!").and_return(groups)
65
+
66
+ expect { |b| relation.each(&b) }.to yield_successive_args(EndPoint::Group, EndPoint::Group, EndPoint::Group)
67
+ end
68
+
69
+ context "limit specified" do
70
+ it "should yield sensor at most specified by limit" do
71
+ relation.limit(1).to_a.count.should eq(1)
72
+ end
73
+ end
74
+ end
75
+
76
+ describe "count" do
77
+ it "should return the total number of record" do
78
+ relation.count.should eq(3)
79
+ end
80
+ end
81
+
82
+ describe "first" do
83
+ it "should return the first record" do
84
+ first = relation.first
85
+ first.should be_kind_of(EndPoint::Group)
86
+ first.name.should eq("Group 1")
87
+ end
88
+ end
89
+
90
+ describe "last" do
91
+ it "should return the last record" do
92
+ relation = GroupRelation.new
93
+ relation.stub("count").and_return(3)
94
+ relation.should_receive("get_data").with(page:2, per_page:1, public:1, total:1, sort:'ASC', sort_field:'email').and_return({"groups" => [{"name" => "Group 1"}], "total" => 3})
95
+
96
+ first = relation.where(public: true, total: true, sort:'ASC', sort_field:'email').last
97
+ first.should be_kind_of(EndPoint::Group)
98
+ first.name.should eq("Group 1")
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,184 @@
1
+ require 'spec_helper'
2
+
3
+ module CS
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
+ date = 1
58
+ value = 1
59
+ sensor_data = SensorDataRelation.new(sensor_id).build(date: date, value: value)
60
+ sensor_data.should be_a_kind_of(EndPoint::SensorData)
61
+ sensor_data.sensor_id.should == sensor_id
62
+ sensor_data.date.to_f.should == date
63
+ sensor_data.value.should == value
64
+ end
65
+ end
66
+
67
+ describe "get_data!" do
68
+ it "should fetch the data point from commonSense" do
69
+ sensor_id = 1
70
+ session = double("Session")
71
+ option = { page: 100, per_page: 99, start_date: 1365278885,
72
+ end_date: 1365278886, last: 1, sort: 'ASC', interval: 300, sensor_id: sensor_id}
73
+ session.should_receive(:get).with("/sensors/#{sensor_id}/data.json", option)
74
+
75
+ relation = SensorDataRelation.new(sensor_id, session)
76
+ relation.get_data!(page:100, per_page: 99, start_date: 1365278885,
77
+ end_date: 1365278886, last: true, sort: 'ASC', interval: 300)
78
+ end
79
+ end
80
+
81
+ describe "each_batch" do
82
+ it "should yield data to with multiple pages" do
83
+ sensor_id = 1
84
+ session = double("Session")
85
+ relation = SensorDataRelation.new(sensor_id, session)
86
+ relation.should_receive(:get_data!).once.ordered.with({page: 0, per_page: 3, sensor_id: sensor_id})
87
+ .and_return (data)
88
+ relation.should_receive(:get_data!).once.ordered.with({page: 1, per_page: 3, sensor_id: sensor_id})
89
+ .and_return (data)
90
+ relation.should_receive(:get_data!).once.ordered.with({page: 2, per_page: 3, sensor_id: sensor_id})
91
+ .and_return ({ "data" => [] })
92
+
93
+ relation.page = 0
94
+ relation.per_page = 3
95
+ relation.each_batch {}
96
+ end
97
+
98
+ end
99
+
100
+ describe "each" do
101
+ it "should get all sensor data based on the criteria and yield" do
102
+ expect { |b| relation.each(&b) }.to yield_successive_args(EndPoint::SensorData, EndPoint::SensorData, EndPoint::SensorData)
103
+ end
104
+
105
+ it "should return an sensor data with sensor_id" do
106
+ relation.each {|data| data.sensor_id.should == "1" }
107
+ end
108
+
109
+ context "limit specified" do
110
+ it "should yield sensor at most specified by limit" do
111
+ relation.limit(1).to_a.count.should eq(1)
112
+ end
113
+ end
114
+ end
115
+
116
+ describe "count" do
117
+ it "should return the total number of sensor data match with criteria" do
118
+ relation.count.should eq(3)
119
+ end
120
+ end
121
+
122
+ describe "first" do
123
+ it "should return the first record" do
124
+ session = double("Session")
125
+ sensor_id = "1"
126
+ option = { page: 0, per_page: 1, sort: 'ASC', sensor_id: sensor_id}
127
+ response = {
128
+ "data" => [{
129
+ "id" => "5150e509b4b735f6290238d3",
130
+ "sensor_id" => sensor_id,
131
+ "value" => "{\"x-axis\":0.259,\"y-axis\":-0.15,\"z-axis\":-9.807}",
132
+ "date" => 1364256004.651,
133
+ "month" => 3,
134
+ "week" => 13,
135
+ "year" => 2013
136
+ }]
137
+ }
138
+
139
+ session.should_receive(:get).with("/sensors/#{sensor_id}/data.json", option).and_return(response)
140
+ relation = SensorDataRelation.new(sensor_id, session)
141
+
142
+ first = relation.first
143
+ first.id.should eq("5150e509b4b735f6290238d3")
144
+ first.sensor_id.should eq("1")
145
+ first.value.should eq("{\"x-axis\":0.259,\"y-axis\":-0.15,\"z-axis\":-9.807}")
146
+ first.date.to_f.should eq(1364256004.651)
147
+ first.month.should eq(3)
148
+ first.week.should eq(13)
149
+ first.year.should eq(2013)
150
+ end
151
+ end
152
+
153
+ describe "last" do
154
+ it "should return the last record" do
155
+ session = double("Session")
156
+ sensor_id = "1"
157
+ option = { page: 0, per_page: 1, sort: 'DESC', sensor_id: sensor_id}
158
+ response = {
159
+ "data" => [{
160
+ "id" => "5150e9b7b4b735d04e010ed9",
161
+ "sensor_id" => sensor_id,
162
+ "value" => "{\"x-axis\":0.191,\"y-axis\":-0.028,\"z-axis\":-9.766}",
163
+ "date" => 1364257203.315,
164
+ "month" => 3,
165
+ "week" => 13,
166
+ "year" => 2013
167
+ }]
168
+ }
169
+ session.should_receive(:get).with("/sensors/#{sensor_id}/data.json", option).and_return(response)
170
+ relation = SensorDataRelation.new(sensor_id, session)
171
+
172
+ last = relation.last
173
+ last.id.should eq("5150e9b7b4b735d04e010ed9")
174
+ last.sensor_id.should eq("1")
175
+ last.value.should eq("{\"x-axis\":0.191,\"y-axis\":-0.028,\"z-axis\":-9.766}")
176
+ last.date.to_f.should eq(1364257203.315)
177
+ last.month.should eq(3)
178
+ last.week.should eq(13)
179
+ last.year.should eq(2013)
180
+ end
181
+ end
182
+ end
183
+ end
184
+ end
@@ -0,0 +1,192 @@
1
+ require 'spec_helper'
2
+ require 'cs/end_point/sensor'
3
+
4
+ module CS
5
+ module Relation
6
+ describe SensorRelation do
7
+ describe "build" do
8
+ it "should return a sensor object" do
9
+ SensorRelation.new.build.should be_a_kind_of(EndPoint::Sensor)
10
+ end
11
+ end
12
+
13
+ let(:sensors) {
14
+ {"sensors" => [{"name" => "sensor1"}, {"name" => "sensor2"}, {"name" => "sensor3"}], "total" => 3}
15
+ }
16
+
17
+ let(:relation) {
18
+ relation = SensorRelation.new
19
+ relation.stub("check_session!").and_return(true)
20
+ relation.stub("get_data!").and_return(sensors)
21
+ relation
22
+ }
23
+
24
+ describe "get_data!" do
25
+ it "should fetch sensor data from commonSense" do
26
+ session = double('Session')
27
+ option = {page: 100, per_page: 99, shared: 1, owned:1, physical: 1, details: "full"}
28
+ session.should_receive(:get).with("/sensors.json", option)
29
+
30
+ relation = SensorRelation.new(session)
31
+ relation.get_data!(page: 100, per_page: 99, shared:true, owned:true, physical:true, details:"full")
32
+ end
33
+ end
34
+
35
+ describe "each" do
36
+ it "should get all sensor and yield each" do
37
+ session = double('Session')
38
+ relation = SensorRelation.new(session)
39
+ relation.stub("get_data!").and_return(sensors)
40
+
41
+ expect { |b| relation.each(&b) }.to yield_successive_args(EndPoint::Sensor, EndPoint::Sensor, EndPoint::Sensor)
42
+ end
43
+
44
+ context "empty result" do
45
+ it "should not yield control" do
46
+ session = double('Session')
47
+ relation = SensorRelation.new(session)
48
+ relation.stub("get_data!").and_return({"sensors" => [], "total" => 0})
49
+
50
+ expect { |b| relation.each(&b) }.not_to yield_control
51
+ end
52
+ end
53
+
54
+ context "limit specified" do
55
+ it "should yield sensor at most specified by limit" do
56
+ relation.limit(1).to_a.count.should eq(1)
57
+ end
58
+ end
59
+ end
60
+
61
+ describe "count" do
62
+ it "should return the total number of record" do
63
+ relation.count.should eq(3)
64
+ end
65
+ end
66
+
67
+ describe "first" do
68
+ it "should return the first record" do
69
+ first = relation.first
70
+ first.should be_kind_of(EndPoint::Sensor)
71
+ first.name.should eq("sensor1")
72
+ end
73
+ end
74
+
75
+ describe "last" do
76
+ it "should return the last record" do
77
+ relation = SensorRelation.new
78
+ relation.stub("count").and_return(3)
79
+ relation.should_receive("get_data").with(page:2, per_page:1).and_return({"sensors" => [{"name" => "sensor3"}], "total" => 3})
80
+
81
+ first = relation.last
82
+ first.should be_kind_of(EndPoint::Sensor)
83
+ first.name.should eq("sensor3")
84
+ end
85
+
86
+ context "with parameters given" do
87
+ it "should return the last record with options" do
88
+ relation = SensorRelation.new
89
+ relation.stub("count").and_return(3)
90
+ relation.should_receive("get_data").with(page:2, per_page:1, shared:1, owned:1, physical:1, details:'full').and_return({"sensors" => [{"name" => "sensor3"}], "total" => 3})
91
+
92
+ first = relation.where(shared: true, owned: true, physical:true, details: 'full').last
93
+ first.should be_kind_of(EndPoint::Sensor)
94
+ first.name.should eq("sensor3")
95
+ end
96
+ end
97
+ end
98
+
99
+ describe "find_by_name" do
100
+ before(:each) do
101
+ @relation = SensorRelation.new
102
+ @relation.session = double('Session')
103
+ @relation.stub("count").and_return(3)
104
+ @relation.should_receive("get_data!").with(page:0, per_page:1000).and_return({"sensors" => [{"name" => "sensor11"}, {"name" => "sensor12"}, {"name" => "sensor2"}], "total" => 3})
105
+ end
106
+
107
+ it "should return an array of matching sensor name by string" do
108
+ sensors = @relation.find_by_name("sensor11")
109
+ sensors.should be_kind_of(Array)
110
+ sensors.size.should eq(1)
111
+ sensors[0].name.should eq("sensor11")
112
+ end
113
+
114
+ it "should return an array of matching sensor by regex" do
115
+ sensors = @relation.find_by_name(/.*sor1/)
116
+ sensors.should be_kind_of(Array)
117
+ sensors.size.should eq(2)
118
+ sensors[0].name.should eq("sensor11")
119
+ sensors[1].name.should eq("sensor12")
120
+ end
121
+ end
122
+
123
+ describe "find_or_new" do
124
+ context "there is already sensor matching criteria" do
125
+ it "should return that sensor" do
126
+ relation = SensorRelation.new
127
+ relation.session = double('Session')
128
+ relation.stub("count").and_return(1)
129
+ relation.should_receive("get_data!").with(page:0, per_page:1000).and_return({"sensors" => [{
130
+ id: "143353",
131
+ name: "sensor1",
132
+ type: "1",
133
+ device_type: "Android",
134
+ pager_type: "pager1",
135
+ display_name: "Sensor1",
136
+ data_type: "json",
137
+ data_structure: "{\"foo\": \"integer\"}"
138
+ }], "total" => 1})
139
+
140
+ attributes = {name: 'sensor1', display_name: 'Sensor1',
141
+ device_type: 'Android', pager_type: 'pager1',
142
+ data_type: 'json', data_structure: {"foo" => 'integer'}}
143
+
144
+ sensor = relation.find_or_new(attributes)
145
+ sensor.should be_kind_of(EndPoint::Sensor)
146
+ sensor.id.should_not be_nil
147
+ attributes.each do |key, value|
148
+ sensor.parameter(key).should eq(value)
149
+ end
150
+ end
151
+ end
152
+
153
+ context "there is no sensor matching criteria" do
154
+ it "should return new sensor object" do
155
+ attributes = {name: 'sensor1', display_name: 'Sensor1',
156
+ device_type: 'Android', pager_type: 'pager1',
157
+ data_type: 'json', data_structure: {"foo" => 'integer'}}
158
+
159
+ relation.session = double('session')
160
+ sensor = relation.find_or_new(attributes)
161
+ sensor.should be_kind_of(EndPoint::Sensor)
162
+ sensor.session.should_not be_nil
163
+ sensor.id.should be_nil
164
+ attributes.each do |key, value|
165
+ sensor.parameter(key).should eq(value)
166
+ end
167
+ end
168
+ end
169
+ end
170
+
171
+ describe "find_or_create" do
172
+ context "there is no sensor matching criteria" do
173
+ it "should return create sensor object" do
174
+ attributes = {name: 'sensor1', display_name: 'Sensor1',
175
+ device_type: 'Android', pager_type: 'pager1',
176
+ data_type: 'json', data_structure: {"foo" => 'integer'}}
177
+
178
+ expected = EndPoint::Sensor.new(attributes)
179
+ expected.should_receive(:save!)
180
+ relation.stub('find_or_new').and_return(expected)
181
+
182
+ sensor = relation.find_or_create!(attributes)
183
+ sensor.should be_kind_of(EndPoint::Sensor)
184
+ attributes.each do |key, value|
185
+ sensor.parameter(key).should eq(value)
186
+ end
187
+ end
188
+ end
189
+ end
190
+ end
191
+ end
192
+ end