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
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c9c07ce0761efba489c25ac1286348c278d43024
|
4
|
+
data.tar.gz: a200d01e595e45f19d4188a68b3089ab864222f0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ee82e700560960c985a55829cba9987952b619b20395435d111948695f36550e9c3f4d5ea6d2bc1a4ae784c78dacdba6c3fb710c4268affcc80782e364b2fcce
|
7
|
+
data.tar.gz: 32cbaac901131587917bc9a9ada98678399cab0d34bec5ba83cd50daee54f13c08377c7879b82ffb7a39bd659dea8b2861b19424bb4fcfdc8d0dd63a25d9e7a5
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
@@ -1,10 +1,26 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
# Specify your gem's dependencies in
|
3
|
+
# Specify your gem's dependencies in cs.gemspec
|
4
4
|
gemspec
|
5
|
-
gem "pry"
|
6
|
-
gem "pry-nav"
|
7
|
-
gem "pry-doc"
|
8
|
-
gem "redcarpet"
|
9
5
|
gem "time-lord"
|
10
|
-
|
6
|
+
gem 'json'
|
7
|
+
|
8
|
+
#
|
9
|
+
# Rubinius does not load ruby stdlib by default
|
10
|
+
#
|
11
|
+
|
12
|
+
platforms :rbx do
|
13
|
+
gem 'rubysl'
|
14
|
+
end
|
15
|
+
|
16
|
+
group :test do
|
17
|
+
gem "rake", "~> 10.1.0"
|
18
|
+
gem "webmock", "~> 1.13.0"
|
19
|
+
gem "rspec", "~> 2.14.1"
|
20
|
+
end
|
21
|
+
|
22
|
+
group :development do
|
23
|
+
gem "pry"
|
24
|
+
gem "pry-nav"
|
25
|
+
gem "pry-doc"
|
26
|
+
end
|
data/README.md
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
# Commonsense::Ruby::Lib
|
2
2
|
Client library to communicate with CommonSense written in ruby
|
3
3
|
|
4
|
+
[](https://codeclimate.com/github/senseobservationsystems/commonsense-ruby-lib)
|
5
|
+
[](https://travis-ci.org/senseobservationsystems/commonsense-ruby-lib)
|
6
|
+
|
4
7
|
## Installation
|
5
8
|
|
6
9
|
Add this line to your application's Gemfile:
|
7
10
|
|
8
|
-
gem '
|
11
|
+
gem 'cs'
|
9
12
|
|
10
13
|
And then execute:
|
11
14
|
|
@@ -13,15 +16,20 @@ And then execute:
|
|
13
16
|
|
14
17
|
Or install it yourself as:
|
15
18
|
|
16
|
-
$ gem install
|
19
|
+
$ gem install cs
|
17
20
|
|
18
21
|
## Usage
|
19
22
|
|
20
23
|
### Authentication
|
21
24
|
```ruby
|
22
|
-
client =
|
25
|
+
client = CS::Client.new
|
23
26
|
client.login('username','password')
|
24
27
|
|
28
|
+
# setting session_id manually
|
29
|
+
|
30
|
+
client = CS::Client.new
|
31
|
+
client.session_id = '1234'
|
32
|
+
|
25
33
|
# get current user
|
26
34
|
current_user = client.current_user
|
27
35
|
```
|
@@ -32,13 +40,16 @@ current_user = client.current_user
|
|
32
40
|
sensors = client.sensors
|
33
41
|
|
34
42
|
# is the same as
|
35
|
-
sensors =
|
43
|
+
sensors = CS::Relation::Sensors.new
|
36
44
|
sensors.session = session
|
37
45
|
|
38
46
|
# Get all sensor
|
39
47
|
sensors = client.sensors
|
40
48
|
sensors.to_a
|
41
49
|
|
50
|
+
# show parameters available when querying
|
51
|
+
client.sensors.parameters
|
52
|
+
|
42
53
|
# Get sensor by specifying parameters
|
43
54
|
client.sensors.where(page: 0, per_page: 1000)
|
44
55
|
client.sensors.where(owned: true)
|
data/cs.gemspec
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
require File.expand_path('../lib/
|
2
|
+
require File.expand_path('../lib/cs/version', __FILE__)
|
3
3
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
5
|
gem.authors = ["Ahmy Yulrizka"]
|
@@ -8,15 +8,15 @@ Gem::Specification.new do |gem|
|
|
8
8
|
gem.summary = %q{Client library to communicate with CommonSense written in ruby}
|
9
9
|
gem.homepage = "https://github.com/senseobservationsystems/commonsense-ruby-lib"
|
10
10
|
|
11
|
+
gem.required_ruby_version = '>= 1.9.3'
|
12
|
+
|
11
13
|
gem.files = `git ls-files`.split($\)
|
12
14
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
15
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
16
|
gem.name = "cs"
|
15
17
|
gem.require_paths = ["lib"]
|
16
|
-
gem.version =
|
17
|
-
gem.add_development_dependency("
|
18
|
-
gem.
|
19
|
-
gem.
|
20
|
-
gem.add_dependency('httparty', '~> 0.11.0')
|
21
|
-
gem.add_dependency('oauth', '~> 0.4.7')
|
18
|
+
gem.version = CS::VERSION
|
19
|
+
gem.add_development_dependency("launchy", "~> 2.3")
|
20
|
+
gem.add_dependency('httparty', '~> 0.12')
|
21
|
+
gem.add_dependency('oauth', '~> 0.4')
|
22
22
|
end
|
@@ -1,18 +1,27 @@
|
|
1
|
-
require "
|
2
|
-
require "
|
3
|
-
require "
|
4
|
-
require "
|
5
|
-
require "
|
6
|
-
require "
|
7
|
-
require "
|
8
|
-
require "
|
9
|
-
require "
|
10
|
-
require "
|
11
|
-
require "
|
12
|
-
require "
|
13
|
-
require "
|
14
|
-
|
15
|
-
|
1
|
+
require "cs/version"
|
2
|
+
require "cs/error"
|
3
|
+
require "cs/time"
|
4
|
+
require "cs/auth/http"
|
5
|
+
require "cs/auth/oauth"
|
6
|
+
require "cs/session"
|
7
|
+
require "cs/end_point"
|
8
|
+
require "cs/end_point/user"
|
9
|
+
require "cs/end_point/group"
|
10
|
+
require "cs/end_point/sensor"
|
11
|
+
require "cs/end_point/sensor_data"
|
12
|
+
require "cs/end_point/trigger"
|
13
|
+
require "cs/end_point/notification"
|
14
|
+
require "cs/parameter_processor"
|
15
|
+
require "cs/relation"
|
16
|
+
require "cs/relation/sensor_relation"
|
17
|
+
require "cs/relation/sensor_data_relation"
|
18
|
+
require "cs/relation/user_relation"
|
19
|
+
require "cs/relation/group_relation"
|
20
|
+
require "cs/relation/trigger_relation"
|
21
|
+
require "cs/relation/notification_relation"
|
22
|
+
require "cs/collection/sensor_data_collection"
|
23
|
+
|
24
|
+
module CS
|
16
25
|
|
17
26
|
# Main entry class of the library.
|
18
27
|
#
|
@@ -20,28 +29,29 @@ module CommonSense
|
|
20
29
|
# will raise an exception when there is an error and the normal (without !)
|
21
30
|
# will return nil when it fails.
|
22
31
|
#
|
23
|
-
# The response can be viewed by looking at the {
|
32
|
+
# The response can be viewed by looking at the {CS::Session}
|
24
33
|
#
|
25
34
|
# client.session # will return the session object
|
26
35
|
#
|
27
36
|
# == Authentication with User And Password
|
28
37
|
#
|
29
|
-
# client =
|
38
|
+
# client = CS::Client.new
|
30
39
|
# client.login('username', 'password')
|
31
40
|
#
|
32
41
|
# == Authentication using OAuth
|
33
42
|
#
|
34
|
-
# client =
|
43
|
+
# client = CS::Client.new
|
35
44
|
# client.oauth('CONSUMER_KEY', 'CONSUMER_SECRET', 'ACCESS_TOKEN', 'ACCESS_TOKEN_SECRET')
|
36
45
|
#
|
37
46
|
# == Using different API server
|
38
47
|
#
|
39
|
-
# client =
|
48
|
+
# client = CS::Client.new(base_uri: 'https://api.dev.sense-os.nl')
|
40
49
|
# # or
|
41
50
|
# client.base_uri = 'https://api.dev.sense-os.nl'
|
42
51
|
#
|
43
52
|
class Client
|
44
53
|
attr_accessor :session, :base_uri
|
54
|
+
attr_reader :logger
|
45
55
|
|
46
56
|
def initialize(opts={})
|
47
57
|
options = {
|
@@ -50,43 +60,61 @@ module CommonSense
|
|
50
60
|
@base_uri = options[:base_uri]
|
51
61
|
end
|
52
62
|
|
63
|
+
def logger=(logger)
|
64
|
+
@logger = logger
|
65
|
+
@session.logger = logger if @session
|
66
|
+
end
|
67
|
+
|
53
68
|
# Create a new session to CommonSense using username and plain text password
|
54
69
|
# with `login!` it will throw exception if there is an error
|
55
70
|
#
|
56
|
-
# client =
|
71
|
+
# client = CS::Client.new
|
57
72
|
# client.login!('username', 'password')
|
58
|
-
def login!(user, password)
|
73
|
+
def login!(user, password, digest=true)
|
59
74
|
@session = Session.new(base_uri: @base_uri)
|
60
|
-
@session.
|
75
|
+
@session.logger = logger
|
76
|
+
@session.login(user, password, digest)
|
61
77
|
end
|
62
78
|
|
63
79
|
# Create a new session to CommonSense using username and plain text password
|
64
80
|
# with `login` it will return nil if it not successful
|
65
81
|
#
|
66
|
-
# client =
|
82
|
+
# client = CS::Client.new
|
67
83
|
# client.login('username', 'password')
|
68
|
-
def login(user, password)
|
69
|
-
login!(user, password) rescue
|
84
|
+
def login(user, password, digest=true)
|
85
|
+
login!(user, password, digest) rescue false
|
70
86
|
end
|
71
87
|
|
72
88
|
# Create a new session to CommonSense using OAuth credentials
|
73
89
|
#
|
74
|
-
# client =
|
90
|
+
# client = CS::Client.new
|
75
91
|
# client.login('username', 'password')
|
76
92
|
def oauth(consumer_key, consumer_secret, access_token, access_token_secret)
|
77
93
|
@session = Session.new(base_uri: @base_uri)
|
94
|
+
@session.logger = logger
|
78
95
|
@session.oauth(consumer_key, consumer_secret, access_token, access_token_secret)
|
79
96
|
end
|
80
97
|
|
81
98
|
# Create new session by manually specifiying `session_id` parameter
|
82
99
|
#
|
83
|
-
# client =
|
84
|
-
# client.session_id
|
85
|
-
def
|
100
|
+
# client = CS::Client.new
|
101
|
+
# client.session_id = '12345'
|
102
|
+
def session_id=(session_id)
|
86
103
|
@session = Session.new(base_uri: @base_uri)
|
104
|
+
@session.logger = logger
|
87
105
|
@session.session_id = session_id
|
88
106
|
end
|
89
107
|
|
108
|
+
# Create new session by specifying api_key
|
109
|
+
#
|
110
|
+
# client = CS::Client.new
|
111
|
+
# client.session_id = '12345'
|
112
|
+
def api_key=(api_key)
|
113
|
+
@session = Session.new(base_uri: @base_uri)
|
114
|
+
@session.logger = logger
|
115
|
+
@session.api_key = api_key
|
116
|
+
end
|
117
|
+
|
90
118
|
# Retrun logged in user
|
91
119
|
def current_user
|
92
120
|
user = EndPoint::User.new
|
@@ -96,7 +124,7 @@ module CommonSense
|
|
96
124
|
|
97
125
|
# Create a new user
|
98
126
|
#
|
99
|
-
# client =
|
127
|
+
# client = CS::Client.new
|
100
128
|
# client.new_user(username: 'Ahmy')
|
101
129
|
# client.email = 'ahmy@gmail.com'
|
102
130
|
# ...
|
@@ -107,21 +135,42 @@ module CommonSense
|
|
107
135
|
user
|
108
136
|
end
|
109
137
|
|
110
|
-
|
138
|
+
def users
|
139
|
+
Relation::UserRelation.new(@session)
|
140
|
+
end
|
141
|
+
|
142
|
+
# @see CS::Relation::SensorRelation
|
111
143
|
def sensors
|
112
144
|
Relation::SensorRelation.new(@session)
|
113
145
|
end
|
114
146
|
|
147
|
+
def groups
|
148
|
+
Relation::GroupRelation.new(@session)
|
149
|
+
end
|
150
|
+
|
151
|
+
def triggers
|
152
|
+
Relation::TriggerRelation.new(@session)
|
153
|
+
end
|
154
|
+
|
155
|
+
def notifications
|
156
|
+
Relation::NotificationRelation.new(@session)
|
157
|
+
end
|
158
|
+
|
115
159
|
def current_groups
|
116
160
|
group = EndPoint::Group.new
|
117
161
|
group.session = @session
|
118
162
|
group.current_groups
|
119
163
|
end
|
120
164
|
|
165
|
+
|
121
166
|
# return errors got from session
|
122
167
|
def errors
|
123
168
|
return @session.errors if @session
|
124
169
|
end
|
125
170
|
end
|
171
|
+
|
172
|
+
def self.load_CLI
|
173
|
+
require "cs/cli/cli"
|
174
|
+
end
|
126
175
|
end
|
127
176
|
|
@@ -1,59 +1,75 @@
|
|
1
1
|
require "httparty"
|
2
2
|
require "json"
|
3
3
|
|
4
|
-
module
|
4
|
+
module CS
|
5
5
|
module Auth
|
6
6
|
class HTTP
|
7
7
|
include HTTParty
|
8
8
|
|
9
9
|
attr_accessor :response_body, :response_code, :response_headers, :errors
|
10
|
-
attr_reader :session_id
|
10
|
+
attr_reader :session_id, :api_key
|
11
11
|
|
12
|
-
def initialize(base_uri = nil)
|
12
|
+
def initialize(base_uri = nil, api_key = nil)
|
13
13
|
self.base_uri = base_uri
|
14
|
+
@api_key = api_key
|
14
15
|
@session_id = nil
|
15
16
|
reset
|
16
17
|
end
|
17
18
|
|
18
|
-
def
|
19
|
+
def execute(&block)
|
19
20
|
reset
|
20
|
-
|
21
|
-
options = {query: query, headers: headers}
|
22
|
-
@response_body = self.class.get(path, options)
|
21
|
+
@response_body = yield
|
23
22
|
parse_response
|
24
23
|
@response_body
|
25
24
|
end
|
26
25
|
|
26
|
+
def process_api_key(path)
|
27
|
+
return path if @api_key.nil?
|
28
|
+
|
29
|
+
if URI(path).query.nil?
|
30
|
+
path += "?API_KEY=#{@api_key}"
|
31
|
+
else
|
32
|
+
path += "&API_KEY=#{@api_key}"
|
33
|
+
end
|
34
|
+
|
35
|
+
path
|
36
|
+
end
|
37
|
+
|
38
|
+
def get(path, query={}, headers = {})
|
39
|
+
execute do
|
40
|
+
headers = default_headers.merge(headers)
|
41
|
+
options = {query: query, headers: headers}
|
42
|
+
path = process_api_key(path)
|
43
|
+
self.class.get(path, options)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
27
47
|
def post(path, body = '', headers = {})
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
@response_body
|
48
|
+
execute do
|
49
|
+
headers = default_headers.merge(headers)
|
50
|
+
self.class.post(path, prepare(body, headers))
|
51
|
+
end
|
33
52
|
end
|
34
53
|
|
35
54
|
def put(path, body = '', headers = {})
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
@response_body
|
55
|
+
execute do
|
56
|
+
headers = default_headers.merge(headers)
|
57
|
+
self.class.put(path, prepare(body, headers))
|
58
|
+
end
|
41
59
|
end
|
42
60
|
|
43
61
|
def delete(path, query={}, headers = {})
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
@response_body
|
62
|
+
execute do
|
63
|
+
headers = default_headers.merge(headers)
|
64
|
+
options = {query: query, headers: headers}
|
65
|
+
self.class.delete(path, options)
|
66
|
+
end
|
50
67
|
end
|
51
68
|
|
52
69
|
def head(path, headers = {})
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
@response_body
|
70
|
+
execute do
|
71
|
+
self.class.head(path, prepare(nil, headers))
|
72
|
+
end
|
57
73
|
end
|
58
74
|
|
59
75
|
def base_uri=(uri = nil)
|
@@ -62,9 +78,9 @@ module CommonSense
|
|
62
78
|
|
63
79
|
def default_headers
|
64
80
|
header = self.class.default_options[:headers] || {}
|
65
|
-
header.merge({"Content-Type" => "application/json"})
|
81
|
+
header.merge!({"Content-Type" => "application/json"})
|
66
82
|
if @session_id
|
67
|
-
header.merge
|
83
|
+
header = header.merge('X-SESSION_ID' => self.session_id)
|
68
84
|
end
|
69
85
|
header
|
70
86
|
end
|
@@ -77,17 +93,19 @@ module CommonSense
|
|
77
93
|
@session_id = session_id
|
78
94
|
end
|
79
95
|
|
80
|
-
|
81
96
|
# login to commonsense
|
82
97
|
# @return [String] session_id
|
83
|
-
def login(username, password)
|
84
|
-
|
98
|
+
def login(username, password, digest=true)
|
99
|
+
if digest
|
100
|
+
password = Digest::MD5.hexdigest password
|
101
|
+
end
|
85
102
|
post('/login.json', {:username => username, :password => password})
|
86
103
|
|
87
104
|
if @response_code == 200
|
88
105
|
self.session_id = response_body['session_id']
|
89
106
|
else
|
90
|
-
|
107
|
+
self.session_id = false
|
108
|
+
@errors = [response_body['error']]
|
91
109
|
end
|
92
110
|
|
93
111
|
session_id
|
@@ -101,6 +119,7 @@ module CommonSense
|
|
101
119
|
end
|
102
120
|
|
103
121
|
def parse_response
|
122
|
+
return unless @response_body
|
104
123
|
@response_code = @response_body.response.code.to_i
|
105
124
|
@response_headers = @response_body.headers
|
106
125
|
if @response_code >= 400
|