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.
- checksums.yaml +7 -0
- data/.travis.yml +8 -0
- data/Gemfile +22 -6
- data/README.md +15 -4
- data/cs.gemspec +7 -7
- data/lib/{commonsense-ruby-lib.rb → cs.rb} +80 -31
- data/lib/{commonsense-ruby-lib → cs}/auth/http.rb +52 -33
- data/lib/{commonsense-ruby-lib → cs}/auth/oauth.rb +28 -28
- data/lib/cs/collection.rb +7 -0
- data/lib/cs/collection/sensor_data_collection.rb +61 -0
- data/lib/{commonsense-ruby-lib → cs}/end_point.rb +36 -24
- data/lib/cs/end_point/group.rb +26 -0
- data/lib/cs/end_point/notification.rb +16 -0
- data/lib/{commonsense-ruby-lib → cs}/end_point/sensor.rb +22 -6
- data/lib/{commonsense-ruby-lib → cs}/end_point/sensor_data.rb +17 -6
- data/lib/cs/end_point/trigger.rb +16 -0
- data/lib/{commonsense-ruby-lib → cs}/end_point/user.rb +8 -4
- data/lib/{commonsense-ruby-lib → cs}/error.rb +7 -1
- data/lib/cs/parameter_processor.rb +99 -0
- data/lib/cs/relation.rb +244 -0
- data/lib/cs/relation/group_relation.rb +24 -0
- data/lib/cs/relation/notification_relation.rb +20 -0
- data/lib/{commonsense-ruby-lib → cs}/relation/sensor_data_relation.rb +7 -52
- data/lib/{commonsense-ruby-lib → cs}/relation/sensor_relation.rb +28 -55
- data/lib/cs/relation/trigger_relation.rb +21 -0
- data/lib/cs/relation/user_relation.rb +20 -0
- data/lib/{commonsense-ruby-lib → cs}/serializer.rb +1 -1
- data/lib/cs/session.rb +170 -0
- data/lib/cs/time.rb +45 -0
- data/lib/cs/version.rb +3 -0
- data/spec/features/sensor_management_spec.rb +146 -45
- data/spec/features/user_management_spec.rb +94 -22
- data/spec/lib/cs/collection/sensor_data_collection_spec.rb +27 -0
- data/spec/lib/cs/end_point/group_spec.rb +120 -0
- data/spec/lib/cs/end_point/sensor_data_spec.rb +110 -0
- data/spec/lib/{commonsense-ruby-lib → cs}/end_point/sensor_spec.rb +6 -6
- data/spec/lib/{commonsense-ruby-lib → cs}/end_point/user_spec.rb +14 -7
- data/spec/lib/{commonsense-ruby-lib → cs}/end_point_spec.rb +25 -12
- data/spec/lib/cs/relation/group_relation_spec.rb +103 -0
- data/spec/lib/cs/relation/sensor_data_relation_spec.rb +184 -0
- data/spec/lib/cs/relation/sensor_relation_spec.rb +192 -0
- data/spec/lib/cs/relation/user_relation_spec.rb +81 -0
- data/spec/lib/cs/relation_spec.rb +151 -0
- data/spec/lib/cs/session_spec.rb +91 -0
- data/spec/lib/cs/time_spec.rb +71 -0
- data/spec/lib/cs_spec.rb +85 -0
- data/spec/spec_helper.rb +6 -26
- metadata +69 -86
- data/lib/commonsense-ruby-lib/end_point/group.rb +0 -28
- data/lib/commonsense-ruby-lib/relation.rb +0 -233
- data/lib/commonsense-ruby-lib/session.rb +0 -105
- data/lib/commonsense-ruby-lib/version.rb +0 -3
- data/spec/lib/commonsense-ruby-lib/end_point/sensor_data_spec.rb +0 -68
- data/spec/lib/commonsense-ruby-lib/relation/sensor_data_relation_spec.rb +0 -444
- data/spec/lib/commonsense-ruby-lib/relation/sensor_relation_spec.rb +0 -165
- data/spec/lib/commonsense-ruby-lib/session_spec.rb +0 -43
- data/spec/lib/commonsense-ruby-lib_spec.rb +0 -51
@@ -1,7 +1,8 @@
|
|
1
|
-
module
|
1
|
+
module CS
|
2
2
|
module Relation
|
3
3
|
class SensorDataRelation
|
4
4
|
include Relation
|
5
|
+
include Enumerable
|
5
6
|
|
6
7
|
parameter :page, Integer, default: 0
|
7
8
|
parameter :per_page, Integer, default: 1000, maximum: 1000
|
@@ -15,13 +16,9 @@ module CommonSense
|
|
15
16
|
parameter_alias :from, :start_date
|
16
17
|
parameter_alias :to, :end_date
|
17
18
|
|
18
|
-
include Enumerable
|
19
|
-
|
20
19
|
def initialize(sensor_id, session=nil)
|
21
20
|
self.sensor_id = sensor_id
|
22
21
|
self.session = session
|
23
|
-
page = 0
|
24
|
-
per_page = 1000
|
25
22
|
end
|
26
23
|
|
27
24
|
def get_url
|
@@ -29,40 +26,9 @@ module CommonSense
|
|
29
26
|
end
|
30
27
|
|
31
28
|
def build(attributes={})
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
data
|
36
|
-
end
|
37
|
-
|
38
|
-
def each_batch(params={}, &block)
|
39
|
-
check_session!
|
40
|
-
options = get_options(params)
|
41
|
-
|
42
|
-
page = self.page || 0;
|
43
|
-
begin
|
44
|
-
options[:page] = page
|
45
|
-
data = get_data(options)
|
46
|
-
|
47
|
-
data = data["data"]
|
48
|
-
if !data.empty?
|
49
|
-
yield data
|
50
|
-
|
51
|
-
page += 1
|
52
|
-
end
|
53
|
-
|
54
|
-
end while data.size == self.per_page
|
55
|
-
end
|
56
|
-
|
57
|
-
def each(&block)
|
58
|
-
self.each_batch do |data|
|
59
|
-
data.each do |data_point|
|
60
|
-
sensor_data = EndPoint::SensorData.new(data_point)
|
61
|
-
sensor_data.sensor_id = self.sensor_id
|
62
|
-
sensor_data.session = session
|
63
|
-
yield sensor_data
|
64
|
-
end
|
65
|
-
end
|
29
|
+
sensor_data = super(attributes)
|
30
|
+
sensor_data.sensor_id = self.sensor_id
|
31
|
+
sensor_data
|
66
32
|
end
|
67
33
|
|
68
34
|
def count
|
@@ -74,7 +40,6 @@ module CommonSense
|
|
74
40
|
retval
|
75
41
|
end
|
76
42
|
|
77
|
-
|
78
43
|
def first
|
79
44
|
data = get_data!(page:0, per_page: 1, sort: "ASC")
|
80
45
|
parse_single_resource(data)
|
@@ -98,18 +63,8 @@ module CommonSense
|
|
98
63
|
end
|
99
64
|
|
100
65
|
private
|
101
|
-
def
|
102
|
-
|
103
|
-
data_point = data[0]
|
104
|
-
|
105
|
-
sensor_data = nil
|
106
|
-
if !data.empty?
|
107
|
-
sensor_data = EndPoint::SensorData.new(data_point)
|
108
|
-
sensor_data.sensor_id = self.sensor_id
|
109
|
-
sensor_data.session = session
|
110
|
-
end
|
111
|
-
|
112
|
-
sensor_data
|
66
|
+
def resource_class
|
67
|
+
EndPoint::SensorData
|
113
68
|
end
|
114
69
|
end
|
115
70
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module CS
|
2
2
|
module Relation
|
3
3
|
|
4
4
|
# Class that used to query List of sensor data from CS.
|
@@ -18,7 +18,7 @@ module CommonSense
|
|
18
18
|
#
|
19
19
|
# === Create Sensors Relation
|
20
20
|
#
|
21
|
-
# client =
|
21
|
+
# client = CS::Client.new
|
22
22
|
# client.login('user', 'password')
|
23
23
|
# session = client.session
|
24
24
|
#
|
@@ -26,7 +26,7 @@ module CommonSense
|
|
26
26
|
# sensors = client.sensors
|
27
27
|
#
|
28
28
|
# # is the same as
|
29
|
-
# sensors =
|
29
|
+
# sensors = CS::Relation::Sensors.new
|
30
30
|
# sensors.session = session
|
31
31
|
#
|
32
32
|
# === Get all sensor
|
@@ -71,33 +71,20 @@ module CommonSense
|
|
71
71
|
parameter :details, String, valid_values: ["no", "full"]
|
72
72
|
parameter :group_id, String
|
73
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
74
|
# Find {EndPoint::Sensor Sensor} by id
|
92
75
|
#
|
93
76
|
# example:
|
94
77
|
#
|
95
78
|
# sensor = client.sensors.find("1")
|
96
|
-
def find(id)
|
79
|
+
def find!(id)
|
97
80
|
check_session!
|
98
81
|
sensor = EndPoint::Sensor.new(id: id)
|
99
82
|
sensor.session = self.session
|
100
|
-
sensor.retrieve ? sensor :
|
83
|
+
sensor.retrieve! ? sensor : false
|
84
|
+
end
|
85
|
+
|
86
|
+
def find(id)
|
87
|
+
find!(id) rescue false
|
101
88
|
end
|
102
89
|
|
103
90
|
# Find sensor by name in regular expression.
|
@@ -109,53 +96,39 @@ module CommonSense
|
|
109
96
|
def find_by_name(regex, parameters={})
|
110
97
|
check_session!
|
111
98
|
self.where(parameters)
|
99
|
+
regex = /\A#{regex}\z/ if regex.kind_of?(String)
|
112
100
|
self.select { |sensor| sensor.name =~ regex }
|
113
101
|
end
|
114
102
|
|
115
|
-
def
|
116
|
-
|
117
|
-
|
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
|
103
|
+
def triggers()
|
104
|
+
Relation::SensorTriggersRelation.new(@session)
|
105
|
+
end
|
130
106
|
|
131
|
-
|
132
|
-
|
107
|
+
def clone_from(other_sensor)
|
108
|
+
sensor = Sensor.new(other_sensor.to_parameters[:sensor])
|
109
|
+
sensor.id = nil
|
110
|
+
sensor.session = self.session
|
133
111
|
|
134
|
-
|
112
|
+
sensor
|
135
113
|
end
|
136
114
|
|
137
115
|
private
|
116
|
+
def resource_class
|
117
|
+
EndPoint::Sensor
|
118
|
+
end
|
119
|
+
|
138
120
|
def get_url
|
139
121
|
"/sensors.json"
|
140
122
|
end
|
123
|
+
end
|
141
124
|
|
142
|
-
|
143
|
-
|
144
|
-
if !sensors.empty?
|
145
|
-
sensor = EndPoint::Sensor.new(sensors[0])
|
146
|
-
sensor.session = self.session
|
125
|
+
class SensorTriggersRelation
|
126
|
+
include Relation
|
147
127
|
|
148
|
-
|
149
|
-
end
|
150
|
-
end
|
128
|
+
parameter :page, Integer, default: 0, required: true
|
151
129
|
|
152
|
-
def
|
153
|
-
|
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)
|
130
|
+
def get_url
|
131
|
+
"/sensors/triggers.json"
|
159
132
|
end
|
160
133
|
end
|
161
134
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module CS
|
2
|
+
module Relation
|
3
|
+
class TriggerRelation
|
4
|
+
include Relation
|
5
|
+
|
6
|
+
parameter :page, Integer, default: 0, required: true
|
7
|
+
parameter :per_page, Integer, default: 1000, required: true, maximum: 1000
|
8
|
+
parameter :total, Boolean
|
9
|
+
|
10
|
+
|
11
|
+
private
|
12
|
+
def resource_class
|
13
|
+
EndPoint::Trigger
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_url
|
17
|
+
"/triggers.json"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module CS
|
2
|
+
module Relation
|
3
|
+
class UserRelation
|
4
|
+
include Relation
|
5
|
+
|
6
|
+
parameter :page, Integer, default: 0, required: true
|
7
|
+
parameter :per_page, Integer, default: 1000, required: true, maximum: 1000
|
8
|
+
parameter :email, Boolean
|
9
|
+
|
10
|
+
private
|
11
|
+
def resource_class
|
12
|
+
EndPoint::User
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_url
|
16
|
+
"/users.json"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/cs/session.rb
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
module CS
|
2
|
+
class Session
|
3
|
+
attr_accessor :logger, :base_uri
|
4
|
+
|
5
|
+
def initialize(opts={})
|
6
|
+
options = {
|
7
|
+
base_uri: 'https://api.sense-os.nl',
|
8
|
+
authentication: true
|
9
|
+
}.merge(opts)
|
10
|
+
@base_uri = options[:base_uri]
|
11
|
+
@auth_proxy = options[:authentication] ? nil : CS::Auth::HTTP.new(@base_uri)
|
12
|
+
end
|
13
|
+
|
14
|
+
# login to commonsense
|
15
|
+
# @return [String] session_id
|
16
|
+
def login(username, password, digest=true)
|
17
|
+
@auth_proxy = CS::Auth::HTTP.new(@base_uri)
|
18
|
+
@auth_proxy.login(username, password, digest)
|
19
|
+
end
|
20
|
+
|
21
|
+
def oauth(consumer_key, consumer_secret, access_token, access_token_secret)
|
22
|
+
@auth_proxy = CS::Auth::OAuth.new(consumer_key, consumer_secret,
|
23
|
+
access_token, access_token_secret,
|
24
|
+
@base_uri)
|
25
|
+
end
|
26
|
+
|
27
|
+
def session_id
|
28
|
+
auth_proxy.session_id
|
29
|
+
end
|
30
|
+
|
31
|
+
def api_key
|
32
|
+
auth_proxy.api_key
|
33
|
+
end
|
34
|
+
|
35
|
+
def session_id=(session_id)
|
36
|
+
@auth_proxy = CS::Auth::HTTP.new(@base_uri)
|
37
|
+
@auth_proxy.session_id = session_id
|
38
|
+
end
|
39
|
+
|
40
|
+
def api_key=(api_key)
|
41
|
+
@api_key = api_key
|
42
|
+
@auth_proxy = CS::Auth::HTTP.new(@base_uri, api_key)
|
43
|
+
end
|
44
|
+
|
45
|
+
def auth_proxy
|
46
|
+
raise 'The session is not logged in' unless @auth_proxy
|
47
|
+
@auth_proxy
|
48
|
+
end
|
49
|
+
|
50
|
+
def retry_on_509(&block)
|
51
|
+
while true
|
52
|
+
response = yield
|
53
|
+
if response_code == 509
|
54
|
+
waitfor = Random.new.rand(30..45)
|
55
|
+
logger.info "limit reached. waiting for #{waitfor} before retrying" if logger
|
56
|
+
sleep(waitfor)
|
57
|
+
else
|
58
|
+
return response
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def log_request(type, path, body, headers)
|
64
|
+
logger.info("")
|
65
|
+
logger.info("#{type} #{path}")
|
66
|
+
logger.debug("headers: #{headers.inspect}")
|
67
|
+
logger.info("parameters: #{body.inspect}")
|
68
|
+
end
|
69
|
+
|
70
|
+
def log_response
|
71
|
+
logger.info("response: #{self.response_code}")
|
72
|
+
logger.debug("body: #{self.response_body}")
|
73
|
+
end
|
74
|
+
|
75
|
+
def execute(type, path, body, headers, &block)
|
76
|
+
log_request(type, path, body, headers) if logger
|
77
|
+
response = retry_on_509 { yield }
|
78
|
+
log_response if logger
|
79
|
+
|
80
|
+
response
|
81
|
+
end
|
82
|
+
|
83
|
+
def get(path, body = '', headers = {})
|
84
|
+
execute("GET", path, body, headers) do
|
85
|
+
auth_proxy.get(path, body, headers)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def post(path, body = '', headers = {})
|
90
|
+
execute("POST", path, body, headers) do
|
91
|
+
auth_proxy.post(path, body, headers = {})
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def put(path, body = '', headers = {})
|
96
|
+
execute("PUT", path, body, headers) do
|
97
|
+
auth_proxy.put(path, body, headers)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def delete(path, body='', headers = {})
|
102
|
+
execute("DELETE", path, body, headers) do
|
103
|
+
auth_proxy.delete(path, body, headers)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def head(path, headers = {})
|
108
|
+
execute("HEAD", path, nil, headers) do
|
109
|
+
auth_proxy.head(path, headers)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def response_code
|
114
|
+
auth_proxy.response_code
|
115
|
+
end
|
116
|
+
|
117
|
+
def response_body
|
118
|
+
auth_proxy.response_body
|
119
|
+
end
|
120
|
+
|
121
|
+
def response_headers
|
122
|
+
auth_proxy.response_headers
|
123
|
+
end
|
124
|
+
|
125
|
+
def errors
|
126
|
+
auth_proxy.errors
|
127
|
+
end
|
128
|
+
|
129
|
+
def base_uri=(uri = nil)
|
130
|
+
auth_proxy.base_uri = uri
|
131
|
+
end
|
132
|
+
|
133
|
+
def dump_to_text(path)
|
134
|
+
begin
|
135
|
+
body = response_body.to_s
|
136
|
+
rescue Exception => e
|
137
|
+
body = e.message
|
138
|
+
end
|
139
|
+
|
140
|
+
File.open(path, 'w') do |f|
|
141
|
+
f.write("Response Code: #{response_code}\n")
|
142
|
+
f.write("Response Headers: #{response_headers}\n")
|
143
|
+
f.write("Errors: #{errors}\n")
|
144
|
+
f.write("\n")
|
145
|
+
f.write(body)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def open_in_browser(path=nil)
|
150
|
+
require 'launchy'
|
151
|
+
|
152
|
+
path ||= "/tmp/common-sense-ruby-#{Time.now.to_i}.html"
|
153
|
+
dump_to_text(path)
|
154
|
+
::Launchy::Browser.run(path)
|
155
|
+
end
|
156
|
+
|
157
|
+
def to_s
|
158
|
+
if session_id
|
159
|
+
return "SESSION_ID \"#{session_id}\""
|
160
|
+
elsif api_key
|
161
|
+
return "API_KEY \"#{api_key}\""
|
162
|
+
end
|
163
|
+
return ""
|
164
|
+
end
|
165
|
+
|
166
|
+
def inspect
|
167
|
+
auth_proxy.kind_of?(CS::Auth::HTTP) ? to_s : "OAuth"
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|