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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 63eed43cc77b773bac24f95586ee557c5f190faa
4
- data.tar.gz: 00bb1526dd327a88d7a84a6eae2a22c5917278ed
3
+ metadata.gz: 5d677266003749366fcffe2d64d691c4428769be
4
+ data.tar.gz: d2848fca4408702e7247db0cbf8cd6ee146fcb28
5
5
  SHA512:
6
- metadata.gz: 0642bc7c0e29c0d90eb0b776710fa26e902de07c23675ed6e818ea245266373fcba3762d7f1958403ebbb7e3ebd08909dede27f1118e02741b64b61e758bd028
7
- data.tar.gz: eb8f5b6c67d4d3582fcf5df831f677ea4e897edfb99417136753c15e052db63d40d6e93c4e220ace15a2362597d1fa7eb540d9178352afb119b772d20cccadc2
6
+ metadata.gz: d8f2304107d5a3d125a590c69ba370592b36723c9f336a75cc62774be62a11e8ae0f227242e2eb6b71d0b00d1db4995be422c9ffa2e9885170f501b61e7322a6
7
+ data.tar.gz: 7a9cdf5cd49cc9afb08b9ba6cca9882ba1ed85b32ed62ff1d59157e9a68a6fa6543f2c5041cb4c5a5e26563c59c1a4f76495eaa3a46fc4ddf58337c7735cb36c
@@ -1,5 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.1.2
3
4
  - 2.0.0
4
5
  - 1.9.3
5
6
  - jruby-19mode
data/lib/cs.rb CHANGED
@@ -62,6 +62,10 @@ module CS
62
62
 
63
63
  def base_uri=(uri)
64
64
  @base_uri = uri
65
+ unless session
66
+ @session = Session.new()
67
+ end
68
+
65
69
  session.base_uri = uri
66
70
  end
67
71
 
@@ -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
- self.base_uri = base_uri
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
- headers = default_headers.merge(headers)
41
- options = {query: query, headers: 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
- headers = default_headers.merge(headers)
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
- headers = default_headers.merge(headers)
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
- headers = default_headers.merge(headers)
64
- options = {query: query, headers: 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
- self.class.base_uri uri
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
- headers = default_headers.merge(headers)
132
- {:body => body.to_json, :headers => headers}
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
@@ -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
- headers = default_headers.merge(headers)
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
- headers = default_headers.merge(headers)
42
- oauth.post(path, body.to_json, headers)
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
- headers = default_headers.merge(headers)
49
- oauth.put(path, body.to_json, headers)
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
- headers = default_headers.merge(headers)
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
- headers = default_headers.merge(headers)
65
+ @request_headers = default_headers.merge(headers)
64
66
  oauth.head(path, headers)
65
67
  end
66
68
  end
@@ -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, body, headers)
66
+ def log_request(type, path)
64
67
  logger.info("")
65
68
  logger.info("#{type} #{path}")
66
- logger.debug("headers: #{headers.inspect}")
69
+ logger.debug("headers: #{@auth_proxy.request_headers.inspect}")
67
70
  if ["POST", "PUT"].include?(type)
68
- logger.debug("request: #{body.inspect}")
71
+ logger.debug("request: #{@auth_proxy.request_body.inspect}")
69
72
  else
70
- logger.info("request: #{body.inspect}")
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
- log_request(type, path, body, headers) if logger
82
- response = retry_on_509 { yield }
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)
@@ -1,3 +1,3 @@
1
1
  module CS
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.6"
3
3
  end
@@ -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
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-06-18 00:00:00.000000000 Z
11
+ date: 2014-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: launchy