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,45 @@
1
+ # Wrapper class around time object. Wether it's Time, ActiveSupport::TimeWithZone, or TimeLord
2
+ module CS
3
+ class Time
4
+ attr_reader :time
5
+
6
+ def initialize(time=nil)
7
+ if time.nil?
8
+ @time = ::Time.new
9
+ elsif time.instance_of?(::Time)
10
+ @time = time
11
+ elsif time.kind_of?(::Numeric)
12
+ @time = ::Time.at(time)
13
+ elsif time.class.to_s == "TimeLord::Period"
14
+ @time = ::Time.at(time.beginning)
15
+ else
16
+ @time = time.to_time
17
+ end
18
+ end
19
+
20
+ def self.at(epoch)
21
+ ::Time.at(epoch)
22
+ end
23
+
24
+ def self.now
25
+ ::Time.now
26
+ end
27
+
28
+ def to_f
29
+ @time.to_f.round(3)
30
+ end
31
+
32
+ def to_s
33
+ @time.to_s
34
+ end
35
+
36
+ def inspect
37
+ @time.inspect
38
+ end
39
+
40
+ private
41
+ def method_missing(method, *args, &block)
42
+ @time.send(method, *args, &block)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module CS
2
+ VERSION = "0.1.0"
3
+ end
@@ -1,17 +1,10 @@
1
1
  require 'spec_helper'
2
2
  require 'ostruct'
3
+ require 'webmock/rspec'
3
4
 
4
5
  describe "Sensor Management" do
5
6
 
6
7
  describe "Manage Sensor" do
7
- before(:all) do
8
- @client = create_client
9
- @client.login($username, $password)
10
- end
11
-
12
- after(:all) do
13
- @client.sensors.each {|sensors| sensors.delete}
14
- end
15
8
 
16
9
  let(:sensor_info) do
17
10
  {
@@ -20,86 +13,194 @@ describe "Sensor Management" do
20
13
  device_type: "BMA123",
21
14
  pager_type: "email",
22
15
  data_type: "json",
23
- data_structure: {"x-axis" => "Float", "y-axis" => "Float", "z-axis" => "Float"}
16
+ data_structure: {"x-axis" => "Float", "y-axis" => "Float", "z-axis" => "Float"}.to_json
24
17
  }
25
18
  end
26
19
 
27
- def compare_to_sensor_info(sensor)
28
- sensor.name.should eq(sensor_info[:name])
29
- sensor.display_name.should eq(sensor_info[:display_name])
30
- sensor.display_name.should eq(sensor_info[:display_name])
31
- sensor.pager_type.should eq(sensor_info[:pager_type])
32
- sensor.data_type.should eq(sensor_info[:data_type])
33
- sensor.data_structure.should eq(sensor_info[:data_structure])
20
+ let!(:logged_in_client) do
21
+ client = CS::Client.new(base_uri: base_uri)
22
+ client.session_id = '1234'
23
+ client
24
+ end
25
+
26
+ let(:response_list_sensors) do
27
+ {
28
+ sensors: [
29
+ {
30
+ id: "143353",
31
+ name: "light",
32
+ type: "1",
33
+ device_type: "CM3602 Light sensor",
34
+ pager_type: "",
35
+ display_name: "light",
36
+ use_data_storage: true,
37
+ data_type: "json",
38
+ data_structure: "{\"lux\":\"Integer\"}",
39
+ device: {
40
+ id: "5492",
41
+ type: "HTC One V",
42
+ uuid: "351816053990044"
43
+ }
44
+ },
45
+ {
46
+ id: "143354",
47
+ name: "noise_sensor",
48
+ type: "1",
49
+ device_type: "noise_sensor",
50
+ pager_type: "",
51
+ display_name: "noise",
52
+ use_data_storage: true,
53
+ data_type: "float",
54
+ data_structure: "",
55
+ device: {
56
+ id: "5492",
57
+ type: "HTC One V",
58
+ uuid: "351816053990044"
59
+ }
60
+ },
61
+ {
62
+ id: "143355",
63
+ name: "Availability",
64
+ type: "2",
65
+ device_type: "2",
66
+ pager_type: "",
67
+ display_name: "",
68
+ use_data_storage: true,
69
+ data_type: "string",
70
+ data_structure: ""
71
+ }
72
+ ],
73
+ total: 3
74
+ }
75
+ end
76
+
77
+ def compare_to_sensor_info(a, b)
78
+ a.name.should eq(b[:name])
79
+ a.display_name.should eq(b[:display_name])
80
+ a.display_name.should eq(b[:display_name])
81
+ a.pager_type.should eq(b[:pager_type])
82
+
83
+ parsed_structure = b[:data_structure]
84
+ if !b[:data_structure].empty?
85
+ parsed_structure = JSON.parse(parsed_structure)
86
+ end
87
+
88
+ a.data_structure.should eq(parsed_structure)
89
+ a.data_type.should eq(b[:data_type])
90
+ end
91
+
92
+ def stub_response_sensor_list
93
+ stub_request(:get, "http://api.dev.sense-os.local/sensors.json?page=0&per_page=1000").
94
+ with(:headers => {'Content-Type'=>'application/json', 'X-Session-Id'=>'1234'}).
95
+ to_return(:status => 200, :body => response_list_sensors.to_json, :headers => {
96
+ 'Content-Type'=>'application/json'
97
+ })
34
98
  end
