Module: StreamlyFFI::Base

Included in:
PersistentRequest, Request
Defined in:
lib/streamly_ffi/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_header_handlerObject

Returns the value of attribute default_header_handler.



5
6
7
# File 'lib/streamly_ffi/base.rb', line 5

def default_header_handler
  @default_header_handler
end

#default_write_handlerObject

Returns the value of attribute default_write_handler.



5
6
7
# File 'lib/streamly_ffi/base.rb', line 5

def default_write_handler
  @default_write_handler
end

#methodObject Also known as: __method__

Returns the value of attribute method.



5
6
7
# File 'lib/streamly_ffi/base.rb', line 5

def method
  @method
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/streamly_ffi/base.rb', line 5

def url
  @url
end

Instance Method Details

#connectionObject



95
96
97
# File 'lib/streamly_ffi/base.rb', line 95

def connection
  @connection ||= CurlFFI::Easy.new
end

#custom_header_callback(string_ptr, size, nmemb) ⇒ Object



148
149
150
151
152
153
# File 'lib/streamly_ffi/base.rb', line 148

def custom_header_callback(string_ptr, size, nmemb)
  length = size * nmemb
  @custom_header_handler.call(string_ptr.read_string(length))

  return length
end

#custom_write_callback(string_ptr, size, nmemb) ⇒ Object



141
142
143
144
145
146
# File 'lib/streamly_ffi/base.rb', line 141

def custom_write_callback(string_ptr, size, nmemb)
  length = size * nmemb
  @custom_write_handler.call(string_ptr.read_string(length))

  return length
end

#default_header_callback(string_ptr, size, nmemb) ⇒ Object



134
135
136
137
138
139
# File 'lib/streamly_ffi/base.rb', line 134

def default_header_callback(string_ptr, size, nmemb)
  length = size * nmemb
  response_header << string_ptr.read_string(length)

  return length
end

#default_write_callback(string_ptr, size, nmemb) ⇒ Object



127
128
129
130
131
132
# File 'lib/streamly_ffi/base.rb', line 127

def default_write_callback(string_ptr, size, nmemb)
  length = size * nmemb
  response_body << string_ptr.read_string(length)

  return length
end

#error_bufferObject Also known as: error



99
100
101
# File 'lib/streamly_ffi/base.rb', line 99

def error_buffer
  @error_buffer ||= FFI::MemoryPointer.new(:char, CurlFFI::ERROR_SIZE, :clear)
end

#execute(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/streamly_ffi/base.rb', line 7

def execute(options={})
  set_options(options).perform

  CurlFFI.slist_free_all(@request_headers) if @request_headers

  connection.reset

  resp  = if(options.has_key?(:response_header_handler) or options.has_key?(:response_body_handler))
            nil
          elsif(options[:method] == :head && response_header.respond_to?(:to_str))
            response_header
          elsif(response_body.is_a?(String))
            response_body
          else
            nil
          end

  return resp
end

#performObject



27
28
29
# File 'lib/streamly_ffi/base.rb', line 27

def perform
  connection.perform
end

#request_headersObject



104
105
106
# File 'lib/streamly_ffi/base.rb', line 104

def request_headers
  @request_headers ||= FFI::MemoryPointer.from_string("")
end

#response_bodyObject Also known as: response, body



108
109
110
# File 'lib/streamly_ffi/base.rb', line 108

def response_body
  @response_body ||= ""
end

#response_headerObject Also known as: headers



114
115
116
# File 'lib/streamly_ffi/base.rb', line 114

def response_header
  @response_header ||= ""
end

#set_header_handler(_callback = :default_header_callback) ⇒ Object



123
124
125
# File 'lib/streamly_ffi/base.rb', line 123

def set_header_handler(_callback=:default_header_callback)
  connection.setopt(:HEADERFUNCTION, FFI::Function.new(:size_t, [:pointer, :size_t, :size_t], &self.__method__(_callback)))
end

#set_options(options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/streamly_ffi/base.rb', line 31

def set_options(options={})
  @url      = options[:url]     if options.has_key?(:url)     # Make sure @url is set, if not
  @method   = options[:method]  if options.has_key?(:method)  # Make sure @method is set, if not
  @payload  = options[:payload]

  @response_body          = nil
  @response_header        = nil
  @custom_header_handler  = nil
  @custom_write_handler   = nil

  # url should be a string that doesn't suck
  # method should be :post, :get, :put, :delete, :head
  # options should contain any of the following keys:
  #   :headers, :response_header_handler, :response_body_handler, :payload (required if method = :post / :put)

  case @method
  when :get     then  connection.setopt :HTTPGET,        1
  when :head    then  connection.setopt :NOBODY,         1
  when :post    then  connection.setopt :POST,           1
                      connection.setopt :POSTFIELDS,     @payload
                      connection.setopt :POSTFIELDSIZE,  @payload.size
  when :put     then  connection.setopt :CUSTOMREQUEST,  "PUT"
                      connection.setopt :POSTFIELDS,     @payload
                      connection.setopt :POSTFIELDSIZE,  @payload.size
  when :delete  then  connection.setopt :CUSTOMREQUEST,  "DELETE"
  # else I WILL CUT YOU
  end

  if options.has_key?(:headers) and not options[:headers].nil?
    options[:headers].each_pair do |key_and_value|
      self.request_headers = CurlFFI.slist_append(self.request_headers, key_and_value.join(": "))
    end
    connection.setopt :HTTPHEADER, @request_headers
  end

  if options.has_key?(:response_header_handler)
    @custom_header_handler = options[:response_header_handler]
    set_header_handler(:custom_header_callback)
  else
    set_header_handler
  end

  if options.has_key?(:response_body_handler)
    @custom_write_handler = options[:response_body_handler]
    set_write_handler(:custom_write_callback)
  else
    set_write_handler
  end

  connection.setopt :ENCODING,        "identity, deflate, gzip" unless @method == :head
  connection.setopt :URL,             @url

  # Other common options (blame streamly guy)
  connection.setopt :FOLLOWLOCATION,  1
  connection.setopt :MAXREDIRS,       3
  # @TODO: This should be an option
  connection.setopt :SSL_VERIFYPEER,  0
  connection.setopt :SSL_VERIFYHOST,  0

  connection.setopt :ERRORBUFFER,     self.error_buffer

  return self
end

#set_write_handler(_callback = :default_write_callback) ⇒ Object



119
120
121
# File 'lib/streamly_ffi/base.rb', line 119

def set_write_handler(_callback=:default_write_callback)
  connection.setopt(:WRITEFUNCTION, FFI::Function.new(:size_t, [:pointer, :size_t, :size_t,], &self.__method__(_callback)))
end