cs 0.1.4 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/lib/cs.rb +4 -0
- data/lib/cs/auth/http.rb +22 -16
- data/lib/cs/auth/oauth.rb +11 -9
- data/lib/cs/session.rb +13 -7
- data/lib/cs/version.rb +1 -1
- data/spec/lib/cs/session_spec.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d677266003749366fcffe2d64d691c4428769be
|
4
|
+
data.tar.gz: d2848fca4408702e7247db0cbf8cd6ee146fcb28
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d8f2304107d5a3d125a590c69ba370592b36723c9f336a75cc62774be62a11e8ae0f227242e2eb6b71d0b00d1db4995be422c9ffa2e9885170f501b61e7322a6
|
7
|
+
data.tar.gz: 7a9cdf5cd49cc9afb08b9ba6cca9882ba1ed85b32ed62ff1d59157e9a68a6fa6543f2c5041cb4c5a5e26563c59c1a4f76495eaa3a46fc4ddf58337c7735cb36c
|
data/.travis.yml
CHANGED
data/lib/cs.rb
CHANGED
data/lib/cs/auth/http.rb
CHANGED
@@ -6,11 +6,11 @@ module CS
|
|
6
6
|
class HTTP
|
7
7
|
include HTTParty
|
8
8
|
|
9
|
-
attr_accessor :response_body, :response_code, :response_headers, :errors
|
9
|
+
attr_accessor :request_body, :request_headers, :response_body, :response_code, :response_headers, :errors, :logger, :base_uri
|
10
10
|
attr_reader :session_id, :api_key
|
11
11
|
|
12
12
|
def initialize(base_uri = nil, api_key = nil)
|
13
|
-
|
13
|
+
@base_uri = base_uri
|
14
14
|
@api_key = api_key
|
15
15
|
@session_id = nil
|
16
16
|
reset
|
@@ -35,45 +35,47 @@ module CS
|
|
35
35
|
path
|
36
36
|
end
|
37
37
|
|
38
|
+
def process_path(path)
|
39
|
+
return base_uri + path
|
40
|
+
end
|
41
|
+
|
38
42
|
def get(path, query={}, headers = {})
|
39
43
|
execute do
|
40
|
-
|
41
|
-
options = {query: query, headers:
|
42
|
-
path = process_api_key(path)
|
44
|
+
@request_headers = default_headers.merge(headers)
|
45
|
+
options = {query: query, headers: @request_headers}
|
46
|
+
path = process_api_key(process_path(path))
|
43
47
|
self.class.get(path, options)
|
44
48
|
end
|
45
49
|
end
|
46
50
|
|
47
51
|
def post(path, body = '', headers = {})
|
48
52
|
execute do
|
49
|
-
|
50
|
-
self.class.post(path, prepare(body, headers))
|
53
|
+
self.class.post(process_path(path), prepare(body, headers))
|
51
54
|
end
|
52
55
|
end
|
53
56
|
|
54
57
|
def put(path, body = '', headers = {})
|
55
58
|
execute do
|
56
|
-
|
57
|
-
self.class.put(path, prepare(body, headers))
|
59
|
+
self.class.put(process_path(path), prepare(body, headers))
|
58
60
|
end
|
59
61
|
end
|
60
62
|
|
61
63
|
def delete(path, query={}, headers = {})
|
62
64
|
execute do
|
63
|
-
|
64
|
-
options = {query: query, headers:
|
65
|
-
self.class.delete(path, options)
|
65
|
+
@request_headers = default_headers.merge(headers)
|
66
|
+
options = {query: query, headers: @request_headers}
|
67
|
+
self.class.delete(process_path(path), options)
|
66
68
|
end
|
67
69
|
end
|
68
70
|
|
69
71
|
def head(path, headers = {})
|
70
72
|
execute do
|
71
|
-
self.class.head(path, prepare(nil, headers))
|
73
|
+
self.class.head(process_path(path), prepare(nil, headers))
|
72
74
|
end
|
73
75
|
end
|
74
76
|
|
75
77
|
def base_uri=(uri = nil)
|
76
|
-
|
78
|
+
@base_uri = uri
|
77
79
|
end
|
78
80
|
|
79
81
|
def default_headers
|
@@ -116,6 +118,8 @@ module CS
|
|
116
118
|
@errors = nil
|
117
119
|
@response_code = nil
|
118
120
|
@response_body = nil
|
121
|
+
@request_body = nil
|
122
|
+
@request_headers = nil
|
119
123
|
end
|
120
124
|
|
121
125
|
def parse_response
|
@@ -128,8 +132,10 @@ module CS
|
|
128
132
|
end
|
129
133
|
|
130
134
|
def prepare(body=nil, headers={})
|
131
|
-
|
132
|
-
|
135
|
+
@request_headers = default_headers.merge(headers)
|
136
|
+
@request_body = body.to_json
|
137
|
+
|
138
|
+
{:body => @request_body, :headers => @request_headers}
|
133
139
|
end
|
134
140
|
end
|
135
141
|
end
|
data/lib/cs/auth/oauth.rb
CHANGED
@@ -4,8 +4,8 @@ module CS
|
|
4
4
|
module Auth
|
5
5
|
class OAuth
|
6
6
|
|
7
|
-
attr_accessor :response_body, :response_code, :response_headers, :errors, :consumer_key,
|
8
|
-
:consumer_secret, :access_token, :access_token_secret
|
7
|
+
attr_accessor :request_body, :request_headers, :response_body, :response_code, :response_headers, :errors, :consumer_key,
|
8
|
+
:consumer_secret, :access_token, :access_token_secret, :logger
|
9
9
|
|
10
10
|
def initialize(consumer_key, consumer_secret, access_token, access_token_secret, uri=nil)
|
11
11
|
@consumer_key = consumer_key
|
@@ -31,36 +31,38 @@ module CS
|
|
31
31
|
def get(path, query={}, headers = {})
|
32
32
|
execute do
|
33
33
|
path += '?' + URI.encode_www_form(query) unless query.empty?
|
34
|
-
|
34
|
+
@request_headers = default_headers.merge(headers)
|
35
35
|
oauth.get(path, headers)
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
def post(path, body = '', headers = {})
|
40
40
|
execute do
|
41
|
-
|
42
|
-
|
41
|
+
@request_headers = default_headers.merge(headers)
|
42
|
+
@request_body = body.to_json
|
43
|
+
oauth.post(path, @request_body, headers)
|
43
44
|
end
|
44
45
|
end
|
45
46
|
|
46
47
|
def put(path, body = '', headers = {})
|
47
48
|
execute do
|
48
|
-
|
49
|
-
|
49
|
+
@request_headers = default_headers.merge(headers)
|
50
|
+
@request_body = body.to_json
|
51
|
+
oauth.put(path, @request_body, headers)
|
50
52
|
end
|
51
53
|
end
|
52
54
|
|
53
55
|
def delete(path, query={}, headers = {})
|
54
56
|
execute do
|
55
57
|
path += '?' + URI.encode_www_form(query) unless query.empty?
|
56
|
-
|
58
|
+
@request_headers = default_headers.merge(headers)
|
57
59
|
oauth.delete(path, headers)
|
58
60
|
end
|
59
61
|
end
|
60
62
|
|
61
63
|
def head(path, headers = {})
|
62
64
|
execute do
|
63
|
-
|
65
|
+
@request_headers = default_headers.merge(headers)
|
64
66
|
oauth.head(path, headers)
|
65
67
|
end
|
66
68
|
end
|
data/lib/cs/session.rb
CHANGED
@@ -15,6 +15,7 @@ module CS
|
|
15
15
|
# @return [String] session_id
|
16
16
|
def login(username, password, digest=true)
|
17
17
|
@auth_proxy = CS::Auth::HTTP.new(@base_uri)
|
18
|
+
@auth_proxy.logger = self.logger
|
18
19
|
@auth_proxy.login(username, password, digest)
|
19
20
|
end
|
20
21
|
|
@@ -22,6 +23,8 @@ module CS
|
|
22
23
|
@auth_proxy = CS::Auth::OAuth.new(consumer_key, consumer_secret,
|
23
24
|
access_token, access_token_secret,
|
24
25
|
@base_uri)
|
26
|
+
@auth_proxy.logger = self.logger if @auth_proxy
|
27
|
+
@auth_proxy
|
25
28
|
end
|
26
29
|
|
27
30
|
def session_id
|
@@ -60,14 +63,14 @@ module CS
|
|
60
63
|
end
|
61
64
|
end
|
62
65
|
|
63
|
-
def log_request(type, path
|
66
|
+
def log_request(type, path)
|
64
67
|
logger.info("")
|
65
68
|
logger.info("#{type} #{path}")
|
66
|
-
logger.debug("headers: #{
|
69
|
+
logger.debug("headers: #{@auth_proxy.request_headers.inspect}")
|
67
70
|
if ["POST", "PUT"].include?(type)
|
68
|
-
logger.debug("request: #{
|
71
|
+
logger.debug("request: #{@auth_proxy.request_body.inspect}")
|
69
72
|
else
|
70
|
-
logger.info("request: #{
|
73
|
+
logger.info("request: #{@auth_proxy.request_body.inspect}")
|
71
74
|
end
|
72
75
|
end
|
73
76
|
|
@@ -78,8 +81,11 @@ module CS
|
|
78
81
|
|
79
82
|
def execute(type, path, body, headers, &block)
|
80
83
|
start_time = Time.now
|
81
|
-
|
82
|
-
|
84
|
+
response = retry_on_509 do
|
85
|
+
value = yield
|
86
|
+
log_request(type, path) if logger
|
87
|
+
value
|
88
|
+
end
|
83
89
|
|
84
90
|
elapsed = (Time.now - start_time) * 1000.0
|
85
91
|
log_response(elapsed) if logger
|
@@ -135,7 +141,7 @@ module CS
|
|
135
141
|
|
136
142
|
def base_uri=(uri = nil)
|
137
143
|
@base_uri = uri
|
138
|
-
auth_proxy.base_uri = uri
|
144
|
+
auth_proxy.base_uri = uri if @auth_proxy
|
139
145
|
end
|
140
146
|
|
141
147
|
def dump_to_text(path)
|
data/lib/cs/version.rb
CHANGED
data/spec/lib/cs/session_spec.rb
CHANGED
@@ -56,7 +56,7 @@ describe "session" do
|
|
56
56
|
session.logger = logger
|
57
57
|
logger.should_receive("info").with("").ordered
|
58
58
|
logger.should_receive("info").with("GET /users/current.json").ordered
|
59
|
-
logger.should_receive("debug").with("headers: {}").ordered
|
59
|
+
logger.should_receive("debug").with("headers: {\"Content-Type\"=>\"application/json\"}").ordered
|
60
60
|
session.get('/users/current.json', '',{})
|
61
61
|
end
|
62
62
|
end
|
@@ -79,7 +79,7 @@ describe "session" do
|
|
79
79
|
client = create_client
|
80
80
|
client.api_key = '123456'
|
81
81
|
CS::Auth::HTTP.should_receive(:get).
|
82
|
-
with("/sensors.json?API_KEY=123456",
|
82
|
+
with("http://api.dev.sense-os.local/sensors.json?API_KEY=123456",
|
83
83
|
{:query=>{:page=>0, :per_page=>1000},
|
84
84
|
:headers=>{"Content-Type"=>"application/json"}}
|
85
85
|
)
|
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.1.
|
4
|
+
version: 0.1.6
|
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-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: launchy
|