songkick-transport 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ require './lib/songkick/transport'
2
+
3
+ t = Songkick::Transport::Curb.new 'localhost:4567'
4
+ r = Songkick::Transport.report
5
+
6
+ r.execute { t.get '/' }
7
+
8
+ p r.first.duration
9
+
@@ -6,7 +6,7 @@ get '/' do
6
6
  end
7
7
 
8
8
  get '/slow' do
9
- sleep 60
9
+ sleep 3
10
10
  '{"helloooooo":"world"}'
11
11
  end
12
12
 
@@ -7,16 +7,18 @@ module Songkick
7
7
  HTTP_VERBS.each do |verb|
8
8
  class_eval %{
9
9
  def #{verb}(path, params = {}, head = {}, timeout = nil)
10
- req = Request.new(endpoint, '#{verb}', path, params, headers.merge(head), timeout, Time.now)
10
+ req = Request.new(endpoint, '#{verb}', path, params, headers.merge(head), timeout)
11
11
  Reporting.log_request(req)
12
12
 
13
- response = execute_request(req)
13
+ req.response = execute_request(req)
14
+
15
+ Reporting.log_response(req)
16
+ Reporting.record(req)
17
+ req.response
14
18
 
15
- Reporting.log_response(response, req)
16
- Reporting.record(req, response)
17
- response
18
19
  rescue => error
19
- Reporting.record(req, nil, error)
20
+ req.error = error
21
+ Reporting.record(req)
20
22
  raise error
21
23
  end
22
24
  }
@@ -45,7 +45,7 @@ module Songkick
45
45
  end
46
46
 
47
47
  if req.use_body?
48
- connection.headers['Content-Type'] = req.content_type
48
+ connection.headers['Content-Type'] ||= req.content_type
49
49
  connection.__send__("http_#{req.verb}", req.body)
50
50
  else
51
51
  connection.__send__("http_#{req.verb}")
@@ -24,19 +24,20 @@ module Songkick
24
24
  def #{verb}(path, params = {}, head = {}, timeout = nil)
25
25
  client = Client.new(@app)
26
26
  start = Time.now
27
- request = Request.new(@app, '#{verb}', path, params, headers.merge(head), timeout, start)
27
+ request = Request.new(@app, '#{verb}', path, params, headers.merge(head), timeout)
28
28
  result = nil
29
29
 
30
30
  Timeout.timeout(timeout || @timeout) do
31
31
  request.headers.each { |key, value| client.header(key, value) }
32
32
  response = client.#{verb}(path, params)
33
- result = process("\#{path}, \#{params.inspect}", response.status, response.headers, response.body)
34
- Reporting.record(request, result)
35
- result
33
+ request.response = process("\#{path}, \#{params.inspect}", response.status, response.headers, response.body)
34
+ Reporting.record(request)
35
+ request.response
36
36
  end
37
37
 
38
38
  rescue UpstreamError => error
39
- Reporting.record(request, nil, error)
39
+ request.error = error
40
+ Reporting.record(request)
40
41
  raise error
41
42
 
42
43
  rescue Object => error
@@ -6,9 +6,9 @@ module Songkick
6
6
  Report.new
7
7
  end
8
8
 
9
- def self.record(request, response, error = nil)
9
+ def self.record(request)
10
10
  return unless report = Thread.current[:songkick_transport_report]
11
- report.record(request, response, error)
11
+ report << request
12
12
  end
13
13
 
14
14
  def self.log_request(request)
@@ -16,8 +16,9 @@ module Songkick
16
16
  logger.info(request.to_s)
17
17
  end
18
18
 
19
- def self.log_response(response, request)
19
+ def self.log_response(request)
20
20
  return unless Transport.verbose?
21
+ response = request.response
21
22
  duration = (Time.now.to_f - request.start_time.to_f) * 1000
22
23
  logger.info "Response status: #{response.status}, duration: #{duration.ceil}ms"
23
24
  logger.debug "Response data: #{response.data.inspect}"
@@ -30,7 +31,7 @@ module Songkick
30
31
  class Report
31
32
  include Enumerable
32
33
  extend Forwardable
33
- def_delegators :@requests, :each, :first, :last, :length, :size, :[]
34
+ def_delegators :@requests, :each, :first, :last, :length, :size, :[], :<<
34
35
 
