streamly_ffi 0.2.4 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,15 +1,16 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- streamly_ffi (0.2.4)
4
+ streamly_ffi (0.2.6)
5
5
  curl_ffi
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
9
9
  specs:
10
- curl_ffi (0.0.6)
10
+ curl_ffi (0.0.8)
11
11
  ffi
12
12
  diff-lcs (1.1.2)
13
+ fakeweb (1.3.0)
13
14
  ffi (0.6.3)
14
15
  rake (>= 0.8.7)
15
16
  rake (0.8.7)
@@ -27,6 +28,7 @@ PLATFORMS
27
28
 
28
29
  DEPENDENCIES
29
30
  curl_ffi
31
+ fakeweb
30
32
  rspec
31
33
  rspec-core
32
34
  streamly_ffi!
data/Rakefile CHANGED
@@ -26,7 +26,7 @@ require 'rspec/core'
26
26
  require 'rspec/core/rake_task'
27
27
 
28
28
  desc "Run all examples with RCov"
29
- Rspec::Core::RakeTask.new('spec:rcov') do |t|
29
+ RSpec::Core::RakeTask.new('spec:rcov') do |t|
30
30
  #Spec::Rake::SpecTask.new('spec:rcov') do |t|
31
31
  t.pattern = "spec/requests/**/*_spec.rb"
32
32
  t.rcov = true
@@ -36,7 +36,7 @@ Rspec::Core::RakeTask.new('spec:rcov') do |t|
36
36
  }.flatten
37
37
  end
38
38
  end
39
- Rspec::Core::RakeTask.new(:spec) do |t|
39
+ RSpec::Core::RakeTask.new(:spec) do |t|
40
40
  #Spec::Rake::SpecTask.new('spec') do |t|
41
41
  t.pattern = "spec/streamly_ffi/**/*_spec.rb"
42
42
  # t.opts << '--options' << 'spec/spec.opts'
@@ -5,10 +5,14 @@ $:.unshift(File.dirname(__FILE__)) unless
5
5
  require 'streamly_ffi/version'
6
6
 
7
7
  module StreamlyFFI
8
- autoload :Base, "streamly_ffi/base"
9
- autoload :Connection, "streamly_ffi/connection"
10
- autoload :Request, "streamly_ffi/request"
11
- autoload :PersistentRequest, "streamly_ffi/persistent_request"
8
+ # autoload :Base, "streamly_ffi/base"
9
+ require "streamly_ffi/base"
10
+ require "streamly_ffi/connection"
11
+ require "streamly_ffi/request"
12
+ require "streamly_ffi/persistent_request"
13
+ # autoload :Connection, "streamly_ffi/connection"
14
+ # autoload :Request, "streamly_ffi/request"
15
+ # autoload :PersistentRequest, "streamly_ffi/persistent_request"
12
16
 
13
17
  class Error < StandardError; end
14
18
  class UnsupportedProtocol < StandardError; end
@@ -4,13 +4,14 @@ module StreamlyFFI
4
4
 
5
5
  attr_accessor :url, :method, :default_write_handler, :default_header_handler
6
6
 
7
+ DEFAULT_CURL_ENCODING = "identity, deflate, gzip"
8
+
7
9
  def execute(options={})
10
+ connection.reset
8
11
  set_options(options).perform
9
12
 
10
13
  CurlFFI.slist_free_all(@request_headers) if @request_headers
11
14
 
12
- connection.reset
13
-
14
15
  resp = if(options.has_key?(:response_header_handler) or options.has_key?(:response_body_handler))
15
16
  nil
16
17
  elsif(options[:method] == :head && response_header.respond_to?(:to_str))
@@ -28,10 +29,29 @@ module StreamlyFFI
28
29
  connection.perform
29
30
  end
30
31
 
32
+ # Set/add request headers in Curl's slist
33
+ # @param [Hash, Array<Hash>, Set<Hash>] headers Any number of headers to be added to curl's slist. Ultimate form
34
+ # must be a Hash; multiple Hashes with the same key (and different value) can be placed in an Array, if desired.
35
+ def set_headers(headers)
36
+ case headers
37
+ when Hash
38
+ headers.each_pair do |k_v|
39
+ self.request_headers = CurlFFI.slist_append(self.request_headers, k_v.join(": "))
40
+ end
41
+ when Array, Set
42
+ headers.each do |header|
43
+ header.each_pair do |k_v|
44
+ self.request_headers = CurlFFI.slist_append(self.request_headers, k_v.join(": "))
45
+ end
46
+ end
47
+ end
48
+ return self.request_headers
49
+ end # def set_headers
50
+
31
51
  def set_options(options={})
