Sha256: a90474a377eec64aabbdaa9d706e84937c2cfa81df9e0bcde904d68ce64ddd0d

Contents?: true

Size: 1.73 KB

Versions: 6

Compression:

Stored size: 1.73 KB

Contents

require 'net/http'
require 'net/https'

module Animoto
  class HTTPEngine
    class NetHTTPAdapter < Animoto::HTTPEngine
      
      HTTP_METHOD_MAP   = {
        :get  => Net::HTTP::Get,
        :post => Net::HTTP::Post
      }
      
      def request method, url, body = nil, headers = {}, options = {}
        uri = URI.parse(url)
        http = build_http uri
        req = build_request method, uri, body, headers, options        
        response = http.request req
        check_response response.code.to_i, response.body
        response.body
      end
      
      private

      # Makes a new HTTP object.
      #
      # @param [URI] uri a URI object of the request URL
      # @return [Net::HTTP] the HTTP object
      def build_http uri
        http = Net::HTTP.new uri.host, uri.port
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
        http
      end
      
      # Builds the request object.
      #
      # @param [Symbol] method which HTTP method to use (should be lowercase, i.e. :get instead of :GET)
      # @param [String] uri the request path
      # @param [String, nil] body the request body
      # @param [Hash<String,String>] headers the request headers (will be sent as-is, which means you should
      #   specify "Content-Type" => "..." instead of, say, :content_type => "...")
      # @param [Hash] options
      # @return [Net::HTTPRequest] the request object
      def build_request method, uri, body, headers, options
        req = HTTP_METHOD_MAP[method].new uri.path
        req.body = body
        req.initialize_http_header headers
        req.basic_auth options[:username], options[:password]
        req
      end      
    end
    
    adapter_map.merge! :net_http => NetHTTPAdapter
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
animoto-0.0.0.alpha9 ./lib/animoto/http_engines/net_http_adapter.rb
animoto-0.0.0.alpha8 ./lib/animoto/http_engines/net_http_adapter.rb
animoto-0.0.0.alpha7 ./lib/animoto/http_engines/net_http_adapter.rb
animoto-0.0.0.alpha6 ./lib/animoto/http_engines/net_http_adapter.rb
animoto-0.0.0.alpha5 ./lib/animoto/http_engines/net_http_adapter.rb
animoto-0.0.0.alpha4 ./lib/animoto/http_engines/net_http_adapter.rb