ethon 0.4.2 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. data/CHANGELOG.md +19 -2
  2. data/Gemfile +20 -1
  3. data/README.md +29 -34
  4. data/lib/ethon/curls/constants.rb +3 -1
  5. data/lib/ethon/curls/functions.rb +36 -29
  6. data/lib/ethon/curls/options.rb +211 -145
  7. data/lib/ethon/easy.rb +747 -102
  8. data/lib/ethon/{easies → easy}/callbacks.rb +1 -1
  9. data/lib/ethon/{easies → easy}/form.rb +6 -6
  10. data/lib/ethon/{easies → easy}/header.rb +1 -1
  11. data/lib/ethon/{easies → easy}/http.rb +16 -10
  12. data/lib/ethon/{easies → easy}/http/actionable.rb +3 -3
  13. data/lib/ethon/{easies → easy}/http/delete.rb +3 -3
  14. data/lib/ethon/{easies → easy}/http/get.rb +3 -3
  15. data/lib/ethon/{easies → easy}/http/head.rb +3 -3
  16. data/lib/ethon/{easies → easy}/http/options.rb +3 -3
  17. data/lib/ethon/{easies → easy}/http/patch.rb +3 -3
  18. data/lib/ethon/{easies → easy}/http/post.rb +3 -3
  19. data/lib/ethon/{easies → easy}/http/postable.rb +1 -1
  20. data/lib/ethon/{easies → easy}/http/put.rb +3 -3
  21. data/lib/ethon/{easies → easy}/http/putable.rb +1 -1
  22. data/lib/ethon/{easies → easy}/informations.rb +1 -1
  23. data/lib/ethon/{easies → easy}/operations.rb +11 -1
  24. data/lib/ethon/{easies → easy}/options.rb +6 -6
  25. data/lib/ethon/{easies → easy}/params.rb +5 -5
  26. data/lib/ethon/{easies → easy}/queryable.rb +1 -1
  27. data/lib/ethon/{easies → easy}/response_callbacks.rb +1 -1
  28. data/lib/ethon/{easies → easy}/util.rb +1 -1
  29. data/lib/ethon/multi.rb +23 -12
  30. data/lib/ethon/{multies → multi}/operations.rb +13 -1
  31. data/lib/ethon/multi/options.rb +86 -0
  32. data/lib/ethon/{multies → multi}/stack.rb +1 -1
  33. data/lib/ethon/version.rb +1 -1
  34. metadata +28 -171
@@ -1,5 +1,5 @@
1
1
  module Ethon
2
- module Easies
2
+ class Easy
3
3
 
4
4
  # This module contains all the logic around the callbacks,
5
5
  # which are needed to interact with libcurl.
@@ -1,15 +1,15 @@
1
- require 'ethon/easies/util'
2
- require 'ethon/easies/queryable'
1
+ require 'ethon/easy/util'
2
+ require 'ethon/easy/queryable'
3
3
 
4
4
  module Ethon
5
- module Easies
5
+ class Easy
6
6
 
7
7
  # This class represents a form and is used to send a payload in the
8
8
  # request body via POST/PUT.
9
9
  # It handles multipart forms, too.
10
10
  class Form
11
- include Ethon::Easies::Util
12
- include Ethon::Easies::Queryable
11
+ include Ethon::Easy::Util
12
+ include Ethon::Easy::Queryable
13
13
 
14
14
  # Return a new Form.
15
15
  #
@@ -92,7 +92,7 @@ module Ethon
92
92
  :form_option, :copyname, :pointer, name,
93
93
  :form_option, :namelength, :long, name.bytesize,
94
94
  :form_option, :copycontents, :pointer, content,
95
- :form_option, :contentslength, :long, content.bytesize,
95
+ :form_option, :contentslength, :long, content ? content.bytesize : 0,
96
96
  :form_option, :end
97
97
  )
98
98
  end
@@ -1,5 +1,5 @@
1
1
  module Ethon
2
- module Easies
2
+ class Easy
3
3
 
4
4
  # This module contains the logic around adding headers to libcurl.