32
- @url = options[:url] if options.has_key?(:url) # Make sure @url is set, if not
33
- @method = options[:method] if options.has_key?(:method) # Make sure @method is set, if not
34
- @payload = options[:payload]
52
+ @url = options[:url].dup if options.has_key?(:url) # Make sure @url is set, if not
53
+ @method = options[:method] if options.has_key?(:method) # Make sure @method is set, if not
54
+ @payload = options[:payload].dup if options.has_key?(:payload)
35
55
 
36
56
  @response_body = nil
37
57
  @response_header = nil
@@ -56,17 +76,15 @@ module StreamlyFFI
56
76
  # else I WILL CUT YOU
57
77
  end
58
78
 
59
- if options.has_key?(:headers) and not options[:headers].nil?
60
- options[:headers].each_pair do |key_and_value|
61
- self.request_headers = CurlFFI.slist_append(self.request_headers, key_and_value.join(": "))
62
- end
63
- connection.setopt :HTTPHEADER, @request_headers
79
+ if options.has_key?(:headers)
80
+ connection.setopt(:HTTPHEADER, set_headers(options[:headers])) unless options[:headers].nil?
64
81
  end
65
82
 
66
83
  if options.has_key?(:response_header_handler)
67
84
  @custom_header_handler = options[:response_header_handler]
68
85
  set_header_handler(:custom_header_callback)
69
86
  else
87
+ @response_header = ""
70
88
  set_header_handler
71
89
  end
72
90
 
@@ -74,10 +92,11 @@ module StreamlyFFI
74
92
  @custom_write_handler = options[:response_body_handler]
75
93
  set_write_handler(:custom_write_callback)
76
94
  else
95
+ @response_body = ""
77
96
  set_write_handler
78
97
  end
79
98
 
80
- connection.setopt :ENCODING, "identity, deflate, gzip" unless @method == :head
99
+ connection.setopt :ENCODING, DEFAULT_CURL_ENCODING unless @method == :head
81
100
  connection.setopt :URL, @url
82
101
 
83
102
  # Other common options (blame streamly guy)
@@ -102,7 +121,7 @@ module StreamlyFFI
102
121
  alias :error :error_buffer
103
122
 
104
123
  def request_headers
105
- @request_headers ||= FFI::MemoryPointer.from_string("")
124
+ @request_headers ||= FFI::MemoryPointer.new(:pointer)
106
125
  end
107
126
 
108
127
  def response_body
@@ -117,37 +136,42 @@ module StreamlyFFI
117
136
  alias :headers :response_header
118
137
 
119
138
  def set_write_handler(_callback=:default_write_callback)
120
- connection.setopt(:WRITEFUNCTION, FFI::Function.new(:size_t, [:pointer, :size_t, :size_t,], &self.__method__(_callback)))
139
+ @_write_handler = FFI::Function.new(:size_t, [:pointer, :size_t, :size_t,], &self.__method__(_callback))
140
+
141
+ connection.setopt(:WRITEFUNCTION, @_write_handler)
121
142
  end
122
143
 
123
144
  def set_header_handler(_callback=:default_header_callback)
124
- connection.setopt(:HEADERFUNCTION, FFI::Function.new(:size_t, [:pointer, :size_t, :size_t], &self.__method__(_callback)))
145
+ @_header_handler = FFI::Function.new(:size_t, [:pointer, :size_t, :size_t], &self.__method__(_callback))
146
+ connection.setopt(:HEADERFUNCTION, @_header_handler)
125
147
  end
126
148
 
127
149
  def default_write_callback(string_ptr, size, nmemb)
128
- length = size * nmemb
129
- response_body << string_ptr.read_string(length)
150
+ length = size * nmemb
151
+ @response_body << string_ptr.read_string(length).dup
130
152
 
131
153
  return length
132
154
  end
133
155
 
134
156
  def default_header_callback(string_ptr, size, nmemb)
135
- length = size * nmemb
136
- response_header << string_ptr.read_string(length)
157
+ length = size * nmemb
158
+ @response_header << string_ptr.read_string(length).dup
137
159
 
138
160
  return length
139
161
  end
140
162
 
141
163
  def custom_write_callback(string_ptr, size, nmemb)
142
- length = size * nmemb
143
- @custom_write_handler.call(string_ptr.read_string(length))
164
+ length = size * nmemb
165
+ @_wcall = string_ptr.read_string(length)
166
+ @custom_write_handler.call(string_ptr.read_string(length).dup)
144
167
 
