cfoundry 0.3.35 → 0.3.36

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.
@@ -1,18 +1,19 @@
1
1
  require "restclient"
2
2
  require "multi_json"
3
+ require "fileutils"
3
4
 
4
5
  module CFoundry
5
6
  class BaseClient # :nodoc:
6
- LOG_FILE = File.expand_path("~/.cfoundry.log")
7
7
  LOG_LENGTH = 10
8
8
 
9
- attr_accessor :trace, :no_backtrace
9
+ attr_accessor :trace, :backtrace, :log
10
10
 
11
11
  def initialize(target, token = nil)
12
12
  @target = target
13
13
  @token = token
14
14
  @trace = false
15
- @no_backtrace = false
15
+ @backtrace = false
16
+ @log = false
16
17
  end
17
18
 
18
19
  def request_path(method, path, types = {}, options = {})
@@ -98,9 +99,13 @@ module CFoundry
98
99
 
99
100
  json = accept == :json
100
101
 
102
+ before = Time.now
103
+
101
104
  RestClient::Request.execute(req) do |response, request|
105
+ time = Time.now - before
106
+
102
107
  print_trace(req, request, response, caller) if @trace
103
- log_request(req, response)
108
+ log_request(time, req, response)
104
109
  handle_response(response, accept)
105
110
  end
106
111
  rescue SocketError, Errno::ECONNREFUSED => e
@@ -168,21 +173,55 @@ module CFoundry
168
173
  }.join("/")
169
174
  end
170
175
 
171
- def log_request(req, response)
172
- file = File.expand_path(LOG_FILE)
176
+ def log_data(time, request, response)
177
+ { :time => time,
178
+ :request => request,
179
+ :response => {
180
+ :code => response.code,
181
+ :headers => response.headers,
182
+ :body => response
183
+ }
184
+ }
185
+ end
173
186
 
174
- if File.exists?(file)
175
- log = File.readlines(file).last(LOG_LENGTH - 1)
176
- end
187
+ def log_line(io, data)
188
+ io.printf(
189
+ "[%s] %0.3fs %6s -> %d %s\n",
190
+ Time.now.strftime("%F %T"),
191
+ data[:time],
192
+ data[:request][:method].to_s.upcase,
193
+ data[:response][:code],
194
+ data[:request][:url])
195
+ end
196
+
197
+ def log_request(time, request, response)
198
+ return unless @log
177
199
 
178
- File.open(file, "w") do |io|
179
- log.each { |l| io.print l } if log
180
- io.puts log_line(req, response.headers[:x_vcap_request_id])
200
+ data = log_data(time, request, response)
201
+
202
+ if @log.is_a?(Array)
203
+ @log << data
204
+ return
181
205
  end
182
- end
183
206
 
184
- def log_line(req, id)
185
- "#{Time.now.strftime("%F %T")} #{id} #{req[:method].to_s.upcase.ljust(6)} #{req[:url]}"
207
+ case @log
208
+ when Array
209
+ @log << data
210
+ return
211
+ when IO
212
+ log_line(@log, data)
213
+ when String
214
+ if File.exists?(@log)
215
+ log = File.readlines(@log).last(LOG_LENGTH - 1)
216
+ elsif !File.exists?(File.dirname(@log))
217
+ FileUtils.mkdir_p(File.dirname(@log))
218
+ end
219
+
220
+ File.open(@log, "w") do |io|
221
+ log.each { |l| io.print l } if log
222
+ log_line(io, data)
223
+ end
224
+ end
186
225
  end
187
226
 
188
227
  def print_trace(req, request, response, locs)
@@ -207,7 +246,7 @@ module CFoundry
207
246
  end
208
247
  $stderr.puts "<<<"
209
248
 
210
- return if @no_backtrace
249
+ return unless @backtrace
211
250
 
