Sha256: 99fc9c158ad340e3fa13211e23d9b83ab42906b0af447c8ccd1e559569c43339

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module FunTranslations
  # This module contains methods to perform HTTP requests
  module Request
    include FunTranslations::Connection

    # Performs an HTTP post request
    # @param path [String]
    # @param client [FunTranslations::Client]
    # @param params [Hash]
    def post(path, client, params = {})
      respond_with(
        connection(client).post(path, params)
      )
    end

    private

    # Parses response body using JSON
    # or raises an error if the status code is not 2xx
    def respond_with(response)
      body = response.body.empty? ? response.body : JSON.parse(response.body)

      respond_with_error(response.status, body['error']) if body.key?('error') || !response.success?

      body['contents']
    end

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fun_translations-0.0.1.rc1 lib/fun_translations/request.rb