Module: Ethon::Easies::Options

Included in:
Ethon::Easy
Defined in:
lib/ethon/easies/options.rb

Overview

This module contains the logic and knowledge about the available options on easy.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ethon/easies/options.rb', line 9

def self.included(base)
  base.extend ClassMethods
  base.const_set(:AVAILABLE_OPTIONS, [
    :dns_cache_timeout, :httppost, :put, :httpget, :nobody, :upload,
    :customrequest, :cainfo, :capath, :connecttimeout,
    :forbid_reuse, :followlocation, :httpauth, :infilesize, :interface,
    :maxredirs, :nosignal, :postfieldsize, :copypostfields, :proxy,
    :proxyauth, :proxyport, :proxytype, :timeout, :readdata, :sslcert,
    :ssl_verifypeer, :ssl_verifyhost, :sslcerttype, :sslkey, :sslkeytype,
    :sslversion, :url, :useragent, :userpwd, :verbose, :readfunction
  ])
  base.send(:attr_accessor, *Ethon::Easy::AVAILABLE_OPTIONS)
end

Instance Method Details

#set_optionsObject

Set specified options on easy handle.

Examples:

Set options.

easy.set_options


76
77
78
79
80
# File 'lib/ethon/easies/options.rb', line 76

def set_options
  self.class.available_options.each do |option|
    Curl.set_option(option, value_for(option), handle)
  end
end

#value_for(option) ⇒ Object

Return the value to set to easy handle. It is converted with the help of bool_options, enum_options and int_options.

Examples:

Return casted the value.

easy.value_for(:verbose)

Returns:

  • (Object)

    The casted value.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ethon/easies/options.rb', line 89

def value_for(option)
  value = method(option).call
  return nil if value.nil?

  if self.class.bool_options.include?(option)
    value ? 1 : 0
  elsif self.class.enum_options.key?(option)
    self.class.enum_options[option].to_h.fetch(value) do
      raise Errors::InvalidValue.new(option, value)
    end
  elsif self.class.int_options.include?(option)
    value.to_i
  elsif value.is_a?(::String)
    Util.escape_zero_byte(value)
  else
    value
  end
end