35
99
 
36
100
  it "create a new sensor" do
37
- sensor = @client.sensors.build
101
+ body = {sensor: sensor_info}
102
+
103
+ stub_request(:post, base_uri + "/sensors.json").
104
+ with(:body => body,
105
+ :headers => {'Content-Type'=>'application/json', 'X-Session-Id'=>'1234'}).
106
+ to_return(:status => 201, :body => "", :headers => {
107
+ location: base_uri + '/sensors/1'
108
+ })
109
+
110
+ sensor = logged_in_client.sensors.build
38
111
  sensor.name = sensor_info[:name]
39
112
  sensor.display_name = sensor_info[:display_name]
40
- sensor.device_type = sensor_info[:display_name]
113
+ sensor.device_type = sensor_info[:device_type]
41
114
  sensor.pager_type = sensor_info[:pager_type]
42
115
  sensor.data_type = sensor_info[:data_type]
43
116
  sensor.data_structure = sensor_info[:data_structure]
44
- sensor.save!
117
+ sensor.save!.should be_true
118
+
119
+ sensor.id.should == "1"
45
120
  end
46
121
 
47
122
  it "get list of sensor from commonSense" do
48
- sensor = @client.sensors.build(sensor_info)
49
- sensor.save!
50
123
 
51
- @client.sensors.all.should_not be_empty
124
+ stub_response_sensor_list
125
+ logged_in_client.sensors.all.should_not be_empty
52
126
 
53
- # there could be another Virtual sensor that is automatically created
54
- @client.sensors.count.should be > 1
127
+ logged_in_client.sensors.count.should == 3
55
128
 
56
- @client.sensors.each do |sensor|
57
- compare_to_sensor_info(sensor)
58
- break
129
+ i = 0
130
+ logged_in_client.sensors.each do |sensor|
131
+ compare_to_sensor_info(sensor, response_list_sensors[:sensors][i])
132
+ i += 1;
59
133
  end
60
134
  end
61
135
 
62
136
  it "get first sensor data" do
63
- sensor = @client.sensors.build(sensor_info)
64
- sensor.save!
65
- sensor = @client.sensors.first
137
+ stub_response_sensor_list
138
+
139
+ sensor = logged_in_client.sensors.first
66
140
 
67
141
  sensor.should_not be nil
68
- compare_to_sensor_info(sensor)
142
+ compare_to_sensor_info(sensor, response_list_sensors[:sensors][0])
69
143
  end
70
144
 
71
145
  it "get sensor data by id" do
72
- sensor = @client.sensors.build(sensor_info)
73
- sensor.save!
146
+ first = response_list_sensors[:sensors][0]
147
+
148
+ body = { sensor: response_list_sensors[:sensors][0] }
74
149
 
75
- first = @client.sensors.first
150
+ stub_request(:get, base_uri + "/sensors/143353.json?").
151
+ with(:headers => {'Content-Type'=>'application/json', 'X-Session-Id'=>'1234'}).
152
+ to_return(:status => 200, :body => body.to_json, :headers => {'Content-Type'=>'application/json'})
76
153
 
