Sha256: ecaa6124dd4bd65ac9325ad82a464d4b4d63da463783a2c7d8370aa0e239f960

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

require 'net/http'
require 'net/https'
require 'addressable/uri'

require 'pathname'
require Pathname(__FILE__).dirname + 'header'

module Addressable
  class URI
    def absolute_path
      absolute_path = ""
      absolute_path << self.path.to_s
      absolute_path << "?#{self.query}" if self.query != nil
      absolute_path << "##{self.fragment}" if self.fragment != nil
      return absolute_path
    end
  end
end

module Resourceful

  class NetHttpAdapter
    def self.make_request(method, uri, body = nil, header = nil)
      uri = uri.is_a?(Addressable::URI) ? uri : Addressable::URI.parse(uri)

      req = net_http_request_class(method).new(uri.absolute_path)
      header.each { |k,v| req[k] = v } if header
      conn = Net::HTTP.new(uri.host, uri.port)
      conn.use_ssl = (/https/i === uri.scheme)
      begin 
        conn.start
        res = conn.request(req, body)
      ensure
        conn.finish
      end

      [ Integer(res.code),
        Resourceful::Header.new(res.header.to_hash),
        res.body
      ]
    ensure
      
    end

    private

    def self.net_http_request_class(method)
      case method
      when :get     then Net::HTTP::Get
      when :head    then Net::HTTP::Head
      when :post    then Net::HTTP::Post
      when :put     then Net::HTTP::Put
      when :delete  then Net::HTTP::Delete
      end

    end

  end

end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
paul-resourceful-0.2.3 lib/resourceful/net_http_adapter.rb
paul-resourceful-0.3.0 lib/resourceful/net_http_adapter.rb
paul-resourceful-0.3.1 lib/resourceful/net_http_adapter.rb
resourceful-0.3.1 lib/resourceful/net_http_adapter.rb
resourceful-0.3.0 lib/resourceful/net_http_adapter.rb