145
168
  return length
146
169
  end
147
170
 
148
171
  def custom_header_callback(string_ptr, size, nmemb)
149
- length = size * nmemb
150
- @custom_header_handler.call(string_ptr.read_string(length))
172
+ length = size * nmemb
173
+ @_hcall = string_ptr.read_string(length)
174
+ @custom_header_handler.call(string_ptr.read_string(length).dup)
151
175
 
152
176
  return length
153
177
  end
@@ -8,7 +8,7 @@ module StreamlyFFI
8
8
  include StreamlyFFI::Base
9
9
 
10
10
  def [](_sym)
11
- send _sym
11
+ send _sym
12
12
  end
13
13
 
14
14
  def escape(_string)
@@ -1,3 +1,3 @@
1
1
  module StreamlyFFI
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.6"
3
3
  end
@@ -4,4 +4,5 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
4
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
5
 
6
6
  require 'streamly_ffi'
7
+ require 'fakeweb'
7
8
  require 'rspec'
@@ -8,27 +8,22 @@ describe StreamlyFFI::Request do
8
8
  end
9
9
 
10
10
  describe "HEAD" do
11
+ before :each do
12
+ @response = StreamlyFFI.head('http://localhost/')
13
+ end
14
+
11
15
  describe "basic" do
12
16
  it "should perform a basic request" do
13
- resp = StreamlyFFI.head('localhost:4567')
14
- resp.should_not be_nil
17
+ @response.should_not be_nil
15
18
  end
16
- =begin
17
- if RUBY_VERSION =~ /^1.9/
18
- it "should default to utf-8 if Encoding.default_internal is nil" do
19
- Encoding.default_internal = nil
20
- StreamlyFFI.head('localhost:4567').encoding.should eql(Encoding.find('utf-8'))
21
- end
22
19
 
23
- it "should use Encoding.default_internal" do
24
- Encoding.default_internal = Encoding.find('utf-8')
25
- StreamlyFFI.head('localhost:4567').encoding.should eql(Encoding.default_internal)
26
- Encoding.default_internal = Encoding.find('us-ascii')
27
- StreamlyFFI.head('localhost:4567').encoding.should eql(Encoding.default_internal)
28
- end
20
+ it "should contain common header data" do
21
+ (@response =~ /HTTP.+200 OK/).should_not be_nil
22
+ (@response =~ /Date/).should_not be_nil
23
+ (@response =~ /Content-Type/).should_not be_nil
24
+ (@response =~ /Hullo/).should be_nil
29
25
  end
30
- =end
31
- end
26
+ end # describe "basic"
32
27
 
33
28
  describe "streaming" do
34
29
  it "should perform a basic request and stream header chunks to the caller" do
@@ -40,29 +35,8 @@ describe StreamlyFFI::Request do
40
35
  resp.should be_nil
41
36
  streamed_response.should_not be_nil
42
37
  end
43
- =begin
44
- if RUBY_VERSION =~ /^1.9/
45
- it "should default to utf-8 if Encoding.default_internal is nil" do
46
- Encoding.default_internal = nil
47
- StreamlyFFI.head('localhost:4567') do |chunk|
48
- chunk.encoding.should eql(Encoding.find('utf-8'))
49
- end
50
- end
51
-
52
- it "should use Encoding.default_internal" do
53
- Encoding.default_internal = Encoding.find('utf-8')
54
- StreamlyFFI.head('localhost:4567') do |chunk|
55
- chunk.encoding.should eql(Encoding.default_internal)
56
- end
57
- Encoding.default_internal = Encoding.find('us-ascii')
58
- StreamlyFFI.head('localhost:4567') do |chunk|
59
- chunk.encoding.should eql(Encoding.default_internal)
60
- end
61
- end
62
- end
63
- =end
64
- end
65
- end
38
+ end # describe "streaming"
39
+ end # describe "HEAD"
66
40
 
67
41
  describe "GET" do
68
42
  describe "basic" do
@@ -70,21 +44,6 @@ describe StreamlyFFI::Request do
70
44
  resp = StreamlyFFI.get('localhost:4567/?name=brian')
71
45
  resp.should eql(@response)
72
46
  end
