restify 0.3.2.1.b62 → 0.3.2.1.b63

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZGMzYTA4ODFiZDdhYTBkMTQyZTA4YTIxNmM5NDcwZmQ0Yjk4MjFkYQ==
4
+ YWFhOGZjNThlNDZmMzRjNTY5ZmM0MDE5NjFmNGIxZDk2OGRiMGY5MA==
5
5
  data.tar.gz: !binary |-
6
- ODg4MzU4NWY4ZjRjZGM4N2IyNWM3Nzk3NjY4NjI4ZTVjZjU1MTExOQ==
6
+ MzFjYTQ3NmExMGU0NDgyNjY1YWU2ZjEyMmZjYzQxZDlkM2ZlZDA1Zg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- M2U5M2YyZmQ5MzJmYmE3ODZhY2M2NDRiYTYwMmNiYTc3ZmYzY2VmN2Y0N2Ix
10
- NDkxMmIxNDFlOTU3NmRkZmU0ZGRlMmU5NDdjY2UyOWFlN2I1NzgwOGZjZTQw
11
- MmIxNzc2M2QzN2Q3ZWNiYmIzNjBhZmUwMjBmYTk4YzljMmRlNmQ=
9
+ ZDkzZDk1OWZmZjkyNjRjYjVjYmMzMGRiYjVjYzNlYWIxZTVkMDYyZDQ0YzBi
10
+ ZDFjMjUwNzRkMGJhZmEzNzBlZWU3NWQxNzE0ZjRmZjUyMTkxYjNlNDQ4YWRi
11
+ ZmUxMTA5YWYyM2U0ZmVlMWFhMzM4MzZiMTJhMzE5OGU2YmNhY2I=
12
12
  data.tar.gz: !binary |-
13
- ZWYzZjQ1M2ZiMTRiZDIzOTYyMzgzM2ExMDYzODg2YzVlYjM2Yzk1ZjFlMzZi
14
- NTc3YjZlMGJiNzcxYWFhNWE0YjBkMTIzNmQzM2Y5OGQ2NzcwNDY1MmEwMGJh
15
- ZGI0NTRiN2RlMDgzNzBiMjIwZGRjNjMyNGNiODAwMzFiNjdkOGY=
13
+ NTI0YTQ2NmIyZTM5MTU4NzM2YTY4MzIwMDZkMjFkOWY5ZjU4NWFmMDk4ODQx
14
+ MzIxOGYwZTZjNjg0OGY3ZjI4YTEwZDI5MzA4MjVlN2MwZTQxMmUwZmZjZDk1
15
+ ZTEyNzgxZjM5ZGY0NTRkOWY4ZTY3MjFiMjQ1YmFiNDYxZTBkODU=
@@ -16,12 +16,14 @@ module Restify
16
16
  body: request.body,
17
17
  socket_class: ::Celluloid::IO::TCPSocket
18
18
 
19
- writer.fulfill Response.new \
20
- request,
21
- response.uri,
22
- response.status.code,
23
- response.headers.to_h,
24
- response.body.to_s
19
+ uri = response.uri
20
+ status = response.status.code
21
+ body = response.body.to_s
22
+ headers = response.headers.to_h.each_with_object({}) do |header, hash|
23
+ hash[header[0].upcase.tr('-', '_')] = header[1]
24
+ end
25
+
26
+ writer.fulfill Response.new request, uri, status, headers, body
25
27
  end
26
28
 
27
29
  def call(request)
@@ -0,0 +1,69 @@
1
+ require 'typhoeus'
2
+
3
+ module Restify
4
+ #
5
+ module Adapter
6
+ #
7
+ class Typhoeus
8
+
9
+ def initialize
10
+ @queue = Queue.new
11
+ @hydra = ::Typhoeus::Hydra.new
12
+
13
+ Thread.new do
14
+ begin
15
+ loop do
16
+ while (req = convert(*@queue.pop))
17
+ @hydra.queue req
18
+
19
+ if @queue.size == 0
20
+ @hydra.run
21
+ end
22
+ end
23
+ end
24
+ rescue Exception => e
25
+ puts "#{self.class}: #{e.message}"
26
+ end
27
+ end
28
+ end
29
+
30
+ def queue(request, writer)
31
+ @queue.push [request, writer]
32
+ end
33
+
34
+ def call(request)
35
+ Obligation.create do |writer|
36
+ queue request, writer
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def convert(request, writer)
43
+ req = ::Typhoeus::Request.new \
44
+ request.uri,
45
+ method: request.method,
46
+ headers: request.headers,
47
+ body: request.body
48
+
49
+ req.on_complete do |response|
50
+ uri = request.uri
51
+ status = response.code
52
+ body = response.body
53
+
54
+ headers = response.headers.each_with_object({}) do |header, hash|
55
+ hash[header[0].upcase.tr('-', '_')] = header[1]
56
+ end
57
+
58
+ writer.fulfill Response.new request, uri, status, headers, body
59
+
60
+ while @queue.size > 0
61
+ @hydra.queue convert(*@queue.pop(true))
62
+ end
63
+ end
64
+
65
+ req
66
+ end
67
+ end
68
+ end
69
+ end
@@ -12,9 +12,8 @@ module Restify
12
12
  #
