juozasg-curl-multi 0.2 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/curl-multi.rb +31 -1
  2. metadata +1 -1
data/lib/curl-multi.rb CHANGED
@@ -83,6 +83,8 @@ module Curl
83
83
  end
84
84
 
85
85
  class Multi
86
+ attr_accessor :user_agent, :follow_location, :max_redirects
87
+
86
88
  def size() @handles.size end
87
89
 
88
90
  def inspect() '{Curl::Multi' + @handles.map{|h| ' '+h.url}.join + '}' end
@@ -136,6 +138,9 @@ module Curl
136
138
 
137
139
  def initialize
138
140
  @handles = []
141
+ @user_agent = ""
142
+ @follow_location = 0
143
+ @max_redirects = -1
139
144
  end
140
145
 
141
146
  # Add a URL to the queue of items to fetch
@@ -185,6 +190,7 @@ module Curl
185
190
  #define CHECKN(e) CHECK(!(e))
186
191
 
187
192
  ID id_initialize, id_done, id_add_chunk, id_size;
193
+ ID id_user_agent, id_follow_location, id_max_redirects;
188
194
  end
189
195
 
190
196
  builder.c_raw_singleton <<-end
@@ -205,6 +211,10 @@ module Curl
205
211
  id_done = rb_intern("done");
206
212
  id_add_chunk = rb_intern("add_chunk");
207
213
  id_size = rb_intern("size");
214
+
215
+ id_user_agent = rb_intern("user_agent");
216
+ id_follow_location = rb_intern("follow_location");
217
+ id_max_redirects = rb_intern("max_redirects");
208
218
 
209
219
  /* must be called at least once before any other curl functions */
210
220
  r = curl_global_init(CURL_GLOBAL_ALL);
@@ -239,12 +249,32 @@ module Curl
239
249
  /* We start getting errors if we have too many open connections at
240
250
  once, so make a hard limit. */
241
251
  if (FIX2INT(rb_funcall(self, id_size, 0)) > 500) return Qnil;
242
-
252
+
253
+ /* get the setopt options */
254
+ VALUE user_agent = rb_funcall(self, id_user_agent, 0);
255
+ VALUE follow_location = rb_funcall(self, id_follow_location, 0);
256
+ VALUE max_redirects = rb_funcall(self, id_max_redirects, 0);
257
+
258
+ /* check types */
259
+ Check_Type(user_agent, T_STRING);
260
+ Check_Type(follow_location, T_FIXNUM);
261
+ Check_Type(max_redirects, T_FIXNUM);
262
+
263
+ /* convert to C types */
264
+ char* c_user_agent = StringValuePtr(user_agent);
265
+ int i_follow_location = FIX2INT(follow_location);
266
+ int i_max_redirects = FIX2INT(max_redirects);
267
+
243
268
  CURL *easy_handle = curl_easy_init();
244
269
  char *c_url = StringValuePtr(url);
245
270
 
246
271
  /* Pass it the URL */
247
272
  curl_easy_setopt(easy_handle, CURLOPT_URL, c_url);
273
+
274
+ /* Set user options */
275
+ curl_easy_setopt(easy_handle, CURLOPT_FOLLOWLOCATION, i_follow_location);
276
+ curl_easy_setopt(easy_handle, CURLOPT_MAXREDIRS, i_max_redirects);
277
+ curl_easy_setopt(easy_handle, CURLOPT_USERAGENT, c_user_agent);
248
278
 
249
279
  /* GET or POST? */
250
280
  if (body != Qnil) {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: juozasg-curl-multi
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.2"
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristjan Petursson