Sha256: 1394deb639784853f6905fbe6f61b668801ffa6cc17f631501d1097626d0391e

Contents?: true

Size: 1.45 KB

Versions: 8

Compression:

Stored size: 1.45 KB

Contents

module RestfulResource
  class HttpClient
    class UnprocessableEntity < RuntimeError
      attr_reader :response

      def initialize(response)
        @response = Response.new(body: response.body, headers: response.headers, status: response.code)
      end
    end

    def initialize(authorization: nil)
      @authorization = authorization
    end

    def get(url)
      response = RestClient.get(url, :accept => :json, authorization: @authorization)
      Response.new(body: response.body, headers: response.headers, status: response.code)
    end

    def delete(url)
      response = RestClient.delete(url, :accept => :json, authorization: @authorization)
      Response.new(body: response.body, headers: response.headers, status: response.code)
    end

    def put(url, data: {})
      begin
        response = RestClient.put(url, data, :accept => :json, authorization: @authorization)
      rescue RestClient::UnprocessableEntity => e
        raise HttpClient::UnprocessableEntity.new(e.response)
      end
      Response.new(body: response.body, headers: response.headers, status: response.code)
    end

    def post(url, data: {})
      begin
        response = RestClient.post(url, data, :accept => :json, authorization: @authorization)
      rescue RestClient::UnprocessableEntity => e
        raise HttpClient::UnprocessableEntity.new(e.response)
      end
      Response.new(body: response.body, headers: response.headers, status: response.code)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
restful_resource-0.8.34 lib/restful_resource/http_client.rb
restful_resource-0.8.33 lib/restful_resource/http_client.rb
restful_resource-0.8.32 lib/restful_resource/http_client.rb
restful_resource-0.8.30 lib/restful_resource/http_client.rb
restful_resource-0.8.29 lib/restful_resource/http_client.rb
restful_resource-0.8.28 lib/restful_resource/http_client.rb
restful_resource-0.8.27 lib/restful_resource/http_client.rb
restful_resource-0.8.26 lib/restful_resource/http_client.rb