13
13
  attr_reader :uri
14
14
 
15
- def initialize(uri, http)
15
+ def initialize(uri)
16
16
  @uri = uri.is_a?(Addressable::URI) ? uri : Addressable::URI.parse(uri.to_s)
17
- @http = http
18
17
  end
19
18
 
20
19
  def join(uri)
@@ -22,7 +21,7 @@ module Restify
22
21
  end
23
22
 
24
23
  def process(response)
25
- context = Context.new response.uri, @http
24
+ context = Context.new response.uri
26
25
  processor = Restify::PROCESSORS.find { |p| p.accept? response }
27
26
  processor ||= Restify::Processors::Base
28
27
 
@@ -30,8 +29,9 @@ module Restify
30
29
  end
31
30
 
32
31
  def request(method, uri, data = nil, opts = {})
33
- request = @http.request(method, join(uri), data, opts)
34
- request.then do |response|
32
+ request = Request.new method: method, uri: join(uri), data: data
33
+
34
+ Restify.adapter.call(request).then do |response|
35
35
  if response.success?
36
36
  process response
37
37
  else
data/lib/restify.rb CHANGED
@@ -7,7 +7,6 @@ require 'addressable/template'
7
7
 
8
8
  #
9
9
  module Restify
10
- require 'restify/http'
11
10
  require 'restify/error'
12
11
 
13
12
  require 'restify/context'
@@ -27,11 +26,18 @@ module Restify
27
26
 
28
27
  class << self
29
28
  def new(uri, opts = {})
30
- Context.new(uri, http).request(:GET, uri)
29
+ Context.new(uri).request(:GET, uri)
31
30
  end
32
31
 
33
- def http
34
- @http ||= HTTP.new
32
+ def adapter
33
+ @adapter ||= begin
34
+ require 'restify/adapter/em'
35
+ Restify::Adapter::EM.new
36
+ end
37
+ end
38
+
39
+ def adapter=(adapter)
40
+ @adapter = adapter
35
41
  end
36
42
  end
37
43
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Restify::Processors::Json do
4
- let(:context) { Restify::Context.new('http://test.host/', nil) }
4
+ let(:context) { Restify::Context.new('http://test.host/') }
5
5
  let(:response) { double 'response' }
6
6
 
7
7
  before do
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Restify::Relation do
4
- let(:context) { Restify::Context.new('http://test.host/', nil) }
4
+ let(:context) { Restify::Context.new('http://test.host/') }
5
5
  let(:pattern) { '/resource/{id}' }
6
6
  let(:relation) { described_class.new context, pattern }
7
7
  subject { relation }
data/spec/spec_helper.rb CHANGED
@@ -10,6 +10,20 @@ end
10
10
 
11
11
  require 'restify'
12
12
 
13
+ case ENV['ADAPTER'].downcase
14
+ when 'em-http-request'
15
+ require 'restify/adapter/em'
16
+ Restify.adapter = Restify::Adapter::EM.new
17
+ when 'typhoeus'
18
+ require 'restify/adapter/typhoeus'
19
+ Restify.adapter = Restify::Adapter::Typhoeus.new
20
+ when 'celluloid'
21
+ require 'restify/adapter/celluloid'
22
+ Restify.adapter = Restify::Adapter::Celluloid.new
23
+ else
24
+ raise "Invalid adapter: #{ENV['ADAPTER']}"
25
+ end
26
+
13
27
  require 'eventmachine'
14
28
  require 'rspec/collection_matchers'
15
29
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2.1.b62
4
+ version: 0.3.2.1.b63
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Graichen
@@ -107,9 +107,9 @@ files:
107
107
  - lib/restify.rb
108
108
  - lib/restify/adapter/celluloid.rb
109
109
  - lib/restify/adapter/em.rb
110
+ - lib/restify/adapter/typhoeus.rb
110
111
  - lib/restify/context.rb
111
112
  - lib/restify/error.rb
112
- - lib/restify/http.rb
113
113
  - lib/restify/link.rb
114
114
  - lib/restify/processors/base.rb
115
115
  - lib/restify/processors/json.rb
data/lib/restify/http.rb DELETED
@@ -1,33 +0,0 @@
1
- require 'restify/adapter/em'
2
-
3
- module Restify
4
- # @api private
5
- #
6
- class HTTP
7
- # @api private
8
- #
9
- def initialize
10
- @adapter = self.class.adapter
11
- end
12
-
13
- # @api private
14
- #
15
- # Request given path with given method.
16
- #
17
- # Returns an obligation that will return a collection or
18
- # resource or fail with a response error depending on
19
- # response from server.
20
- #
21
- def request(method, uri, data = nil, _opts = {})
22
- request = Request.new method: method, uri: uri, data: data
23
-
24
- @adapter.call(request)
25
- end
26
-
27
- class << self
28
- def adapter
29
- @adapter ||= Restify::Adapter::EM.new
30
- end
31
- end
32
- end
33
- end