77
- sensor = @client.sensors.find(first.id)
154
+ sensor = logged_in_client.sensors.find!(first[:id])
78
155
  sensor.should_not be_nil
156
+ compare_to_sensor_info(sensor, response_list_sensors[:sensors][0])
79
157
  end
80
158
 
81
159
 
82
160
  it "filter sensor data" do
83
- sensor = @client.sensors.build(sensor_info)
84
- sensor.save!
85
- sensors = @client.sensors.where(page: 0, per_page: 1, owned: true , details: "full")
86
- sensors.to_a.should_not be_empty
161
+ stub_request(:get, base_uri + "/sensors.json?details=full&owned=1&page=0&per_page=1").
162
+ with(:headers => {'Content-Type'=>'application/json', 'X-Session-Id'=>'1234'}).
163
+ to_return(:status => 200, :body => response_list_sensors.to_json, :headers => {'Content-Type'=>'application/json'})
164
+
165
+ sensors = logged_in_client.sensors.where(page: 0, per_page: 1, owned: true , details: "full")
166
+ result = sensors.to_a
167
+ result.should_not be_empty
168
+
169
+ i = 0
170
+ result.each do |sensor|
171
+ compare_to_sensor_info(sensor, response_list_sensors[:sensors][i])
172
+ i += 1;
173
+ end
87
174
  end
88
175
 
89
176
  it "should handle pagination" do
90
- 2.times do
91
- sensor = @client.sensors.build(sensor_info)
92
- sensor.save!
177
+ client = logged_in_client
178
+
179
+ # stub page 0-2
180
+ for i in 0..2
181
+ response = {
182
+ sensors: [ response_list_sensors[:sensors][i] ],
183
+ total: 3
184
+ }
185
+ stub_request(:get, base_uri + "/sensors.json?page=#{i}&per_page=1").
186
+ with(:headers => {'Content-Type'=>'application/json', 'X-Session-Id'=>'1234'}).
187
+ to_return(:status => 200, :body => response.to_json, :headers => {'Content-Type'=>'application/json'})
93
188
  end
94
189
 
95
- count = @client.sensors.count
190
+ # last page, empty response
191
+ response = { sensors: [], total: 3 }
192
+ stub_request(:get, base_uri + "/sensors.json?page=3&per_page=1").
193
+ with(:headers => {'Content-Type'=>'application/json', 'X-Session-Id'=>'1234'}).
194
+ to_return(:status => 200, :body => response.to_json, :headers => {'Content-Type'=>'application/json'})
195
+
96
196
 
97
197
  i = 0;
98
- @client.sensors.where(per_page: 1).each do
198
+ client.sensors.where(per_page: 1).each do |sensor|
199
+ compare_to_sensor_info(sensor, response_list_sensors[:sensors][i])
99
200
  i += 1
100
201
  end
101
202
 
102
- i.should eq(count)
203
+ i.should == 3
103
204
  end
104
205
  end
105
206
  end
@@ -1,25 +1,98 @@
1
1
  require 'spec_helper'
2
+ require 'webmock/rspec'
2
3
 
3
4
  describe "User management" do
4
5
 
5
6
  describe "Manage user" do
6
7
 
7
8
  let!(:user) do
8
- $user
9
+ username = "user1@tester.com"
10
+
11
+ client = CS::Client.new(base_uri: base_uri)
12
+ user = client.new_user
13
+ user.username = username
14
+ user.email = user.username
15
+ user.password = 'password'
16
+ user.name = 'Jan'
17
+ user.surname = 'jagger'
18
+ user.address = 'Lloydstraat 5'
19
+ user.zipcode = '3024ea'
20
+ user.country = 'NETHERLANDS'
21
+ user.mobile = '123456789'
22
+ user
23
+ end
24
+
25
+ let!(:logged_in_client) do
26
+ client = CS::Client.new(base_uri: base_uri)
27
+ client.session_id = '123488'
28
+ client
29
+ end
30
+
31
+ def get_attribute(user)
32
+ {
33
+ email: user.email,
34
+ username: user.username,
35
+ name: user.name,
36
+ surname: user.surname,
37
+ address: user.address,
38
+ zipcode: user.zipcode,
39
+ country: user.country,
40
+ mobile: user.mobile,
41
+ password: user.password
42
+ }
9
43
  end
10
44
 