5
5
  module Header
@@ -1,14 +1,14 @@
1
- require 'ethon/easies/http/actionable'
2
- require 'ethon/easies/http/post'
3
- require 'ethon/easies/http/get'
4
- require 'ethon/easies/http/head'
5
- require 'ethon/easies/http/put'
6
- require 'ethon/easies/http/delete'
7
- require 'ethon/easies/http/patch'
8
- require 'ethon/easies/http/options'
1
+ require 'ethon/easy/http/actionable'
2
+ require 'ethon/easy/http/post'
3
+ require 'ethon/easy/http/get'
4
+ require 'ethon/easy/http/head'
5
+ require 'ethon/easy/http/put'
6
+ require 'ethon/easy/http/delete'
7
+ require 'ethon/easy/http/patch'
8
+ require 'ethon/easy/http/options'
9
9
 
10
10
  module Ethon
11
- module Easies
11
+ class Easy
12
12
 
13
13
  # This module contains logic about making valid http requests.
14
14
  module Http
@@ -21,6 +21,12 @@ module Ethon
21
21
  # @param [ String ] url The url.
22
22
  # @param [ String ] action_name The http action name.
23
23
  # @param [ Hash ] options The options hash.
24
+ #
25
+ # @option options :params [ Hash ] Params hash which
26
+ # is attached to the url.
27
+ # @option options :body [ Hash ] Body hash which
28
+ # becomes the request body. It is a PUT body for
29
+ # PUT requests and a POST from for everything else.
24
30
  def http_request(url, action_name, options = {})
25
31
  fabricate(action_name).new(url, options).setup(self)
26
32
  end
@@ -36,7 +42,7 @@ module Ethon
36
42
  #
37
43
  # @return [ Class ] The action class.
38
44
  def fabricate(action_name)
39
- Ethon::Easies::Http.const_get(action_name.to_s.capitalize)
45
+ Ethon::Easy::Http.const_get(action_name.to_s.capitalize)
40
46
  end
41
47
  end
42
48
  end
@@ -1,8 +1,8 @@
1
- require 'ethon/easies/http/putable'
2
- require 'ethon/easies/http/postable'
1
+ require 'ethon/easy/http/putable'
2
+ require 'ethon/easy/http/postable'
3
3
 
4
4
  module Ethon
5
- module Easies
5
+ class Easy
6
6
  module Http
7
7
  # This module represents a Http Action and is a factory
8
8
  # for more real actions like GET, HEAD, POST and PUT.
@@ -1,11 +1,11 @@
1
1
  module Ethon
2
- module Easies
2
+ class Easy
3
3
  module Http
4
4
 
5
5
  # This class knows everything about making DELETE requests.
6
6
  class Delete
7
- include Ethon::Easies::Http::Actionable
8
- include Ethon::Easies::Http::Postable
7
+ include Ethon::Easy::Http::Actionable
8
+ include Ethon::Easy::Http::Postable
9
9
 
10
10
  # Setup easy to make a DELETE request.
11
11
  #
@@ -1,11 +1,11 @@
1
1
  module Ethon
2
- module Easies
2
+ class Easy
3
3
  module Http
4
4
 
5
5
  # This class knows everything about making GET requests.
6
6
  class Get
7
- include Ethon::Easies::Http::Actionable
8
- include Ethon::Easies::Http::Postable
7
+ include Ethon::Easy::Http::Actionable
8
+ include Ethon::Easy::Http::Postable
9
9
 
10
10
  # Setup easy to make a GET request.
11
11
  #
@@ -1,11 +1,11 @@
1
1
  module Ethon
2
- module Easies
2
+ class Easy
3
3
  module Http
4
4
 
5
5
  # This class knows everything about making HEAD requests.
6
6
  class Head
7
- include Ethon::Easies::Http::Actionable
8
- include Ethon::Easies::Http::Postable
7
+ include Ethon::Easy::Http::Actionable
8
+ include Ethon::Easy::Http::Postable
9
9
 
10
10
  # Setup easy to make a HEAD request.
11
11
  #
