cs 0.1.6 → 0.2.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 +4 -4
- data/.travis.yml +6 -3
- data/CHANGELOG.md +4 -0
- data/Gemfile +2 -1
- data/README.md +16 -7
- data/lib/cs.rb +21 -4
- data/lib/cs/auth/http.rb +1 -0
- data/lib/cs/session.rb +14 -4
- data/lib/cs/version.rb +1 -1
- data/spec/lib/cs/session_spec.rb +3 -1
- data/spec/lib/cs_spec.rb +45 -0
- data/spec/spec_helper.rb +12 -0
- data/test.rb +25 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65314f9bb5ad1ee8bb734e9a787ec6593b59c5ec
|
4
|
+
data.tar.gz: aa4f03d2c222473803addfe489038be17db58489
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7eaf6550b00069705edb1c11dde22e3eda0ed839776881e1bb646a37817aa83c046adb163307bff80f7bf8f8699a08d27eb9071a60b0250114d0c2dc5c28e1ee
|
7
|
+
data.tar.gz: 139898e8a861b7ffe9b9659e6ed085105b3b6a9c1f9a9be54adc9c1f33bf10cc58e941037cca90f96edc3239edc56ed84bc6201424f21261e2df17a0b4836dc0
|
data/.travis.yml
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
|
-
- 2.1.
|
3
|
+
- 2.1.5
|
4
4
|
- 2.0.0
|
5
5
|
- 1.9.3
|
6
6
|
- jruby-19mode
|
7
|
-
- rbx-2.2.
|
8
|
-
- jruby
|
7
|
+
- rbx-2.2.10
|
8
|
+
- jruby
|
9
9
|
script: bundle exec rspec spec
|
10
|
+
addons:
|
11
|
+
code_climate:
|
12
|
+
repo_token: a4b19928b743eda49f7acaba02190126122bfa09bb2615395aa1820a7cce9fc0
|
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
@@ -10,13 +10,14 @@ gem 'json'
|
|
10
10
|
#
|
11
11
|
|
12
12
|
platforms :rbx do
|
13
|
-
gem 'rubysl'
|
13
|
+
# gem 'rubysl'
|
14
14
|
end
|
15
15
|
|
16
16
|
group :test do
|
17
17
|
gem "rake", "~> 10.1.0"
|
18
18
|
gem "webmock", "~> 1.13.0"
|
19
19
|
gem "rspec", "~> 2.14.1"
|
20
|
+
gem "codeclimate-test-reporter", require: nil
|
20
21
|
end
|
21
22
|
|
22
23
|
group :development do
|
data/README.md
CHANGED
@@ -4,6 +4,7 @@ Client library to communicate with CommonSense written in ruby
|
|
4
4
|
[](http://badge.fury.io/rb/cs)
|
5
5
|
[](https://codeclimate.com/github/senseobservationsystems/commonsense-ruby-lib)
|
6
6
|
[](https://travis-ci.org/senseobservationsystems/commonsense-ruby-lib)
|
7
|
+
[](https://codeclimate.com/github/senseobservationsystems/commonsense-ruby-lib)
|
7
8
|
|
8
9
|
RDoc [Documentation](http://rdoc.info/github/senseobservationsystems/commonsense-ruby-lib/frames)
|
9
10
|
|
@@ -108,9 +109,20 @@ data.save!
|
|
108
109
|
sensor.data.build(date: Time.now, value: {"lux" => 1}).save!
|
109
110
|
```
|
110
111
|
|
112
|
+
### Making manual API call
|
113
|
+
|
114
|
+
With the client we could interact with CS API using the client object
|
115
|
+
|
116
|
+
example
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
client.get('/users/current.json')
|
120
|
+
client.post('/sensors.json', {"params": "bar"})
|
121
|
+
```
|
122
|
+
|
111
123
|
### Debuging
|
112
124
|
|
113
|
-
To get information about the API response (body, code, headers)
|
125
|
+
To get information about the API response (body, code, headers) use this method
|
114
126
|
|
115
127
|
example
|
116
128
|
|
@@ -118,12 +130,9 @@ example
|
|
118
130
|
# do some API call
|
119
131
|
current_user = client.current_user
|
120
132
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
response_code = session.response_code
|
125
|
-
response_body = session.resopnse_body
|
126
|
-
header = session.response_headers
|
133
|
+
response_code = client.response_code
|
134
|
+
response_body = client.resopnse_body
|
135
|
+
header = client.response_headers
|
127
136
|
|
128
137
|
# dump the output to text file
|
129
138
|
session.dump_to_text("/tmp/output.txt")
|
data/lib/cs.rb
CHANGED
@@ -50,8 +50,13 @@ module CS
|
|
50
50
|
# client.base_uri = 'https://api.dev.sense-os.nl'
|
51
51
|
#
|
52
52
|
class Client
|
53
|
+
extend Forwardable
|
54
|
+
|
53
55
|
attr_accessor :session
|
54
|
-
|
56
|
+
|
57
|
+
def_delegators :delegator, :get, :post, :put, :delete, :head,
|
58
|
+
:response_code, :response_body, :response_headers, :errors,
|
59
|
+
:dump_to_txt, :open_in_browser
|
55
60
|
|
56
61
|
def initialize(opts={})
|
57
62
|
options = {
|
@@ -74,6 +79,10 @@ module CS
|
|
74
79
|
@session.logger = logger if @session
|
75
80
|
end
|
76
81
|
|
82
|
+
def logger
|
83
|
+
@logger
|
84
|
+
end
|
85
|
+
|
77
86
|
# Create a new session to CommonSense using username and plain text password
|
78
87
|
# with `login!` it will throw exception if there is an error
|
79
88
|
#
|
@@ -81,7 +90,7 @@ module CS
|
|
81
90
|
# client.login!('username', 'password')
|
82
91
|
def login!(user, password, digest=true)
|
83
92
|
@session = Session.new(base_uri: @base_uri)
|
84
|
-
@session.logger = logger
|
93
|
+
@session.logger = @logger
|
85
94
|
@session.login(user, password, digest)
|
86
95
|
end
|
87
96
|
|
@@ -100,7 +109,7 @@ module CS
|
|
100
109
|
# client.login('username', 'password')
|
101
110
|
def oauth(consumer_key, consumer_secret, access_token, access_token_secret)
|
102
111
|
@session = Session.new(base_uri: @base_uri)
|
103
|
-
@session.logger = logger
|
112
|
+
@session.logger = @logger
|
104
113
|
@session.oauth(consumer_key, consumer_secret, access_token, access_token_secret)
|
105
114
|
end
|
106
115
|
|
@@ -110,7 +119,7 @@ module CS
|
|
110
119
|
# client.session_id = '12345'
|
111
120
|
def session_id=(session_id)
|
112
121
|
@session = Session.new(base_uri: @base_uri)
|
113
|
-
@session.logger = logger
|
122
|
+
@session.logger = @logger
|
114
123
|
@session.session_id = session_id
|
115
124
|
end
|
116
125
|
|
@@ -176,10 +185,18 @@ module CS
|
|
176
185
|
def errors
|
177
186
|
return @session.errors if @session
|
178
187
|
end
|
188
|
+
|
189
|
+
private
|
190
|
+
def delegator
|
191
|
+
raise Error::SessionEmptyError unless session
|
192
|
+
@session
|
193
|
+
end
|
179
194
|
end
|
180
195
|
|
181
196
|
def self.load_CLI
|
182
197
|
require "cs/cli/cli"
|
183
198
|
end
|
199
|
+
|
200
|
+
|
184
201
|
end
|
185
202
|
|
data/lib/cs/auth/http.rb
CHANGED
data/lib/cs/session.rb
CHANGED
@@ -16,7 +16,14 @@ module CS
|
|
16
16
|
def login(username, password, digest=true)
|
17
17
|
@auth_proxy = CS::Auth::HTTP.new(@base_uri)
|
18
18
|
@auth_proxy.logger = self.logger
|
19
|
-
|
19
|
+
|
20
|
+
start_time = Time.now
|
21
|
+
result = @auth_proxy.login(username, password, digest)
|
22
|
+
elapsed = (Time.now - start_time) * 1000.0
|
23
|
+
log_request("Logging in", "")
|
24
|
+
log_response(elapsed)
|
25
|
+
|
26
|
+
result
|
20
27
|
end
|
21
28
|
|
22
29
|
def oauth(consumer_key, consumer_secret, access_token, access_token_secret)
|
@@ -64,17 +71,20 @@ module CS
|
|
64
71
|
end
|
65
72
|
|
66
73
|
def log_request(type, path)
|
74
|
+
return if logger.nil?
|
75
|
+
|
67
76
|
logger.info("")
|
68
|
-
logger.info("#{type} #{path}")
|
77
|
+
logger.info("#{type} #{base_uri}#{path}")
|
69
78
|
logger.debug("headers: #{@auth_proxy.request_headers.inspect}")
|
70
79
|
if ["POST", "PUT"].include?(type)
|
71
80
|
logger.debug("request: #{@auth_proxy.request_body.inspect}")
|
72
81
|
else
|
73
|
-
logger.
|
82
|
+
logger.debug("request: #{@auth_proxy.request_body.inspect}")
|
74
83
|
end
|
75
84
|
end
|
76
85
|
|
77
86
|
def log_response(elapsed)
|
87
|
+
return if logger.nil?
|
78
88
|
logger.info("result: #{self.response_code} in #{elapsed}ms")
|
79
89
|
logger.debug("response: #{self.response_body}")
|
80
90
|
end
|
@@ -88,7 +98,7 @@ module CS
|
|
88
98
|
end
|
89
99
|
|
90
100
|
elapsed = (Time.now - start_time) * 1000.0
|
91
|
-
log_response(elapsed)
|
101
|
+
log_response(elapsed)
|
92
102
|
|
93
103
|
response
|
94
104
|
end
|
data/lib/cs/version.rb
CHANGED
data/spec/lib/cs/session_spec.rb
CHANGED
@@ -55,8 +55,10 @@ describe "session" do
|
|
55
55
|
logger = double().as_null_object
|
56
56
|
session.logger = logger
|
57
57
|
logger.should_receive("info").with("").ordered
|
58
|
-
logger.should_receive("info").with("GET /users/current.json").ordered
|
58
|
+
logger.should_receive("info").with("GET http://api.dev.sense-os.local/users/current.json").ordered
|
59
59
|
logger.should_receive("debug").with("headers: {\"Content-Type\"=>\"application/json\"}").ordered
|
60
|
+
logger.should_receive("debug").with("request: \"{\\\"username\\\":\\\"user1@tester.com\\\",\\\"password\\\":\\\"5f4dcc3b5aa765d61d8327deb882cf99\\\"}\""
|
61
|
+
).ordered
|
60
62
|
session.get('/users/current.json', '',{})
|
61
63
|
end
|
62
64
|
end
|
data/spec/lib/cs_spec.rb
CHANGED
@@ -80,6 +80,51 @@ module CS
|
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
|
+
describe "get" do
|
84
|
+
it 'shoudl delegate method to session' do
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "post" do
|
89
|
+
it 'shoudl delegate method to session' do
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe "put" do
|
94
|
+
it 'shoudl delegate method to session' do
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "delete" do
|
99
|
+
it 'shoudl delegate method to session' do
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe "head" do
|
104
|
+
it 'shoudl delegate method to session' do
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe "response_code" do
|
109
|
+
it 'shoudl delegate method to session' do
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe "response_body" do
|
114
|
+
it 'shoudl delegate method to session' do
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe "response_headers" do
|
119
|
+
it 'shoudl delegate method to session' do
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
describe "errors" do
|
124
|
+
it 'shoudl delegate method to session' do
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
83
128
|
end
|
84
129
|
end
|
85
130
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,12 +1,24 @@
|
|
1
|
+
#ENV['CODECLIMATE_REPO_TOKEN'] = "a4b19928b743eda49f7acaba02190126122bfa09bb2615395aa1820a7cce9fc0"
|
2
|
+
# code climate
|
3
|
+
if ENV['CODECLIMATE_REPO_TOKEN']
|
4
|
+
require "codeclimate-test-reporter"
|
5
|
+
CodeClimate::TestReporter.start
|
6
|
+
end
|
7
|
+
|
1
8
|
require 'bundler/setup'
|
2
9
|
require 'rspec'
|
3
10
|
require 'cs'
|
4
11
|
require 'pry'
|
5
12
|
|
13
|
+
|
6
14
|
Dir[File.join(File.dirname(__FILE__),("support/**/*.rb"))].each {|f| require f}
|
7
15
|
|
8
16
|
RSpec.configure do |config|
|
9
17
|
config.treat_symbols_as_metadata_keys_with_true_values = true
|
18
|
+
|
19
|
+
config.after(:suite) do
|
20
|
+
WebMock.disable_net_connect!(:allow => 'codeclimate.com')
|
21
|
+
end
|
10
22
|
end
|
11
23
|
|
12
24
|
def create_client
|
data/test.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'pry'
|
3
|
+
require 'cs'
|
4
|
+
|
5
|
+
logger = Logger.new(STDOUT)
|
6
|
+
logger.level = Logger::DEBUG
|
7
|
+
cl1 = CS::Client.new
|
8
|
+
cl1.logger = logger
|
9
|
+
|
10
|
+
puts "loggin cl1"
|
11
|
+
cl1.login!('ahmy+sf@sense-os.nl', 'sfpassword')
|
12
|
+
puts "current_user cl1"
|
13
|
+
cl1.current_user
|
14
|
+
|
15
|
+
cl2 = CS::Client.new(base_uri: 'http://api.test.sense-os.nl')
|
16
|
+
cl2.logger = logger
|
17
|
+
puts "loggin cl2"
|
18
|
+
cl2.login('a', 'a')
|
19
|
+
puts "current_user cl2"
|
20
|
+
cl2.current_user
|
21
|
+
|
22
|
+
cl1.current_user
|
23
|
+
puts "current_user cl1 again"
|
24
|
+
|
25
|
+
binding.pry
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ahmy Yulrizka
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: launchy
|
@@ -78,6 +78,7 @@ extra_rdoc_files: []
|
|
78
78
|
files:
|
79
79
|
- .gitignore
|
80
80
|
- .travis.yml
|
81
|
+
- CHANGELOG.md
|
81
82
|
- Gemfile
|
82
83
|
- LICENSE
|
83
84
|
- README.md
|
@@ -132,6 +133,7 @@ files:
|
|
132
133
|
- spec/spec_helper.rb
|
133
134
|
- spec/support/spec_config.yml.sample
|
134
135
|
- spec/support/vcr.rb
|
136
|
+
- test.rb
|
135
137
|
homepage: https://github.com/senseobservationsystems/commonsense-ruby-lib
|
136
138
|
licenses: []
|
137
139
|
metadata: {}
|