Sha256: 44d6311957b0b1a1be9f54e99b091258e5ca4fa3c76af21545545f6427851376

Contents?: true

Size: 1.27 KB

Versions: 12

Compression:

Stored size: 1.27 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, body)
        end

        def put_uri(uri, body, as)
          do_request(Net::HTTP::Put, uri, as, body)
        end

        def patch_uri(uri, body, as)
          do_request(Net::HTTP::Patch, uri, as, body)
        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["accept"]     = as  # TODO: test me. # DISCUSS: if Accept is not set, rails treats this request as as "text/html".
          req.body          = body if body
          http.request(req)
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
roar-0.12.0 lib/roar/representer/transport/net_http.rb
roar-0.11.19 lib/roar/representer/transport/net_http.rb
roar-0.11.18 lib/roar/representer/transport/net_http.rb
roar-0.11.17 lib/roar/representer/transport/net_http.rb
roar-0.11.16 lib/roar/representer/transport/net_http.rb
roar-0.11.15 lib/roar/representer/transport/net_http.rb
roar-0.11.14 lib/roar/representer/transport/net_http.rb
roar-0.11.13 lib/roar/representer/transport/net_http.rb
roar-0.11.12 lib/roar/representer/transport/net_http.rb
roar-0.11.11 lib/roar/representer/transport/net_http.rb
roar-0.11.10 lib/roar/representer/transport/net_http.rb
roar-0.11.9 lib/roar/representer/transport/net_http.rb