@@ -1,11 +1,11 @@
1
1
  module Ethon
2
- module Easies
2
+ class Easy
3
3
  module Http
4
4
 
5
5
  # This class knows everything about making OPTIONS requests.
6
6
  class Options
7
- include Ethon::Easies::Http::Actionable
8
- include Ethon::Easies::Http::Postable
7
+ include Ethon::Easy::Http::Actionable
8
+ include Ethon::Easy::Http::Postable
9
9
 
10
10
  # Setup easy to make a OPTIONS request.
11
11
  #
@@ -1,11 +1,11 @@
1
1
  module Ethon
2
- module Easies
2
+ class Easy
3
3
  module Http
4
4
 
5
5
  # This class knows everything about making PATCH requests.
6
6
  class Patch
7
- include Ethon::Easies::Http::Actionable
8
- include Ethon::Easies::Http::Postable
7
+ include Ethon::Easy::Http::Actionable
8
+ include Ethon::Easy::Http::Postable
9
9
 
10
10
  # Setup easy to make a PATCH request.
11
11
  #
@@ -1,10 +1,10 @@
1
1
  module Ethon
2
- module Easies
2
+ class Easy
3
3
  module Http
4
4
  # This class knows everything about making POST requests.
5
5
  class Post
6
- include Ethon::Easies::Http::Actionable
7
- include Ethon::Easies::Http::Postable
6
+ include Ethon::Easy::Http::Actionable
7
+ include Ethon::Easy::Http::Postable
8
8
 
9
9
  # Setup easy to make a POST request.
10
10
  #
@@ -1,5 +1,5 @@
1
1
  module Ethon
2
- module Easies
2
+ class Easy
3
3
  module Http
4
4
 
5
5
  # This module contains logic for setting up a [multipart] POST body.
@@ -1,11 +1,11 @@
1
1
  module Ethon
2
- module Easies
2
+ class Easy
3
3
  module Http
4
4
 
5
5
  # This class knows everything about making PUT requests.
6
6
  class Put
7
- include Ethon::Easies::Http::Actionable
8
- include Ethon::Easies::Http::Putable
7
+ include Ethon::Easy::Http::Actionable
8
+ include Ethon::Easy::Http::Putable
9
9
 
10
10
  # Setup easy to make a PUT request.
11
11
  #
@@ -1,5 +1,5 @@
1
1
  module Ethon
2
- module Easies
2
+ class Easy
3
3
  module Http
4
4
 
5
5
  # This module contains logic about setting up a PUT body.
@@ -1,5 +1,5 @@
1
1
  module Ethon
2
- module Easies
2
+ class Easy
3
3
 
4
4
  # This module contains the methods to return informations
5
5
  # from the easy handle. See http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html
@@ -1,9 +1,19 @@
1
1
  module Ethon
2
- module Easies
2
+ class Easy
3
3
  # This module contains the logic to prepare and perform
4
4
  # an easy.
5
5
  module Operations
6
6
 
7
+ # Returns a pointer to the curl easy handle.
8
+ #
9
+ # @example Return the handle.
10
+ # easy.handle
11
+ #
12
+ # @return [ FFI::Pointer ] A pointer to the curl easy handle.
13
+ def handle
14
+ @handle ||= Curl.easy_init
15
+ end
16
+
7
17
  # Perform the easy request.
8
18
  #
9
19
  # @example Perform the request.
@@ -1,5 +1,5 @@
1
1
  module Ethon
2
- module Easies
2
+ class Easy
3
3
 
4
4
  # This module contains the logic and knowledge about the
5
5
  # available options on easy.
@@ -9,11 +9,11 @@ module Ethon
9
9
  def self.included(base)
10
10
  base.extend ClassMethods
