Sha256: 5e11452a3f51ba8de58da2bc744c2c256421a8d33640001b12a8a5644aa62bc0

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

require 'curl'

module Animoto
  module HTTPEngines
    class CurlAdapter < Animoto::HTTPEngines::Base
      
      # @return [String]
      def request method, url, body = nil, headers = {}, options = {}
        curl = build_curl method, url, body, headers, options
        perform curl, method, body
        check_response curl.response_code, curl.body_str
        curl.body_str
      end
      
      private
      
      # Creates a Curl::Easy object with the headers, options, body, etc. set.
      #
      # @param [Symbol] method the HTTP method
      # @param [String] url the URL to request
      # @param [String,nil] body the request body
      # @param [Hash<String,String>] headers hash of HTTP request headers
      # @return [Curl::Easy] the Easy instance
      def build_curl method, url, body, headers, options
        ::Curl::Easy.new(url) do |c|
          c.username = options[:username]
          c.password = options[:password]
          c.timeout = options[:timeout]
          c.post_body = body
          headers.each { |header, value| c.headers[header] = value }
        end
      end
      
      # Performs the request.
      #
      # @param [Curl::Easy] curl the Easy object with the request parameters
      # @param [Symbol] method the HTTP method to use
      # @param [String] body the HTTP request body
      def perform curl, method, body
        case method
        when :get
          curl.http_get
        when :post
          curl.http_post(body)
        end
      end
    end
    
    adapter_map.merge! :curl => CurlAdapter
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
animoto-0.1.1.beta1 ./lib/animoto/http_engines/curl_adapter.rb
animoto-0.1.0.beta1 ./lib/animoto/http_engines/curl_adapter.rb
animoto-0.1.0.beta0 ./lib/animoto/http_engines/curl_adapter.rb