35
36
  def initialize
36
37
  @requests = []
@@ -43,12 +44,6 @@ module Songkick
43
44
  Thread.current[:songkick_transport_report] = nil
44
45
  end
45
46
 
46
- def record(request, response, error)
47
- request.response = response
48
- request.error = error
49
- @requests << request
50
- end
51
-
52
47
  def total_duration
53
48
  inject(0) { |s,r| s + r.duration }
54
49
  end
@@ -2,9 +2,6 @@ module Songkick
2
2
  module Transport
3
3
 
4
4
  class Request
5
- attr_accessor :response,
6
- :error
7
-
8
5
  attr_reader :endpoint,
9
6
  :verb,
10
7
  :path,
@@ -12,24 +9,37 @@ module Songkick
12
9
  :headers,
13
10
  :timeout,
14
11
  :start_time,
15
- :duration
12
+ :response,
13
+ :error
16
14
 
17
15
  alias :http_method :verb
18
16
 
19
- def initialize(endpoint, verb, path, params, headers = {}, timeout = DEFAULT_TIMEOUT, start_time = nil, response = nil, error = nil)
17
+ def initialize(endpoint, verb, path, params, headers = {}, timeout = DEFAULT_TIMEOUT)
20
18
  @endpoint = endpoint
21
19
  @verb = verb.to_s.downcase
22
20
  @path = path
23
21
  @headers = headers
24
22
  @params = params
25
23
  @timeout = timeout
26
- @response = response
27
- @error = error
28
24
  @start_time = start_time || Time.now
29
- @duration = (Time.now.to_f - start_time.to_f) * 1000
30
25
  @multipart = Serialization.multipart?(params)
31
26
  end
32
27
 
28
+ def response=(response)
29
+ @response = response
30
+ @end_time = Time.now
31
+ end
32
+
33
+ def error=(error)
34
+ @error = error
35
+ @end_time = Time.now
36
+ end
37
+
38
+ def duration
39
+ return nil unless @end_time
40
+ (@end_time.to_f - @start_time.to_f) * 1000
41
+ end
42
+
33
43
  def use_body?
34
44
  USE_BODY.include?(@verb)
35
45
  end
@@ -61,7 +71,10 @@ module Songkick
61
71
  end
62
72
 
63
73
  def to_s
64
- url = Serialization.build_url(@verb, @endpoint, @path, @params, true)
74
+ url = String === @endpoint ?
75
+ Serialization.build_url(@verb, @endpoint, @path, @params, true) :
76
+ @endpoint.to_s
77
+
65
78
  command = "#{@verb.upcase} '#{url}'"
66
79
  @headers.each do |key, value|
67
80
  value = Serialization::SANITIZED_VALUE if Serialization.sanitize?(key)
@@ -59,6 +59,11 @@ describe Songkick::Transport do
59
59
  data = transport.get("/authenticate").data
60
60
  data.should == {"successful" => false}
61
61
  end
62
+
63
+ it "can set Content-Type" do
64
+ data = transport.with_headers("Content-Type" => "application/json").post("/content").data
65
+ data.should == {"type" => "application/json"}
66
+ end
62
67
  end
63
68
 
64
69
  describe :post do
@@ -34,6 +34,10 @@ class TestApp < Sinatra::Base
34
34
  Yajl::Encoder.encode('id' => 'new', 'name' => params[:name].upcase)
35
35
  end
36
36
 
37
+ post '/content' do
38
+ Yajl::Encoder.encode('type' => env['CONTENT_TYPE'])
39
+ end
40
+
37
41
  put '/artists/:id' do
38
42
  name = params[:name] || CGI.parse(env['rack.input'].read)['name'].first || ''
39
43
  Yajl::Encoder.encode('id' => params[:id].to_i, 'name' => name.downcase)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: songkick-transport
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-31 00:00:00.000000000 Z
12
+ date: 2012-08-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multipart-post
@@ -167,6 +167,7 @@ files:
167
167
  - examples/loop.rb
168
168
  - examples/thread_safety.rb
169
169
  - examples/server.rb
170
+ - examples/foo.rb
170
171
  - examples/example.rb
171
172
  - lib/songkick/transport/request.rb
172
173
  - lib/songkick/transport/rack_test.rb