class Facebooker::Service::CurlService

Public Instance Methods

multipart_post_file?(object) click to toggle source

Net::HTTP::MultipartPostFile

# File lib/facebooker/service/curl_service.rb, line 18
def multipart_post_file?(object)
  object.respond_to?(:content_type) &&
  object.respond_to?(:data) &&
  object.respond_to?(:filename)
end
post_form(url,params,multipart=false) click to toggle source
# File lib/facebooker/service/curl_service.rb, line 4
def post_form(url,params,multipart=false)
  curl = Curl::Easy.new(url.to_s) do |c|
    c.multipart_form_post = multipart
    c.timeout = Facebooker.timeout
  end
  curl.http_post(*to_curb_params(params)) 
  curl.body_str
end
post_multipart_form(url,params) click to toggle source
# File lib/facebooker/service/curl_service.rb, line 13
def post_multipart_form(url,params)
  post_form(url,params,true)
end
to_curb_params(params) click to toggle source
# File lib/facebooker/service/curl_service.rb, line 24
def to_curb_params(params)
  parray = []
  params.each_pair do |k,v|
    if multipart_post_file?(v)
      # Curl doesn't like blank field names
      field = Curl::PostField.file((k.blank? ? 'xxx' : k.to_s), nil, File.basename(v.filename))
      field.content_type = v.content_type
      field.content = v.data
      parray << field
    else
      parray << Curl::PostField.content(
        k.to_s,
        (Array === v || Hash===v) ? Facebooker.json_encode(v) : v.to_s
      )
    end
  end
  parray
end