restify 1.0.0 → 1.0.1.beta1

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: 9f5b0ad969a7b5ab4889aa69bd9c5d0dde4a7e5f
4
- data.tar.gz: 57fb5334cfbdba973c5df3569450c18413b3884b
3
+ metadata.gz: 3b00cff50046f420707ed6d6e8851d74e1392f5f
4
+ data.tar.gz: fa5553ba030b9ec8bcd2457d4c24a866ad1db293
5
5
  SHA512:
6
- metadata.gz: 6a8cb08d62f1b91d7087e1e1ae75c0afd4278280c1334e072db3673d8388c341f957362019d81d92aa6832bcd9d04ae6c59ed5f578ef09edea486d497dfc9176
7
- data.tar.gz: 78668d83c4b4d6867ab6e25663de52ab5804da8380019bd3bf39f4bacb5f667499c20a421ca0049ef323395da7605b99ee7d2eadb07b1fbb25f2124a1fa27e21
6
+ metadata.gz: 76ad0f4229b26ba714763793a086ba2cffb62642fa1ebb329037e1b542a5802ace3f29acc1220100488f7010b2eae5963a729d812475d17fa7834e43079a1004
7
+ data.tar.gz: 32908687b5e5b3234ccc5985cf1184a179d60f5589383482a4dff87abf168f7dbbefe101b4f7a4fcbe0d5ccb10b0b2d5e2c9a46dbae11dfd9cf54ddedb94baae
@@ -3,30 +3,23 @@ require 'typhoeus'
3
3
  module Restify
4
4
  module Adapter
5
5
  class Typhoeus < Base
6
- def initialize(**options)
7
- @queue = Queue.new
8
- @hydra = ::Typhoeus::Hydra.new
9
- @options = options
6
+ attr_reader :sync
10
7
 
11
- Thread.new do
12
- begin
13
- loop do
14
- while (req = convert(*@queue.pop))
15
- @hydra.queue req
8
+ DEFAULT_HEADERS = {
9
+ 'Expect' => '',
10
+ 'Transfer-Encoding' => ''
11
+ }.freeze
16
12
 
17
- if @queue.size == 0
18
- @hydra.run
19
- end
20
- end
21
- end
22
- rescue Exception => e
23
- puts "#{self.class}: #{e.message}"
24
- end
25
- end
13
+ def initialize(sync: false, **options)
14
+ @queue = Queue.new
15
+ @sync = sync
16
+ @hydra = ::Typhoeus::Hydra.new(pipelining: true, **options)
17
+
18
+ start unless sync?
26
19
  end
27
20
 
28
21
  def sync?
29
- @options.fetch :sync, false
22
+ @sync
30
23
  end
31
24
 
32
25
  def queue(request, writer)
@@ -48,26 +41,47 @@ module Restify
48
41
  req = ::Typhoeus::Request.new \
49
42
  request.uri,
50
43
  method: request.method,
51
- headers: request.headers,
44
+ headers: DEFAULT_HEADERS.merge(request.headers),
52
45
  body: request.body
53
46
 
54
- req.on_complete do |response|
55
- uri = request.uri
56
- status = response.code
57
- body = response.body
47
+ req.on_complete {|response| handle(response, writer, request) }
48
+ req
49
+ end
58
50
 
59
- headers = response.headers.each_with_object({}) do |header, hash|
60
- hash[header[0].upcase.tr('-', '_')] = header[1]
61
- end
51
+ def handle(native_response, writer, request)
52
+ writer.fulfill convert_back(native_response, request)
62
53
 
63
- writer.fulfill Response.new request, uri, status, headers, body
54
+ @hydra.queue convert(*@queue.pop(true)) while !@queue.empty?
55
+ end
64
56
 
65
- while @queue.size > 0
66
- @hydra.queue convert(*@queue.pop(true))
67
- end
57
+ def convert_back(response, request)
58
+ uri = request.uri
59
+ status = response.code
60
+ body = response.body
61
+ headers = convert_headers(response.headers)
62
+
63
+ ::Restify::Response.new(request, uri, status, headers, body)
64
+ end
65
+
66
+ def convert_headers(headers)
67
+ headers.each_with_object({}) do |header, memo|
68
+ memo[header[0].upcase.tr('-', '_')] = header[1]
68
69
  end
70
+ end
69
71
 
70
- req
72
+ def start
73
+ Thread.new do
74
+ loop do
75
+ begin
76
+ while (req = convert(*@queue.pop))
77
+ @hydra.queue req
78
+ @hydra.run if @queue.empty?
79
+ end
80
+ rescue StandardError => e
81
+ puts "#{self.class}: #{e.message}"
82
+ end
83
+ end
84
+ end
71
85
  end
72
86
  end
73
87
  end
@@ -1,26 +1,19 @@
1
1
  require 'delegate'
2
- require 'forwardable'
3
2
 
4
3
  module Restify
5
4
  #
6
- class Resource < Delegator
7
- extend Forwardable
5
+ class Resource < SimpleDelegator
8
6
 
9
7
  # @api private
10
8
  #
11
9
  def initialize(context, response: nil, data: nil, relations: {})
12
- @data = data
10
+ super(data)
11
+
13
12
  @context = context
14
13
  @response = response
15
14
  @relations = relations
16
15
  end
17
16
 
18
- # @api private
19
- #
20
- def __getobj__
21
- @data
22
- end
23
-
24
17
  # Check if resource has a relation with given name.
25
18
  #
26
19
  # @param name [String, Symbol] Relation name.
@@ -55,7 +48,7 @@ module Restify
55
48
  #
56
49
  # @return [Object] Response data.
57
50
  #
58
- attr_reader :data
51
+ alias data __getobj__
59
52
 
60
53
  # @!method response
61
54
  #
@@ -104,7 +97,7 @@ module Restify
104
97
  # @api private
105
98
  def inspect
106
99
  text = {
107
- "@data" => @data,
100
+ "@data" => data,
108
101
  "@relations" => @relations
109
102
  }.map {|k,v | k + '=' + v.inspect }.join(' ')
110
103
 
@@ -2,8 +2,8 @@ module Restify
2
2
  module VERSION
3
3
  MAJOR = 1
4
4
  MINOR = 0
5
- PATCH = 0
6
- STAGE = nil
5
+ PATCH = 1
6
+ STAGE = :beta1
7
7
  STRING = [MAJOR, MINOR, PATCH, STAGE].reject(&:nil?).join('.').freeze
8
8
 
9
9
  def self.to_s
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restify
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Graichen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-22 00:00:00.000000000 Z
11
+ date: 2017-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -176,9 +176,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
176
176
  version: '0'
177
177
  required_rubygems_version: !ruby/object:Gem::Requirement
178
178
  requirements:
179
- - - ">="
179
+ - - ">"
180
180
  - !ruby/object:Gem::Version
181
- version: '0'
181
+ version: 1.3.1
182
182
  requirements: []
183
183
  rubyforge_project:
184
184
  rubygems_version: 2.5.1