Sha256: 6f6023f36932785864613392f26bc67f32aef690ab8d2415ccc8afb919065f61

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 KB

Contents

require "net/http"
require "uri"

module Roar
  module Representer
    # Implements the (HTTP) transport interface with Net::HTTP.
    module Transport
      # Definitions: every call returns a Response object responding to #body.
      class NetHTTP
        # TODO: generically handle return codes.
        def get_uri(uri, as)
          do_request(Net::HTTP::Get, uri, as)
        end
        
        def post_uri(uri, body, as)
          do_request(Net::HTTP::Post, uri, as)
        end
        
        def put_uri(uri, body, as)
          do_request(Net::HTTP::Put, uri, as)
        end
        
        def patch_uri(uri, body, as)
          do_request(Net::HTTP::Patch, uri, as)
        end
        
        def delete_uri(uri, as)
          do_request(Net::HTTP::Delete, uri, as)
        end
      
      private
        def do_request(what, uri, as, body="")
          # DISCUSS: can this be made easier?
          uri   = URI.parse(uri)
          http  = Net::HTTP.new(uri.host, uri.port)
          req   = what.new(uri.request_uri)
          req.content_type  = as
          req.body          = body if body
          http.request(req)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
roar-0.11.1 lib/roar/representer/transport/net_http.rb
roar-0.11.0 lib/roar/representer/transport/net_http.rb
roar-0.10.2 lib/roar/representer/transport/net_http.rb
roar-0.10.1 lib/roar/representer/transport/net_http.rb