73
- =begin
74
- if RUBY_VERSION =~ /^1.9/
75
- it "should default to utf-8 if Encoding.default_internal is nil" do
76
- Encoding.default_internal = nil
77
- StreamlyFFI.get('localhost:4567').encoding.should eql(Encoding.find('utf-8'))
78
- end
79
-
80
- it "should use Encoding.default_internal" do
81
- Encoding.default_internal = Encoding.find('utf-8')
82
- StreamlyFFI.get('localhost:4567').encoding.should eql(Encoding.default_internal)
83
- Encoding.default_internal = Encoding.find('us-ascii')
84
- StreamlyFFI.get('localhost:4567').encoding.should eql(Encoding.default_internal)
85
- end
86
- end
87
- =end
88
47
  end
89
48
 
90
49
  describe "streaming" do
@@ -97,27 +56,6 @@ describe StreamlyFFI::Request do
97
56
  resp.should be_nil
98
57
  streamed_response.should eql(@response)
99
58
  end
100
- =begin
101
- if RUBY_VERSION =~ /^1.9/
102
- it "should default to utf-8 if Encoding.default_internal is nil" do
103
- Encoding.default_internal = nil
104
- StreamlyFFI.get('localhost:4567') do |chunk|
105
- chunk.encoding.should eql(Encoding.find('utf-8'))
106
- end
107
- end
108
-
109
- it "should use Encoding.default_internal" do
110
- Encoding.default_internal = Encoding.find('utf-8')
111
- StreamlyFFI.get('localhost:4567') do |chunk|
112
- chunk.encoding.should eql(Encoding.default_internal)
113
- end
114
- Encoding.default_internal = Encoding.find('us-ascii')
115
- StreamlyFFI.get('localhost:4567') do |chunk|
116
- chunk.encoding.should eql(Encoding.default_internal)
117
- end
118
- end
119
- end
120
- =end
121
59
  end
122
60
  end
123
61
 
@@ -127,21 +65,6 @@ describe StreamlyFFI::Request do
127
65
  resp = StreamlyFFI.post('localhost:4567', 'name=brian')
128
66
  resp.should eql(@response)
129
67
  end
130
- =begin
131
- if RUBY_VERSION =~ /^1.9/
132
- it "should default to utf-8 if Encoding.default_internal is nil" do
133
- Encoding.default_internal = nil
134
- StreamlyFFI.post('localhost:4567', 'name=brian').encoding.should eql(Encoding.find('utf-8'))
135
- end
136
-
137
- it "should use Encoding.default_internal" do
138
- Encoding.default_internal = Encoding.find('utf-8')
139
- StreamlyFFI.post('localhost:4567', 'name=brian').encoding.should eql(Encoding.default_internal)
140
- Encoding.default_internal = Encoding.find('us-ascii')
141
- StreamlyFFI.post('localhost:4567', 'name=brian').encoding.should eql(Encoding.default_internal)
142
- end
143
- end
144
- =end
145
68
  end
146
69
 
147
70
  describe "streaming" do
@@ -154,27 +77,6 @@ describe StreamlyFFI::Request do
154
77
  resp.should be_nil
155
78
  streamed_response.should eql(@response)
156
79
  end
157
- =begin
158
- if RUBY_VERSION =~ /^1.9/
159
- it "should default to utf-8 if Encoding.default_internal is nil" do
160
- Encoding.default_internal = nil
161
- StreamlyFFI.post('localhost:4567', 'name=brian') do |chunk|
162
- chunk.encoding.should eql(Encoding.find('utf-8'))
163
- end
164
- end
165
-
166
- it "should use Encoding.default_internal" do
167
- Encoding.default_internal = Encoding.find('utf-8')
168
- StreamlyFFI.post('localhost:4567', 'name=brian') do |chunk|
169
- chunk.encoding.should eql(Encoding.default_internal)
170
- end
171
- Encoding.default_internal = Encoding.find('us-ascii')
172
- StreamlyFFI.post('localhost:4567', 'name=brian') do |chunk|
173
- chunk.encoding.should eql(Encoding.default_internal)
174
- end
175
- end
176
- end
177
- =end
178
80
  end
179
81
  end
180
82
 
@@ -184,21 +86,6 @@ describe StreamlyFFI::Request do
184
86
  resp = StreamlyFFI.put('localhost:4567', 'name=brian')
185
87
  resp.should eql(@response)
186
88
  end
187
- =begin
188
- if RUBY_VERSION =~ /^1.9/
189
- it "should default to utf-8 if Encoding.default_internal is nil" do
190
- Encoding.default_internal = nil
191
- StreamlyFFI.put('localhost:4567', 'name=brian').encoding.should eql(Encoding.find('utf-8'))
192
- end
193
-
194
- it "should use Encoding.default_internal" do
195
- Encoding.default_internal = Encoding.find('utf-8')
196
- StreamlyFFI.put('localhost:4567', 'name=brian').encoding.should eql(Encoding.default_internal)
197
- Encoding.default_internal = Encoding.find('us-ascii')
198
- StreamlyFFI.put('localhost:4567', 'name=brian').encoding.should eql(Encoding.default_internal)
199
- end
200
- end
201
- =end
202
89
  end
