Sha256: d69617bff524045bcfb1f0705c1fc1c654237ab82050d9a4fca807fdeceb88e9

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 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(
        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

6 entries across 6 versions & 1 rubygems

Version Path
rating-chgk-v2-1.5.0 lib/rating_chgk_v2/request.rb
rating-chgk-v2-1.4.0 lib/rating_chgk_v2/request.rb
rating-chgk-v2-1.3.0 lib/rating_chgk_v2/request.rb
rating-chgk-v2-1.2.0 lib/rating_chgk_v2/request.rb
rating-chgk-v2-1.1.0 lib/rating_chgk_v2/request.rb
rating-chgk-v2-1.0.1 lib/rating_chgk_v2/request.rb