Sha256: 87e1a0c1ddc3b5cd038c25c205279e95c4b9f772818f322990556ec94f87c7d8

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 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)
      raw_body = response.body
      body = raw_body.empty? ? raw_body : JSON.parse(raw_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.from_response(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 lib/fun_translations/request.rb