11
45
  it "create new user" do
12
46
  current_user = user
47
+ body = {user: get_attribute(current_user)}
48
+ stub_request(:post, base_uri + '/users.json').
49
+ with(:body => body).
50
+ to_return(:status => 201, :body => "", :headers => {
51
+ location: base_uri + '/users/1'
52
+ })
53
+
54
+ user.save!.should be_true
55
+ user.id.should == "1"
13
56
  current_user.id.should_not be_nil
14
57
  end
15
58
 
59
+ it "should login the user" do
60
+ client = create_client
61
+ current_user = user
62
+ stub_request(:post, base_uri + "/login.json").
63
+ with(:body => {username: current_user.username, password: current_user.password},
64
+ :headers => {'Content-Type'=>'application/json'}).
65
+ to_return(:status => 200, :body => {session_id: "1"}.to_json, :headers => {'Content-Type' => 'application/json'})
66
+
67
+ session_id = client.login!(user.username, 'password')
68
+ session_id.should == "1"
69
+ end
70
+
16
71
  it "get user data from commonSense" do
17
- attributes = user.to_h
72
+ current_user = user
73
+ attributes = get_attribute(current_user)
74
+
75
+ body = {
76
+ user: {
77
+ id: "3357",
78
+ email: current_user.email,
79
+ username: current_user.username,
80
+ name: current_user.name,
81
+ surname: current_user.surname,
82
+ address: current_user.address,
83
+ zipcode: current_user.zipcode,
84
+ country: current_user.country,
85
+ mobile: current_user.mobile,
86
+ UUID: "203a8-aa54-11e1-85e6-da0007d04f45",
87
+ openid: nil
88
+ }
89
+ }
18
90
 
19
- client = create_client
20
- session_id = client.login(user.username, 'password')
21
- session_id.should_not be_nil
91
+ stub_request(:get, "http://api.dev.sense-os.local/users/current.json?").
92
+ with(:headers => {'Content-Type'=>'application/json', 'X-Session-Id'=>'123488'}).
93
+ to_return(:status => 200, :body => body.to_json, :headers => {'Content-Type'=>'application/json'})
22
94
 
95
+ client = logged_in_client
23
96
  current_user = client.current_user
24
97
 
25
98
  attributes.each do |key, value|
@@ -29,15 +102,9 @@ describe "User management" do
29
102
  end
30
103
 
31
104
  it "update user" do
32
- current_time = Time.now.to_f
33
- client = create_client
34
- username = "user-#{current_time}@tester.com"
35
- current_user = client.new_user(username: username, email: username, password: 'password')
36
- current_user.save!
37
- current_user.id.should_not be_nil
38
-
39
- expected = { username: "user-edit-#{current_time}@tester.com",
40
- email: "user-edit-#{current_time}@tester.com",
105
+ expected = {
106
+ email: "user-edit-1@tester.com",
107
+ username: "user-edit-1@tester.com",
41
108
  name: "Jan Edit",
42
109
  surname: "jagger edit",
43
110
  address: 'Looydstraat 5 edit',
@@ -46,20 +113,25 @@ describe "User management" do
46
113
  mobile: '987654321'
47
114
  }
48
115
 
49
- client = create_client
50
- client.session.should be_nil
51
- session_id = client.login(current_user.username, 'password')
52
- session_id.should_not be_nil
53
- client.session.should_not be_nil
116
+ client = logged_in_client
117
+ current_user = user
118
+ current_user.id = "1"
119
+
120
+ body = {user: expected}
121
+ body[:user][:password] = current_user.password
122
+ body[:user][:id] = current_user.id
123
+
124
+ stub_request(:put, "http://api.dev.sense-os.local/users/1.json").
125
+ with(:body => body,
126
+ :headers => {'Content-Type'=>'application/json'}).
127
+ to_return(:status => 200, :body => "", :headers => {})
54
128
 
55
- current_user = client.current_user
56
129
 
57
130
  expected.each do |key, value|
58
- current_user.send("#{key}=".to_sym, value)
131
+ current_user.send("#{key}=".to_sym, value) unless key == :password
59
132
  end
60
133
 
61
134
  current_user.save!
62
- current_user.reload
63
135
 
64
136
  expected.each do |key,value|
65
137
  current_user.send(key).should eq(value)