Sha256: d8a2f07f6c459a44703643dfe4b106c465549ba3b951586ca468087dbfd0e7da

Contents?: true

Size: 1.78 KB

Versions: 12

Compression:

Stored size: 1.78 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
        [curl.response_code, 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.http_auth_types = Curl::CURLAUTH_BASIC
          c.username = options[:username]
          c.password = options[:password]
          c.timeout = options[:timeout]
          c.post_body = body
          c.ssl_verify_host = false
          c.ssl_verify_peer = false
          c.proxy_url = options[:proxy]
          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
      # @return [void]
      def perform curl, method, body
        case method
        when :head
          curl.http_head
        when :get
          curl.http_get
        when :post
          curl.http_post(body)
        when :put
          curl.http_put(body)
        when :delete
          curl.http_delete
        end
      end
    end    
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
animoto-1.5.6 ./lib/animoto/http_engines/curl_adapter.rb
animoto-1.5.5 ./lib/animoto/http_engines/curl_adapter.rb
animoto-1.5.4 ./lib/animoto/http_engines/curl_adapter.rb
animoto-1.5.3 ./lib/animoto/http_engines/curl_adapter.rb
animoto-1.5.2 ./lib/animoto/http_engines/curl_adapter.rb
animoto-1.5.1 ./lib/animoto/http_engines/curl_adapter.rb
animoto-1.5.0 ./lib/animoto/http_engines/curl_adapter.rb
animoto-1.3.1 ./lib/animoto/http_engines/curl_adapter.rb
animoto-1.3.0 ./lib/animoto/http_engines/curl_adapter.rb
animoto-1.2.0 ./lib/animoto/http_engines/curl_adapter.rb
animoto-1.1.1 ./lib/animoto/http_engines/curl_adapter.rb
animoto-1.1.0 ./lib/animoto/http_engines/curl_adapter.rb