11
11
  base.const_set(:AVAILABLE_OPTIONS, [
12
- :dns_cache_timeout, :httppost, :put, :httpget, :nobody, :upload,
13
- :customrequest, :cainfo, :capath, :connecttimeout,
12
+ :dns_cache_timeout, :httppost, :httpget, :nobody, :upload,
13
+ :customrequest, :cainfo, :capath, :connecttimeout, :connecttimeout_ms,
14
14
  :forbid_reuse, :followlocation, :httpauth, :infilesize, :interface,
15
15
  :maxredirs, :nosignal, :postfieldsize, :copypostfields, :proxy,
16
- :proxyauth, :proxyport, :proxytype, :timeout, :readdata, :sslcert,
16
+ :proxyauth, :proxyport, :proxytype, :timeout, :timeout_ms, :readdata, :sslcert,
17
17
  :ssl_verifypeer, :ssl_verifyhost, :sslcerttype, :sslkey, :sslkeytype,
18
18
  :sslversion, :url, :useragent, :userpwd, :verbose, :readfunction
19
19
  ])
@@ -63,8 +63,8 @@ module Ethon
63
63
  # @return [ Array ] The int options.
64
64
  def int_options
65
65
  [
66
- :connecttimeout, :dns_cache_timeout, :infilesize, :maxredirs,
67
- :postfieldsize, :proxyport, :ssl_verifyhost, :timeout
66
+ :connecttimeout, :connecttimeout_ms, :dns_cache_timeout, :infilesize, :maxredirs,
67
+ :postfieldsize, :proxyport, :ssl_verifyhost, :timeout, :timeout_ms
68
68
  ]
69
69
  end
70
70
  end
@@ -1,13 +1,13 @@
1
- require 'ethon/easies/util'
2
- require 'ethon/easies/queryable'
1
+ require 'ethon/easy/util'
2
+ require 'ethon/easy/queryable'
3
3
 
4
4
  module Ethon
5
- module Easies
5
+ class Easy
6
6
 
7
7
  # This class represents http request parameters.
8
8
  class Params
9
- include Ethon::Easies::Util
10
- include Ethon::Easies::Queryable
9
+ include Ethon::Easy::Util
10
+ include Ethon::Easy::Queryable
11
11
 
12
12
  # Create a new Params.
13
13
  #
@@ -1,5 +1,5 @@
1
1
  module Ethon
2
- module Easies
2
+ class Easy
3
3
 
4
4
  # This module contains logic about building
5
5
  # query parameters for url or form.
@@ -1,5 +1,5 @@
1
1
  module Ethon
2
- module Easies
2
+ class Easy
3
3
 
4
4
  # This module contains the logic for the response callbacks.
5
5
  # The on_complete callback is the only one at the moment.
@@ -1,5 +1,5 @@
1
1
  module Ethon
2
- module Easies # :nodoc:
2
+ class Easy # :nodoc:
3
3
 
4
4
  # This module contains small helpers.
5
5
  module Util
data/lib/ethon/multi.rb CHANGED
@@ -1,12 +1,14 @@
1
- require 'ethon/multies/stack'
2
- require 'ethon/multies/operations'
1
+ require 'ethon/multi/stack'
2
+ require 'ethon/multi/operations'
3
+ require 'ethon/multi/options'
3
4
 
4
5
  module Ethon
5
6
 
6
7
  # This class represents libcurl multi.
7
8
  class Multi
8
- include Ethon::Multies::Stack
9
- include Ethon::Multies::Operations
9
+ include Ethon::Multi::Stack
10
+ include Ethon::Multi::Operations
11
+ include Ethon::Multi::Options
10
12
 
11
13
  class << self
12
14
 
@@ -27,21 +29,30 @@ module Ethon
27
29
  # it didn't happen before.
28
30
  #
29
31
  # @return [ Multi ] The new multi.
30
- def initialize
32
+ def initialize(options = {})
31
33
  Curl.init
32
34
  ObjectSpace.define_finalizer(self, self.class.finalizer(self))
35
+ set_attributes(options)
33
36
  init_vars
34
37
  end
35
38
 
36
- # Return the multi handle. Inititialize multi handle,
37
- # in case it didn't happened already.
39
+ # Set given options.
38
40
  #
39
- # @example Return multi handle.
40
- # multi.handle
41
+ # @example Set options.
42
+ # multi.set_attributes(options)
41
43
  #
