Sha256: 7d786ca02e5fba0c4915ff58995a0972226ff3d3d73386fa25d0ccb2fba66875

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module RatingChgkV2
  module Request
    include RatingChgkV2::Connection
    include RatingChgkV2::JsonHandler

    def get(path, client, params = {})
      respond_with(
        connection(client).get(prepare(path), params),
        client
      )
    end

    def post(path, client, params = {})
      respond_with(
        connection(client).post(prepare(path), custom_dump(params)),
        client
      )
    end

    def put(path, client, params = {})
      respond_with(
        connection(client).put(prepare(path), custom_dump(params)),
        client
      )
    end

    def delete(path, client, _params = {})
      respond_with(
        # Rubocop tries to replace `delete` with `gsub` but that's a different method here!
        connection(client).delete(prepare(path)),
        client
      )
    end

    private

    # Get rid of double slashes in the `path`, leading and trailing slash
    def prepare(path)
      path.delete_prefix('/').gsub(%r{//}, '/').gsub(%r{/+\z}, '')
    end

    def respond_with(response, _client)
      body = response.body.empty? ? response.body : custom_load(response.body)
      status = response.status
      respond_with_error status, body if status.between?(400, 599)

      body
    end

    def respond_with_error(code, body)
      raise(RatingChgkV2::Error, body) unless RatingChgkV2::Error::ERRORS.key? code

      raise RatingChgkV2::Error::ERRORS[code].from_response(body)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rating-chgk-v2-1.0.0 lib/rating_chgk_v2/request.rb
rating-chgk-v2-1.0.0.rc1 lib/rating_chgk_v2/request.rb