203
90
 
204
91
  describe "streaming" do
@@ -211,27 +98,6 @@ describe StreamlyFFI::Request do
211
98
  resp.should be_nil
212
99
  streamed_response.should eql(@response)
213
100
  end
214
- =begin
215
- if RUBY_VERSION =~ /^1.9/
216
- it "should default to utf-8 if Encoding.default_internal is nil" do
217
- Encoding.default_internal = nil
218
- StreamlyFFI.put('localhost:4567', 'name=brian') do |chunk|
219
- chunk.encoding.should eql(Encoding.find('utf-8'))
220
- end
221
- end
222
-
223
- it "should use Encoding.default_internal" do
224
- Encoding.default_internal = Encoding.find('utf-8')
225
- StreamlyFFI.put('localhost:4567', 'name=brian') do |chunk|
226
- chunk.encoding.should eql(Encoding.default_internal)
227
- end
228
- Encoding.default_internal = Encoding.find('us-ascii')
229
- StreamlyFFI.put('localhost:4567', 'name=brian') do |chunk|
230
- chunk.encoding.should eql(Encoding.default_internal)
231
- end
232
- end
233
- end
234
- =end
235
101
  end
236
102
  end
237
103
 
@@ -240,21 +106,6 @@ describe StreamlyFFI::Request do
240
106
  it "should perform a basic request" do
241
107
  resp = StreamlyFFI.delete('localhost:4567/?name=brian').should eql(@response)
242
108
  end
243
- =begin
244
- if RUBY_VERSION =~ /^1.9/
245
- it "should default to utf-8 if Encoding.default_internal is nil" do
246
- Encoding.default_internal = nil
247
- StreamlyFFI.delete('localhost:4567/?name=brian').encoding.should eql(Encoding.find('utf-8'))
248
- end
249
-
250
- it "should use Encoding.default_internal" do
251
- Encoding.default_internal = Encoding.find('utf-8')
252
- StreamlyFFI.delete('localhost:4567/?name=brian').encoding.should eql(Encoding.default_internal)
253
- Encoding.default_internal = Encoding.find('us-ascii')
254
- StreamlyFFI.delete('localhost:4567/?name=brian').encoding.should eql(Encoding.default_internal)
255
- end
256
- end
257
- =end
258
109
  end
259
110
 
260
111
  describe "streaming" do
@@ -267,27 +118,6 @@ describe StreamlyFFI::Request do
267
118
  resp.should be_nil
268
119
  streamed_response.should eql(@response)
269
120
  end
270
- =begin
271
- if RUBY_VERSION =~ /^1.9/
272
- it "should default to utf-8 if Encoding.default_internal is nil" do
273
- Encoding.default_internal = nil
274
- StreamlyFFI.delete('localhost:4567/?name=brian') do |chunk|
275
- chunk.encoding.should eql(Encoding.find('utf-8'))
276
- end
277
- end
278
-
279
- it "should use Encoding.default_internal" do
280
- Encoding.default_internal = Encoding.find('utf-8')
281
- StreamlyFFI.delete('localhost:4567/?name=brian') do |chunk|
282
- chunk.encoding.should eql(Encoding.default_internal)
283
- end
284
- Encoding.default_internal = Encoding.find('us-ascii')
285
- StreamlyFFI.delete('localhost:4567/?name=brian') do |chunk|
286
- chunk.encoding.should eql(Encoding.default_internal)
287
- end
288
- end
289
- end
290
- =end
291
121
  end
292
122
  end
293
123
  end
@@ -18,8 +18,8 @@ Gem::Specification.new do |s|
18
18
  s.email = %q{seniorlopez@gmail.com}
19
19
  s.homepage = %q{http://github.com/aitrus/streamly_ffi}
20
20
 
21
- s.add_dependency "curl_ffi"
22
- s.add_development_dependency "rspec"
21
+ s.add_dependency "curl_ffi"
22
+ s.add_development_dependency "rspec"
23
23
 
24
24
  s.files = `git ls-files`.split("\n")
25
25
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 4
9
- version: 0.2.4
8
+ - 6
9
+ version: 0.2.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brian Lopez
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-10 00:00:00 -08:00
18
+ date: 2010-11-23 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency