cs 0.1.0beta3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/.gitignore +21 -0
  2. data/Gemfile +10 -0
  3. data/LICENSE +22 -0
  4. data/README.md +132 -0
  5. data/Rakefile +7 -0
  6. data/cs.gemspec +22 -0
  7. data/lib/commonsense-ruby-lib.rb +127 -0
  8. data/lib/commonsense-ruby-lib/auth/http.rb +117 -0
  9. data/lib/commonsense-ruby-lib/auth/oauth.rb +101 -0
  10. data/lib/commonsense-ruby-lib/end_point.rb +276 -0
  11. data/lib/commonsense-ruby-lib/end_point/group.rb +28 -0
  12. data/lib/commonsense-ruby-lib/end_point/sensor.rb +36 -0
  13. data/lib/commonsense-ruby-lib/end_point/sensor_data.rb +70 -0
  14. data/lib/commonsense-ruby-lib/end_point/user.rb +50 -0
  15. data/lib/commonsense-ruby-lib/error.rb +51 -0
  16. data/lib/commonsense-ruby-lib/relation.rb +233 -0
  17. data/lib/commonsense-ruby-lib/relation/sensor_data_relation.rb +116 -0
  18. data/lib/commonsense-ruby-lib/relation/sensor_relation.rb +162 -0
  19. data/lib/commonsense-ruby-lib/serializer.rb +20 -0
  20. data/lib/commonsense-ruby-lib/session.rb +105 -0
  21. data/lib/commonsense-ruby-lib/version.rb +3 -0
  22. data/spec/features/sensor_data_management_spec.rb +4 -0
  23. data/spec/features/sensor_management_spec.rb +105 -0
  24. data/spec/features/user_management_spec.rb +70 -0
  25. data/spec/lib/commonsense-ruby-lib/end_point/sensor_data_spec.rb +68 -0
  26. data/spec/lib/commonsense-ruby-lib/end_point/sensor_spec.rb +98 -0
  27. data/spec/lib/commonsense-ruby-lib/end_point/user_spec.rb +36 -0
  28. data/spec/lib/commonsense-ruby-lib/end_point_spec.rb +190 -0
  29. data/spec/lib/commonsense-ruby-lib/relation/sensor_data_relation_spec.rb +444 -0
  30. data/spec/lib/commonsense-ruby-lib/relation/sensor_relation_spec.rb +165 -0
  31. data/spec/lib/commonsense-ruby-lib/session_spec.rb +43 -0
  32. data/spec/lib/commonsense-ruby-lib_spec.rb +51 -0
  33. data/spec/spec_helper.rb +40 -0
  34. data/spec/support/spec_config.yml.sample +6 -0
  35. data/spec/support/vcr.rb +5 -0
  36. metadata +175 -0
