Sha256: 2d85a402be937bdf6d8f58364b9d373cae93dd1e6142a34c117a98b39a418312

Contents?: true

Size: 1.83 KB

Versions: 4

Compression:

Stored size: 1.83 KB

Contents

require 'roar/transport/net_http'

module Roar
  # Gives HTTP-power to representers. They can serialize, send, process and deserialize HTTP-requests.
  module HttpVerbs

    class << self
      attr_accessor :transport_engine

      def included(base)
        base.extend ClassMethods
      end
    end
    self.transport_engine = ::Roar::Transport::NetHTTP


    module ClassMethods
      # GETs +url+ with +format+ and returns deserialized represented object.
      def get(*args)
        new.get(*args)
      end
    end


    attr_writer :transport_engine
    def transport_engine
      @transport_engine || HttpVerbs.transport_engine
    end

    # Serializes the object, POSTs it to +url+ with +format+, deserializes the returned document
    # and updates properties accordingly.
    def post(options={}, &block)
      response = http.post_uri(options.merge(:body => serialize), &block)
      handle_response(response)
    end

    # GETs +url+ with +format+, deserializes the returned document and updates properties accordingly.
    def get(options={}, &block)
      response = http.get_uri(options, &block)
      handle_response(response)
    end

    # Serializes the object, PUTs it to +url+ with +format+, deserializes the returned document
    # and updates properties accordingly.
    def put(options={}, &block)
      response = http.put_uri(options.merge(:body => serialize), &block)
      handle_response(response)
      self
    end

    def patch(options={}, &block)
      response = http.patch_uri(options.merge(:body => serialize), &block)
      handle_response(response)
      self
    end

    def delete(options, &block)
      http.delete_uri(options, &block)
      self
    end

  private
    def handle_response(response)
      document = response.body
      deserialize(document)
    end

    def http
      transport_engine.new
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/roar-1.2.0/lib/roar/http_verbs.rb
roar-1.2.0 lib/roar/http_verbs.rb
roar-1.1.1 lib/roar/http_verbs.rb
roar-1.1.0 lib/roar/http_verbs.rb