Update. Object wrapper int examples.

This commit is contained in:
Alexey Melnichuk 2014-09-01 11:57:55 +05:00
parent ef25a2f444
commit fc7a570060

View File

@ -4,8 +4,8 @@
--
-- local e = cURL.easy{
-- url = 'http://example.com',
-- write = io.stdout;
-- header = function(msg) io.stderr:write(msg) end;
-- writefunction = io.stdout;
-- headerfunction = function(msg) io.stderr:write(msg) end;
-- }
-- e:perform()
--
@ -14,16 +14,8 @@ local curl = require "lcurl.safe"
local easy = {} do
local LCURL_ERROR_EASY = 1
local CURLE_UNKNOWN_OPTION = 48
local RENAME_OPT = {
read = "readfunction";
write = "writefunction";
header = "headerfunction";
}
local postfields_storage = setmetatable({}, {__mode = "k"})
local LCURL_ERROR_EASY = curl.ERROR_EASY
local CURLE_UNKNOWN_OPTION = curl.E_UNKNOWN_OPTION
easy.__index = function(self, k)
if k:sub(1, 4) == 'set_' then
@ -43,7 +35,6 @@ easy.__index = function(self, k)
end
local function setopt(handle, k, v, ...)
k = RENAME_OPT[k] or k
local fn = handle["setopt_" .. k]
if not fn then
return nil, curl.error(LCURL_ERROR_EASY, CURLE_UNKNOWN_OPTION)
@ -95,27 +86,15 @@ function easy:set(k, v, ...)
end
function easy:perform(opt)
if opt then
local ok, err = self:set(opt)
if not ok then return nil, err end
end
local e = self:handle()
local oerror = opt.error or function(err) return nil, err end
if opt.read then
local ok, err = e:setopt_readfunction(opt.read)
if not ok then return oerror(err) end
end
if opt.write then
local ok, err = e:setopt_writefunction(opt.write)
if not ok then return oerror(err) end
end
if opt.header then
local ok, err = e:setopt_headerfunction(opt.header)
if not ok then return oerror(err) end
end
local ok, err = e:perform()
if ok then return oerror(err) end
if ok then return nil, err end
return self
end