Sha256: de4bb40a0f13192d05b4e645e527430fd7bc4a64f63b1498b526fcc4d4aeb7a3

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

module Mpesa
  class Resource
    attr_reader :client, :args

    def initialize(client, args = {})
      @client = client
      @args = args
    end

    def get_request(url:, params: {}, headers: {}, basic_auth: true)
      handle_response client.connection(basic_auth: basic_auth).get(url, params, headers)
    end

    def post_request(url:, body: {}, headers: {})
      handle_response client.connection.post(url, body, headers)
    end

    def handle_response(response)
      case response.status
      when 400
        raise Error, "Your request was malformed. #{response.body['errorMessage']}"
      when 401
        raise Error, "You did not supply valid authentication credentials. #{response.body['errorMessage']}"
      when 403
        raise Error, "You are not allowed to perform that action. #{response.body['errorMessage']}"
      when 404
        raise Error, "No results were found for your request. #{response.body['errorMessage']}"
      when 500
        raise Error, "Something wrong happened. #{response.body['errorMessage']}"
      end
      response
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mpesarb-0.3.1 lib/mpesa/resource.rb
mpesarb-0.3.0 lib/mpesa/resource.rb