42
- # @return [ ::FFI::Pointer ] The multi handle.
43
- def handle
44
- @handle ||= Curl.multi_init
44
+ # @param [ Hash ] options The options.
45
+ #
46
+ # @raise InvalidOption
47
+ #
48
+ # @see initialize
49
+ def set_attributes(options)
50
+ options.each_pair do |key, value|
51
+ unless respond_to?("#{key}=")
52
+ raise Errors::InvalidOption.new(key)
53
+ end
54
+ method("#{key}=").call(value)
55
+ end
45
56
  end
46
57
  end
47
58
  end
@@ -1,9 +1,20 @@
1
1
  module Ethon
2
- module Multies # :nodoc
2
+ class Multi # :nodoc
3
3
 
4
4
  # This module contains logic to run a multi.
5
5
  module Operations
6
6
 
7
+ # Return the multi handle. Inititialize multi handle,
8
+ # in case it didn't happened already.
9
+ #
10
+ # @example Return multi handle.
11
+ # multi.handle
12
+ #
13
+ # @return [ ::FFI::Pointer ] The multi handle.
14
+ def handle
15
+ @handle ||= Curl.multi_init
16
+ end
17
+
7
18
  # Initialize variables.
8
19
  #
9
20
  # @example Initialize variables.
@@ -32,6 +43,7 @@ module Ethon
32
43
  # @example Perform multi.
33
44
  # multi.perform
34
45
  def perform
46
+ set_options
35
47
  Ethon.logger.debug("ETHON: started MULTI") if Ethon.logger
36
48
  while ongoing?
37
49
  run
@@ -0,0 +1,86 @@
1
+ module Ethon
2
+ class Multi
3
+
4
+ # This module contains the logic and knowledge about the
5
+ # available options on multi.
6
+ module Options
7
+
8
+ # :nodoc:
9
+ def self.included(base)
10
+ base.extend ClassMethods
11
+ base.const_set(:AVAILABLE_OPTIONS, [
12
+ :socketfunction, :socketdata, :pipelining,
13
+ :timerfunction, :timerdata, :maxconnects
14
+ ])
15
+ base.send(:attr_accessor, *Ethon::Multi::AVAILABLE_OPTIONS)
16
+ end
17
+
18
+ module ClassMethods # :nodoc:
19
+
20
+ # Return the available options.
21
+ #
22
+ # @example Return the available options.
23
+ # multi.available_options
24
+ #
25
+ # @return [ Array ] The available options.
26
+ def available_options
27
+ Ethon::Multi::AVAILABLE_OPTIONS
28
+ end
29
+
30
+ # Return the options which need to set as 0 or 1 for multi.
31
+ #
32
+ # @example Return the bool options.
33
+ # multi.bool_options
34
+ #
35
+ # @return [ Array ] The bool options.
36
+ def bool_options
37
+ [
38
+ :pipelining
39
+ ]
40
+ end
41
+
42
+ # Return the options which need to set as an integer for multi.
43
+ #
44
+ # @example Return the int options.
45
+ # multi.int_options
46
+ #
47
+ # @return [ Array ] The int options.
48
+ def int_options
49
+ [
50
+ :maxconnects
51
+ ]
52
+ end
53
+ end
54
+
55
+ # Set specified options on multi handle.
56
+ #
57
+ # @example Set options.
58
+ # multi.set_options
59
+ def set_options
60
+ self.class.available_options.each do |option|
61
+ Curl.set_option(option, value_for(option), handle, :multi)
62
+ end
63
+ end
64
+
65
+ # Return the value to set to multi handle. It is converted with the help
66
+ # of bool_options, enum_options and int_options.
67
+ #
68
+ # @example Return casted the value.
69
+ # multi.value_for(:verbose)
70
+ #
71
+ # @return [ Object ] The casted value.
72
+ def value_for(option)
73
+ value = method(option).call
74
+ return nil if value.nil?
75
+
76
+ if self.class.bool_options.include?(option)
77
+ value ? 1 : 0
78
+ elsif self.class.int_options.include?(option)
79
+ value.to_i
80
+ else
81
+ value
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end