@@ -0,0 +1,162 @@
1
+ module CommonSense
2
+ module Relation
3
+
4
+ # Class that used to query List of sensor data from CS.
5
+ #
6
+ # == parameters
7
+ # The relation object has serveral parameter
8
+ #
9
+ # * page : Integer, number of page in pagination. starts from 0
10
+ # * per_page : Integer, number of sensor per page, default 1000, max: 1000
11
+ # * shared : Boolean, filter only sensor that is shared.
12
+ # * onwed : Boolean, filter only sensor that user own.
13
+ # * physical : Boolean, filter only physical sensor (sensor that connected to device).
14
+ # * details : String "no" or "full", gives full description of sensor
15
+ #
16
+ # == Examples
17
+ # This is an example how would you use the sensors relation object
18
+ #
19
+ # === Create Sensors Relation
20
+ #
21
+ # client = CommonSense::Client.new
22
+ # client.login('user', 'password')
23
+ # session = client.session
24
+ #
25
+ # # create sensors relation
26
+ # sensors = client.sensors
27
+ #
28
+ # # is the same as
29
+ # sensors = CommonSense::Relation::Sensors.new
30
+ # sensors.session = session
31
+ #
32
+ # === Get all sensor
33
+ #
34
+ # sensors = client.sensors
35
+ # sensors.to_a
36
+ #
37
+ # === Get sensor by specifying parameters
38
+ #
39
+ # client.sensors.where(page: 0, per_page: 1000)
40
+ # client.sensors.where(owned: true)
41
+ # client.sensors.where(physical: true)
42
+ # client.sensors.where(page: 0, per_page: 1000, physical: true, owned: true, details: "full")
43
+ #
44
+ # === Chain parameters
45
+ #
46
+ # client.sensors.where(page:0, per_page: 10).where(physical: true)
47
+ #
48
+ # === Find sensor by name
49
+ #
50
+ # client.sensors.find_by_name(/position/)
51
+ # client.sensors.find_by_name(/position/, owned: true) # or
52
+ # client.sensors.where(owned: true).find_by_name(/position/)
53
+ #
54
+ # === Get first sensor or last sensor
55
+ #
56
+ # sensor = client.sensors.first
57
+ # sensor = client.sensors.last
58
+ #
59
+ # === Get number of sensors
60
+ #
61
+ # client.sensors.count
62
+ # client.sensors.where(owned: true).count
63
+ class SensorRelation
64
+ include Relation
65
+
66
+ parameter :page, Integer, default: 0, required: true
67
+ parameter :per_page, Integer, default: 1000, required: true, maximum: 1000
68
+ parameter :shared, Boolean
69
+ parameter :owned, Boolean
70
+ parameter :physical, Boolean
71
+ parameter :details, String, valid_values: ["no", "full"]
72
+ parameter :group_id, String
73
+
74
+ def initialize(session=nil)
75
+ @session = session
76
+ page = 0
77
+ per_page = 1000
78
+ end
79
+
80
+ # Create a new {EndPoint::Sensor Sensor} object.
81
+ #
82
+ # example:
83
+ #
84
+ # sensor = client.sensors.build
85
+ def build(attribtues={})
86
+ sensor = EndPoint::Sensor.new(attribtues)
87
+ sensor.session = self.session
88
+ sensor
89
+ end
90
+
91
+ # Find {EndPoint::Sensor Sensor} by id
92
+ #
93
+ # example:
94
+ #
95
+ # sensor = client.sensors.find("1")
96
+ def find(id)
97
+ check_session!
98
+ sensor = EndPoint::Sensor.new(id: id)
99
+ sensor.session = self.session
100
+ sensor.retrieve ? sensor : nil
101
+ end
102
+
103
+ # Find sensor by name in regular expression.
104
+ # The second argument is parameter that is usualy us in {#where where}
105
+ #
106
+ # example:
107
+ #
108
+ # client.sensors.find_by_name(/position/, owned: true)
109
+ def find_by_name(regex, parameters={})
110
+ check_session!
111
+ self.where(parameters)
112
+ self.select { |sensor| sensor.name =~ regex }
113
+ end
114
+
115
+ def each(&block)
116
+ page = self.page || 0;
117
+ begin
118
+ sensors = get_data!({
119
+ page: page, per_page: self.per_page, shared: self.shared,
120
+ owned: self.owned, physical: self.physical, details: self.details, group_id: self.group_id
121
+ })
122
+
123
+ sensors = sensors["sensors"]
124
+ if !sensors.empty?
125
+ sensors.each do |sensor|
126
+ sensor = EndPoint::Sensor.new(sensor)
127
+ sensor.session = session
128
+ yield sensor
129
+ end
130
+
131
+ page += 1
132
+ end
133
+
134
+ end while sensors.size == self.per_page
135
+ end
136
+
137
+ private
138
+ def get_url
139
+ "/sensors.json"
140
+ end
141
+
142
+ def parse_single_resource(sensors)
143
+ sensors = sensors["sensors"]
144
+ if !sensors.empty?
145
+ sensor = EndPoint::Sensor.new(sensors[0])
146
+ sensor.session = self.session
147
+
148
+ return sensor
149
+ end
150
+ end
151
+
152
+ def get_single_resource(params={})
153
+ options = {
154
+ page: 0, per_page: 1, shared: self.shared,
155
+ owned: self.owned, physical: self.physical, details: self.details
156
+ }
157
+ options.merge!(params)
158
+ get_data(options)
159
+ end
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,20 @@
1
+ module CommonSense
2
+ module Serializer
3
+ def from_hash(hash)
4
+ if hash
5
+ hash.each do |k,v|
6
+ self.instance_variable_set("@#{k}", v) if self.respond_to?(k)
7
+ end
8
+ end
9
+ end
10
+
11
+ def to_h(include_nil = true)
12
+ symbol = self.class.attribute_set
13
+ hash = {}
14
+ symbol.each {|var| hash[var] = instance_variable_get("@#{var}")}
15
+
16
+ return include_nil ? hash : hash.reject { |k,v| v.nil? }
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,105 @@
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
@@ -0,0 +1,3 @@
1
+ module CommonSense
2
+ VERSION = "0.1.0beta3"
3
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Sensor Management" do
4
+ end
@@ -0,0 +1,105 @@
1
+ require 'spec_helper'
2
+ require 'ostruct'
3
+
4
+ describe "Sensor Management" do
5
+
6
+ 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
+
16
+ let(:sensor_info) do
17
+ {
18
+ name: "accelerometer",
19
+ display_name: "Accelerometer",
20
+ device_type: "BMA123",
21
+ pager_type: "email",
22
+ data_type: "json",
23
+ data_structure: {"x-axis" => "Float", "y-axis" => "Float", "z-axis" => "Float"}
24
+ }
25
+ end
26
+
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])
34
+ end
35
+
36
+ it "create a new sensor" do
37
+ sensor = @client.sensors.build
38
+ sensor.name = sensor_info[:name]
39
+ sensor.display_name = sensor_info[:display_name]
40
+ sensor.device_type = sensor_info[:display_name]
41
+ sensor.pager_type = sensor_info[:pager_type]
42
+ sensor.data_type = sensor_info[:data_type]
43
+ sensor.data_structure = sensor_info[:data_structure]
44
+ sensor.save!
45
+ end
46
+
47
+ it "get list of sensor from commonSense" do
48
+ sensor = @client.sensors.build(sensor_info)
49
+ sensor.save!
50
+
51
+ @client.sensors.all.should_not be_empty
52
+
53
+ # there could be another Virtual sensor that is automatically created
54
+ @client.sensors.count.should be > 1
55
+
56
+ @client.sensors.each do |sensor|
57
+ compare_to_sensor_info(sensor)
58
+ break
59
+ end
60
+ end
61
+
62
+ it "get first sensor data" do
63
+ sensor = @client.sensors.build(sensor_info)
64
+ sensor.save!
65
+ sensor = @client.sensors.first
66
+
67
+ sensor.should_not be nil
68
+ compare_to_sensor_info(sensor)
69
+ end
70
+
71
+ it "get sensor data by id" do
72
+ sensor = @client.sensors.build(sensor_info)
73
+ sensor.save!
74
+
75
+ first = @client.sensors.first
76
+
77
+ sensor = @client.sensors.find(first.id)
78
+ sensor.should_not be_nil
79
+ end
80
+
81
+
82
+ 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
87
+ end
88
+
89
+ it "should handle pagination" do
90
+ 2.times do
91
+ sensor = @client.sensors.build(sensor_info)
92
+ sensor.save!
93
+ end
94
+
95
+ count = @client.sensors.count
96
+
97
+ i = 0;
98
+ @client.sensors.where(per_page: 1).each do
99
+ i += 1
100
+ end
101
+
102
+ i.should eq(count)
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe "User management" do
4
+
5
+ describe "Manage user" do
6
+
7
+ let!(:user) do
8
+ $user
9
+ end
10
+
11
+ it "create new user" do
12
+ current_user = user
13
+ current_user.id.should_not be_nil
14
+ end
15
+
16
+ it "get user data from commonSense" do
17
+ attributes = user.to_h
18
+
19
+ client = create_client
20
+ session_id = client.login(user.username, 'password')
21
+ session_id.should_not be_nil
22
+
23
+ current_user = client.current_user
24
+
25
+ attributes.each do |key, value|
26
+ next if key == :password
27
+ current_user.send(key).should eq(value)
28
+ end
29
+ end
30
+
31
+ 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",
41
+ name: "Jan Edit",
42
+ surname: "jagger edit",
43
+ address: 'Looydstraat 5 edit',
44
+ zipcode: '12345',
45
+ country: 'GERMANY',
46
+ mobile: '987654321'
47
+ }
48
+
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
54
+
55
+ current_user = client.current_user
56
+
57
+ expected.each do |key, value|
58
+ current_user.send("#{key}=".to_sym, value)
59
+ end
60
+
61
+ current_user.save!
62
+ current_user.reload
63
+
64
+ expected.each do |key,value|
65
+ current_user.send(key).should eq(value)
66
+ end
67
+
68
+ end
69
+ end
70
+ end