212
251
  interesting_locs = locs.drop_while { |loc|
213
252
  loc =~ /\/(cfoundry\/|restclient\/|net\/http)/
@@ -7,19 +7,12 @@ require "cfoundry/errors"
7
7
 
8
8
  module CFoundry::V1
9
9
  class Base < CFoundry::BaseClient
10
- attr_accessor :target, :token, :proxy, :trace
10
+ attr_accessor :target, :token, :proxy, :trace, :backtrace, :log
11
11
 
12
12
  def initialize(
13
13
  target = "https://api.cloudfoundry.com",
14
14
  token = nil)
15
- @target = target
16
- @token = token
17
- end
18
-
19
-
20
- # invalidate token data when changing token
21
- def token=(t)
22
- @token = t
15
+ super
23
16
  end
24
17
 
25
18
 
@@ -46,6 +46,21 @@ module CFoundry::V1
46
46
  @base.trace = bool
47
47
  end
48
48
 
49
+ # The current log. See +log=+.
50
+ def log
51
+ @base.log
52
+ end
53
+
54
+ # Set the logging mode. Mode can be one of:
55
+ #
56
+ # [+String+] Name of a file to log the last 10 requests to.
57
+ # [+Array+] Array to append with log data (a Hash).
58
+ # [+IO+] An IO object to write to.
59
+ # [+false+] No logging.
60
+ def log=(mode)
61
+ @base.log = mode
62
+ end
63
+
49
64
  # The currently authenticated user.
50
65
  def current_user
51
66
  if user = info[:user]
@@ -8,19 +8,12 @@ require "cfoundry/errors"
8
8
 
9
9
  module CFoundry::V2
10
10
  class Base < CFoundry::BaseClient
11
- attr_accessor :target, :token, :proxy, :trace
11
+ attr_accessor :target, :token, :proxy, :trace, :backtrace, :log
12
12
 
13
13
  def initialize(
14
14
  target = "https://api.cloudfoundry.com",
15
15
  token = nil)
16
- @target = target
17
- @token = token
18
- end
19
-
20
-
21
- # invalidate token data when changing token
22
- def token=(t)
23
- @token = t
16
+ super
24
17
  end
25
18
 
26
19
 
@@ -171,5 +164,16 @@ module CFoundry::V2
171
164
  raise CFoundry::BadResponse.new(response.code, response)
172
165
  end
173
166
  end
167
+
168
+ def log_line(io, data)
169
+ io.printf(
170
+ "[%s] %0.3fs %s %6s -> %d %s\n",
171
+ Time.now.strftime("%F %T"),
172
+ data[:time],
173
+ data[:response][:headers][:x_vcap_request_id],
174
+ data[:request][:method].to_s.upcase,
175
+ data[:response][:code],
176
+ data[:request][:url])
177
+ end
174
178
  end
175
179
  end
@@ -66,6 +66,21 @@ module CFoundry::V2
66
66
  @base.trace = bool
67
67
  end
68
68
 
69
+ # The current log. See +log=+.
70
+ def log
71
+ @base.log
72
+ end
73
+
74
+ # Set the logging mode. Mode can be one of:
75
+ #
76
+ # [+String+] Name of a file to log the last 10 requests to.
77
+ # [+Array+] Array to append with log data (a Hash).
78
+ # [+IO+] An IO object to write to.
79
+ # [+false+] No logging.
80
+ def log=(mode)
81
+ @base.log = mode
82
+ end
83
+
69
84
  # The currently authenticated user.
70
85
  def current_user
71
86
  if guid = @base.token_data[:user_id]
@@ -1,4 +1,4 @@
1
1
  module CFoundry # :nodoc:
2
2
  # CFoundry library version number.
3
- VERSION = "0.3.35"
3
+ VERSION = "0.3.36"
4
4
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cfoundry
3
3
  version: !ruby/object:Gem::Version
4
- hash: 85
4
+ hash: 91
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 35
10
- version: 0.3.35
9
+ - 36
10
+ version: 0.3.36
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alex Suraci
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-09-26 00:00:00 Z
18
+ date: 2012-09-27 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rest-client